gamma_function.pl 496 B

1234567891011121314151617181920212223242526272829
  1. #!/usr/bin/perl
  2. # Author: Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 17 November 2015
  5. # Website: https://github.com/trizen
  6. # The gamma function implemented as an improper integral
  7. # See: https://en.wikipedia.org/wiki/Gamma_function
  8. use 5.010;
  9. use strict;
  10. use warnings;
  11. sub gamma {
  12. my ($n) = @_;
  13. my $sum = 0;
  14. for my $t (0 .. 1000) {
  15. $sum += $t**($n - 1) * exp(-$t);
  16. }
  17. return $sum;
  18. }
  19. for my $n (1 .. 20) {
  20. printf "gamma(%2d) = %.24f\n", $n, gamma($n);
  21. }