polygonal_representations.pl 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # Date: 08 March 2018
  4. # https://github.com/trizen
  5. # Find all the possible polygonal representations P(a,b) for a given number `n`.
  6. # Example:
  7. # 235 = P(5, 25) = P(235, 2) = P(10, 7)
  8. # See also:
  9. # https://oeis.org/A176774
  10. use 5.020;
  11. use strict;
  12. use warnings;
  13. use experimental qw(signatures);
  14. use ntheory qw(divisors);
  15. use Math::AnyNum qw(:overload polygonal);
  16. sub polygonal_representations ($n) {
  17. my @divisors = divisors(2 * $n);
  18. shift @divisors; # skip d=1
  19. my @representations;
  20. foreach my $d (@divisors) {
  21. my $t = $d - 1;
  22. my $k = (2*$n / $d + 2*$d - 4);
  23. if ($k % $t == 0) {
  24. push @representations, [$d, $k / $t];
  25. }
  26. }
  27. return @representations;
  28. }
  29. foreach my $i (1 .. 30) {
  30. my $n = 2**$i + 1;
  31. my @P = polygonal_representations($n);
  32. # Display the solutions
  33. say "2^$i + 1 = ", join(' = ', map { "P($_->[0], $_->[1])" } @P);
  34. # Verify the solutions
  35. die 'error' if grep { $_ != $n } map { polygonal($_->[0], $_->[1]) } @P;
  36. }
  37. __END__
  38. 2^1 + 1 = P(2, 3) = P(3, 2)
  39. 2^2 + 1 = P(2, 5) = P(5, 2)
  40. 2^3 + 1 = P(2, 9) = P(3, 4) = P(9, 2)
  41. 2^4 + 1 = P(2, 17) = P(17, 2)
  42. 2^5 + 1 = P(2, 33) = P(3, 12) = P(33, 2)
  43. 2^6 + 1 = P(2, 65) = P(5, 8) = P(65, 2)
  44. 2^7 + 1 = P(2, 129) = P(3, 44) = P(129, 2)
  45. 2^8 + 1 = P(2, 257) = P(257, 2)
  46. 2^9 + 1 = P(2, 513) = P(3, 172) = P(9, 16) = P(513, 2)
  47. 2^10 + 1 = P(2, 1025) = P(5, 104) = P(1025, 2)
  48. 2^11 + 1 = P(2, 2049) = P(3, 684) = P(2049, 2)
  49. 2^12 + 1 = P(2, 4097) = P(17, 32) = P(4097, 2)
  50. 2^13 + 1 = P(2, 8193) = P(3, 2732) = P(8193, 2)
  51. 2^14 + 1 = P(2, 16385) = P(5, 1640) = P(16385, 2)
  52. 2^15 + 1 = P(2, 32769) = P(3, 10924) = P(9, 912) = P(33, 64) = P(32769, 2)
  53. 2^16 + 1 = P(2, 65537) = P(65537, 2)
  54. 2^17 + 1 = P(2, 131073) = P(3, 43692) = P(131073, 2)
  55. 2^18 + 1 = P(2, 262145) = P(5, 26216) = P(65, 128) = P(262145, 2)
  56. 2^19 + 1 = P(2, 524289) = P(3, 174764) = P(524289, 2)
  57. 2^20 + 1 = P(2, 1048577) = P(17, 7712) = P(1048577, 2)
  58. 2^21 + 1 = P(2, 2097153) = P(3, 699052) = P(9, 58256) = P(129, 256) = P(2097153, 2)
  59. 2^22 + 1 = P(2, 4194305) = P(5, 419432) = P(4194305, 2)
  60. 2^23 + 1 = P(2, 8388609) = P(3, 2796204) = P(8388609, 2)
  61. 2^24 + 1 = P(2, 16777217) = P(257, 512) = P(16777217, 2)
  62. 2^25 + 1 = P(2, 33554433) = P(3, 11184812) = P(33, 63552) = P(33554433, 2)
  63. 2^26 + 1 = P(2, 67108865) = P(5, 6710888) = P(67108865, 2)
  64. 2^27 + 1 = P(2, 134217729) = P(3, 44739244) = P(9, 3728272) = P(513, 1024) = P(134217729, 2)
  65. 2^28 + 1 = P(2, 268435457) = P(17, 1973792) = P(268435457, 2)
  66. 2^29 + 1 = P(2, 536870913) = P(3, 178956972) = P(536870913, 2)
  67. 2^30 + 1 = P(2, 1073741825) = P(5, 107374184) = P(65, 516224) = P(1025, 2048) = P(1073741825, 2)