number_of_partitions_into_2_distinct_positive_squares.pl 764 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/perl
  2. # Count the number of partitions of n into 2 distinct nonzero squares.
  3. # See also:
  4. # https://oeis.org/A025441
  5. # https://mathworld.wolfram.com/SumofSquaresFunction.html
  6. # https://en.wikipedia.org/wiki/Fermat%27s_theorem_on_sums_of_two_squares
  7. use 5.020;
  8. use warnings;
  9. use experimental qw(signatures);
  10. use ntheory qw(:all);
  11. # Number of solutions to `n = a^2 + b^2, with 0 < a < b.
  12. sub r2_positive_distinct ($n) {
  13. my $B = 1;
  14. foreach my $p (factor_exp($n)) {
  15. my $r = $p->[0] % 4;
  16. if ($r == 3) {
  17. $p->[1] % 2 == 0 or return 0;
  18. }
  19. if ($r == 1) {
  20. $B *= $p->[1] + 1;
  21. }
  22. }
  23. return ($B >> 1);
  24. }
  25. foreach my $n(1..100) {
  26. print(r2_positive_distinct($n), ", ");
  27. }