prog2.pl 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/perl
  2. # Numbers with record number of iterations of x -> A306938(x) required to reach 1 (A306944).
  3. # https://oeis.org/A306978
  4. use 5.020;
  5. use warnings;
  6. use Math::AnyNum qw(:overload ceil sqrt floor round);
  7. use experimental qw(signatures);
  8. sub f ($n) {
  9. my $count = 0;
  10. my $cons = 0;
  11. while ($n != 1) {
  12. ++$count;
  13. if ($n % 3 == 0) {
  14. $n = int($n/3);
  15. $cons = 0;
  16. }
  17. else {
  18. if ($cons) {
  19. return -1;
  20. }
  21. $n = int($n * sqrt(3));
  22. $cons = 1;
  23. }
  24. }
  25. $count;
  26. }
  27. # Conjecture: sqrt(3) < a(n)/a(n-1) <= 3. - ~~~~
  28. my $prev = 1;
  29. foreach my $k(
  30. 2, 4, 7, 21, 49, 85, 253, 442, 766, 1327, 2299, 3982, 11839, 20506, 35518, 61519, 184557, 553645, 966928, 1674769, 2900785, 8701141, 25877593, 44821306, 77676682, 134539960, 402368674, 696922987, 1207106023, 2090768962, 3632578906,
  31. 6291811228, 10897736719, 18875433685
  32. ) {
  33. #say "$k -> ", ceil($k * sqrt(3));
  34. say "$k -> ", $k/$prev, ' -> ', ($k/$prev) > sqrt(3);
  35. $prev = $k;
  36. }
  37. __END__
  38. my $prev = 6291811228;
  39. foreach my $n(1..10) {
  40. my $curr = ceil($prev * sqrt(3));
  41. say $curr;
  42. if (f($curr) == -1) {
  43. $curr = $prev * 3;
  44. say "$curr -> ", f($curr);
  45. }
  46. else {
  47. say "$curr -> ", f($curr);
  48. }
  49. $prev = $curr;
  50. }
  51. __END__
  52. foreach my $k(77676682, 134539960, 402368674, 696922987, 1207106023, 2090768962, 3632578906, 6291811228, 10897736719, 18875433685, 56626301055) {
  53. say "$k -> ", f($k);
  54. }
  55. __END__
  56. # 6291811228 -- 55
  57. # 10897736719 -- 57
  58. # 18875433685 -- 59
  59. my $record = 59;
  60. foreach my $k(18875433685 * 1.732 ..1e11) {
  61. if (f($k) > $record) {
  62. $record = f($k);
  63. say "New record: $record with $k";
  64. }
  65. }