faulhaber_s_formulas_expanded_2.pl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/perl
  2. # Author: Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 21 September 2015
  5. # Website: https://github.com/trizen
  6. # The script generates formulas for calculating the sum
  7. # of consecutive numbers raised to a given power, such as:
  8. # 1^p + 2^p + 3^p + ... + n^p
  9. # where p is a positive integer.
  10. # See also: https://en.wikipedia.org/wiki/Faulhaber%27s_formula
  11. # To simplify the formulas, use Wolfram Alpha:
  12. # https://www.wolframalpha.com/
  13. use 5.010;
  14. use strict;
  15. use warnings;
  16. use Math::AnyNum qw(:overload);
  17. use Memoize qw( memoize );
  18. memoize('binomial');
  19. memoize('factorial');
  20. memoize('bernoulli_number');
  21. # Factorial
  22. # See: https://en.wikipedia.org/wiki/Factorial
  23. sub factorial {
  24. my ($n) = @_;
  25. return 1 if $n == 0;
  26. my $f = $n;
  27. while ($n-- > 1) {
  28. $f = "$f*$n";
  29. }
  30. return $f;
  31. }
  32. # Binomial coefficient
  33. # See: https://en.wikipedia.org/wiki/Binomial_coefficient
  34. sub binomial {
  35. my ($n, $k) = @_;
  36. ## This line expands the factorials
  37. #return "(".factorial($n) .")" . "/((" . factorial($k).")*(". factorial($n-$k) . "))";
  38. ## This line expands the binomial coefficients into factorials
  39. return "$n!/($k!*" . ($n - $k) . "!)";
  40. ## This line computes the binomial coefficients
  41. #$k == 0 || $n == $k ? 1.0 : binomial($n - 1, $k - 1) + binomial($n - 1, $k);
  42. }
  43. # Bernoulli numbers
  44. # See: https://en.wikipedia.org/wiki/Bernoulli_number#Algorithmic_description
  45. sub bernoulli_number {
  46. my ($n) = @_;
  47. # return 0 if $n > 1 && $n % 2; # Bn = 0 for all odd n > 1
  48. my @A;
  49. for my $m (0 .. $n) {
  50. $A[$m] = 1 / ($m + 1);
  51. for (my $j = $m ; $j > 0 ; $j--) {
  52. $A[$j - 1] = "$j*" . '(' . join('-', ($A[$j - 1], $A[$j])) . ')';
  53. }
  54. }
  55. return $A[0]; # which is Bn
  56. }
  57. # Faulhaber's formula
  58. # See: https://en.wikipedia.org/wiki/Faulhaber%27s_formula
  59. sub faulhaber_s_formula {
  60. my ($p, $n) = @_;
  61. my @formula;
  62. for my $j (0 .. $p) {
  63. push @formula, ('(' . (binomial($p + 1, $j) . "*" . bernoulli_number($j)) . ')') . '*' . "n^" . ($p + 1 - $j);
  64. }
  65. my $formula = join(' + ', @formula);
  66. "1/" . ($p + 1) . " * ($formula)";
  67. }
  68. for my $i (0 .. 5) {
  69. printf "%d => %s\n", $i, faulhaber_s_formula($i + 0);
  70. }
  71. __END__
  72. 0 => 1/1 * ((1!/(0!*1!)*1)*n^1)
  73. 1 => 1/2 * ((2!/(0!*2!)*1)*n^2 + (2!/(1!*1!)*1*(1-1/2))*n^1)
  74. 2 => 1/3 * ((3!/(0!*3!)*1)*n^3 + (3!/(1!*2!)*1*(1-1/2))*n^2 + (3!/(2!*1!)*1*(1*(1-1/2)-2*(1/2-1/3)))*n^1)
  75. 3 => 1/4 * ((4!/(0!*4!)*1)*n^4 + (4!/(1!*3!)*1*(1-1/2))*n^3 + (4!/(2!*2!)*1*(1*(1-1/2)-2*(1/2-1/3)))*n^2 + (4!/(3!*1!)*1*(1*(1*(1-1/2)-2*(1/2-1/3))-2*(2*(1/2-1/3)-3*(1/3-1/4))))*n^1)
  76. 4 => 1/5 * ((5!/(0!*5!)*1)*n^5 + (5!/(1!*4!)*1*(1-1/2))*n^4 + (5!/(2!*3!)*1*(1*(1-1/2)-2*(1/2-1/3)))*n^3 + (5!/(3!*2!)*1*(1*(1*(1-1/2)-2*(1/2-1/3))-2*(2*(1/2-1/3)-3*(1/3-1/4))))*n^2 + (5!/(4!*1!)*1*(1*(1*(1*(1-1/2)-2*(1/2-1/3))-2*(2*(1/2-1/3)-3*(1/3-1/4)))-2*(2*(2*(1/2-1/3)-3*(1/3-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