narcisistic_harmonic_numbers.sf 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/ruby
  2. # Numbers k such that the decimal expansion of the k-th harmonic number starts with the digits of k, in the same order.
  3. # https://oeis.org/A337904
  4. # This program generates such values of `k` (with possible gaps?).
  5. func isok(k) {
  6. k.harmreal.to_s.sub('.', '').first(k.len) == k.to_s
  7. }
  8. var n = 1
  9. for k in (1..100) {
  10. local Num!PREC = numify(4*(n + 10))
  11. if (harmreal(k * 10**n).to_s.begins_with(k.to_s)) {
  12. var (min, max) = (harmreal(k * 10**n) * 10**n, harmreal((k+1) * 10**n) * 10**n)
  13. var v = bsearch_le(min, max, {|k|
  14. (k / 10**n) <=> harmreal(k)
  15. })
  16. var ok = false
  17. if (isok(v-1)) {
  18. say v-1
  19. ok = true
  20. }
  21. if (isok(v)) {
  22. say v
  23. ok = true
  24. }
  25. ++n if ok
  26. }
  27. }
  28. __END__
  29. 43
  30. 714
  31. 715
  32. 9763
  33. 122968
  34. 122969
  35. 1478366
  36. 17239955
  37. 196746419
  38. 2209316467
  39. 24499118645
  40. 268950072605
  41. 2928264676792
  42. 31663398162514
  43. 340383084842938
  44. 3640820101879826
  45. 38773735148047406
  46. 38773735148047407
  47. 411354473689969709
  48. 4349378018952283972
  49. 45849102972919835109
  50. 482017272880546223321
  51. 5055191842047792613562
  52. 52899904144628296988725
  53. 552458815055031196782525
  54. 5759002212954326201649741
  55. 59932476432458393252342721
  56. 622733769885549555429595981
  57. 6461284125481523641339765370
  58. 66950973792848320411792499601
  59. 692878680960562864969363384910
  60. 7162360810346449261192969637028
  61. 73958269462680913376461650794152
  62. 762919205903660944920523210661377
  63. 7862462356803215299533205007813806
  64. 80956434902985402104622557703016977
  65. 832874061781047994819672246576419514
  66. 8561758460942363424315837377300688366
  67. 87947013526805356793864093102783600499
  68. 902757327901213687376081267798908754616
  69. 9260377915106116852616564230637740582108
  70. 94931186568893878451195580899916418846736
  71. 972579864870501412683269238042780744028743
  72. 9958420807622547975400738352360988337988344