prog.pl 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # Date: 25 March 2019
  4. # https://github.com/trizen
  5. # Generate a visual representation of the Pascal-Fibonacci triangle.
  6. # Definition by Elliott Line, Mar 22 2019:
  7. # Consider a version of Pascal's Triangle: a triangular array with a single 1 on row 0,
  8. # with numbers below equal to the sum of the two numbers above it if and only if that sum
  9. # appears in the Fibonacci sequence. If the sum is not a Fibonacci number, `1` is put in its place.
  10. # OEIS sequence:
  11. # https://oeis.org/A307069
  12. use 5.010;
  13. use strict;
  14. use warnings;
  15. use ntheory qw(is_square lucasu);
  16. use experimental qw(signatures);
  17. sub is_fibonacci($n) {
  18. my $m = 5 * $n * $n;
  19. is_square($m - 4) or is_square($m + 4);
  20. }
  21. my %fib;
  22. foreach my $n (0 .. 100) {
  23. $fib{lucasu(1, -1, $n)} = $n;
  24. }
  25. my %seen;
  26. my @row = (1);
  27. foreach my $n (1 .. 1e6) {
  28. if ($n % 10_000 == 0) {
  29. say "Processing row $n...";
  30. open my $fh, '>', "row_$n.txt";
  31. say $fh "$n\n@row";
  32. close $fh;
  33. }
  34. my @t = (
  35. map {
  36. my $t = $row[$_] + $row[$_ + 1];
  37. if (exists($fib{$t})) {
  38. if (not exists $seen{$t}) {
  39. $seen{$t} = 1;
  40. say "$t -> $n";
  41. open my $fh, '>', "found_$t.txt";
  42. say $fh sprintf("%s\n%s", $n - 1, "@row");
  43. close $fh;
  44. }
  45. $t;
  46. }
  47. else {
  48. 1;
  49. }
  50. } 0 .. ($n - ($n % 2)) / 2 - 1
  51. );
  52. #~ if ($n < 20) {
  53. #~ say "@row";
  54. #~ }
  55. my @u = reverse(@t);
  56. if ($n % 2 == 0) {
  57. shift @u;
  58. }
  59. @row = (1, @t, @u, 1);
  60. }
  61. __END__
  62. 1 -> 1
  63. 2 -> 1 1
  64. 3 -> 1 2 1
  65. 4 -> 1 3 3 1
  66. 5 -> 1 1 1 1 1
  67. 6 -> 1 2 2 2 2 1
  68. 7 -> 1 3 1 1 1 3 1
  69. 8 -> 1 1 1 2 2 1 1 1
  70. 9 -> 1 2 2 3 1 3 2 2 1
  71. 10 -> 1 3 1 5 1 1 5 1 3 1