period_of_continued_fraction_for_square_roots.pl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # Date: 31 August 2016
  4. # License: GPLv3
  5. # https://github.com/trizen
  6. # Compute the period length of the continued fraction for square root of a given number.
  7. # Algorithm from:
  8. # http://web.math.princeton.edu/mathlab/jr02fall/Periodicity/mariusjp.pdf
  9. # OEIS sequences:
  10. # https://oeis.org/A003285 -- Period of continued fraction for square root of n (or 0 if n is a square).
  11. # https://oeis.org/A059927 -- Period length of the continued fraction for sqrt(2^(2n+1)).
  12. # https://oeis.org/A064932 -- Period length of the continued fraction for sqrt(3^(2n+1)).
  13. # https://oeis.org/A067280 -- Terms in continued fraction for sqrt(n), excl. 2nd and higher periods.
  14. # See also:
  15. # https://en.wikipedia.org/wiki/Continued_fraction
  16. # http://mathworld.wolfram.com/PeriodicContinuedFraction.html
  17. # TODO:
  18. # https://oeis.org/A061682 Quotient cycle length in continued fraction expansion of sqrt(2^(2n+1)+1).
  19. # 133093360, 1019300318, 60079334
  20. # a(29)-a(31) from ~~~~
  21. # https://oeis.org/A077098 Quotient cycle length in continued fraction expansion of sqrt(-1+n^n). (no luck)
  22. # 0, 2, 1, 2, 10, 2, 26, 2, 2, 2, 84531, 2, 531160, 2, 4738, 2,
  23. # https://oeis.org/A077097 Quotient cycle length in continued fraction expansion of sqrt(1+n^n).
  24. # 1, 1, 4, 1, 30, 1, 32, 1, 1, 1, 20554, 1, 23522, 1, 1013500, 1, 29130218, 1,
  25. # Submit new sequence:
  26. # Quotient cycle length in continued fraction expansion of sqrt(2^(2n+1)-1).
  27. # 4, 8, 12, 20, 12, 164, 40, 40, 1208, 660, 1304, 3056, 2492, 1080, 13004, 10232, 11296, 148736, 56576, 615482, 44448, 64, 2628524, 28219952, 139558, 3067080, 2683626, 90740360,
  28. use 5.010;
  29. use strict;
  30. use warnings;
  31. use Math::GMPz;
  32. use Math::AnyNum qw(prod binomial hyperfactorial superfactorial subfactorial);
  33. use ntheory qw(is_square sqrtint powint divint factorial pn_primorial consecutive_integer_lcm nth_prime);
  34. sub period_length_mpz {
  35. my ($n) = @_;
  36. $n = Math::GMPz->new("$n");
  37. return 0 if Math::GMPz::Rmpz_perfect_square_p($n);
  38. my $t = Math::GMPz::Rmpz_init();
  39. my $x = Math::GMPz::Rmpz_init();
  40. my $y = Math::GMPz::Rmpz_init_set($x);
  41. my $z = Math::GMPz::Rmpz_init_set_ui(1);
  42. Math::GMPz::Rmpz_sqrt($x, $n);
  43. my $period = 0;
  44. do {
  45. Math::GMPz::Rmpz_add($t, $x, $y);
  46. Math::GMPz::Rmpz_div($t, $t, $z);
  47. Math::GMPz::Rmpz_mul($t, $t, $z);
  48. Math::GMPz::Rmpz_sub($y, $t, $y);
  49. Math::GMPz::Rmpz_mul($t, $y, $y);
  50. Math::GMPz::Rmpz_sub($t, $n, $t);
  51. Math::GMPz::Rmpz_div($z, $t, $z);
  52. ++$period;
  53. } until (Math::GMPz::Rmpz_cmp_ui($z, 1) == 0);
  54. return $period;
  55. }
  56. sub period_length {
  57. my ($n) = @_;
  58. if (ref($n)) {
  59. die "\nerror\n";
  60. return period_length_mpz($n);
  61. }
  62. # die "\nerror\n" if ref $n;
  63. my $x = sqrtint($n);
  64. my $y = $x;
  65. my $z = 1;
  66. return 0 if is_square($n);
  67. my $period = 0;
  68. do {
  69. $y = int(($x + $y)/ $z) * $z - $y;
  70. $z = int(($n - $y * $y)/ $z);
  71. #say "$y $z -- $n";
  72. ++$period;
  73. } until ($z == 1);
  74. return $period;
  75. }
  76. local $| = 1;
  77. for my $n (16) {
  78. #print period_length_mpz(Math::GMPz->new(3)**(2*$n+1)), ", ";
  79. #print period_length_mpz(Math::GMPz->new(2)**(2*$n+1) + 1), ", ";
  80. print period_length_mpz(Math::GMPz->new($n)**$n + 1), ", ";
  81. #print period_length(powint($n, $n) + 1), ", ";
  82. #Math::GMPz->new(3)**(2*$n + 1)), ", ";
  83. #next if is_square($n);
  84. #say period_length(powint($n, 2 * 7 + 1)) / powint($n, 7);
  85. }
  86. __END__
  87. A064932(1) = 2
  88. A064932(2) = 10
  89. A064932(3) = 30
  90. A064932(4) = 98
  91. A064932(5) = 270
  92. A064932(6) = 818
  93. A064932(7) = 2382
  94. A064932(8) = 7282
  95. A064932(9) = 21818
  96. A064932(10) = 65650
  97. A064932(11) = 196406
  98. A064932(12) = 589982
  99. A064932(13) = 1768938
  100. A064932(14) = 5309294