prog.sf 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/ruby
  2. # Least number k such that k^n and k^n-1 contain the same number of prime factors (counted with multiplicity) or 0 if no such k exists.
  3. # https://oeis.org/A241793
  4. # Known terms:
  5. # 3, 34, 5, 15, 17, 55, 79, 5, 53, 23, 337, 13, 601, 79, 241, 41, 18433, 31, 40961, 89, 3313, 1153
  6. # Lower-bounds:
  7. # a(23) > 206593
  8. include("../../../factordb/auto.sf")
  9. func check_partial_factors(f,n) {
  10. f.sum {|p| p.is_prime ? 1 : 2 } > n && return false
  11. if (f.all_prime) {
  12. if (f.len == n) {
  13. return true
  14. }
  15. return false
  16. }
  17. return true
  18. }
  19. func a(m, from=2) {
  20. for k in (from..Inf) {
  21. var n = bigomega(k)*m
  22. var v = (k**m - 1)
  23. say "[#{n}] Checking: #{k}"
  24. var tf = v.trial_factor(1e6)
  25. check_partial_factors(tf, n) || next
  26. tf.len.dec + tf.last.ilog(1e6) + 1 >= n || next
  27. tf = v.trial_factor(1e7)
  28. check_partial_factors(tf, n) || next
  29. tf.len.dec + tf.last.ilog(1e7) + 1 >= n || next
  30. if (tf.last > 1e60) {
  31. tf = v.trial_factor(1e8)
  32. check_partial_factors(tf, n) || next
  33. tf.len.dec + tf.last.ilog(1e8) + 1 >= n || next
  34. }
  35. say "Many factors (at least #{tf.len-1 + (tf.last.is_prime ? 1 : 2)} with C#{tf.last.len}): #{v}"
  36. var ff = v.special_factor
  37. check_partial_factors(ff, n) || next
  38. var sf = []
  39. if (v % (k-1) == 0) {
  40. sf = gcd_factors(v, tf + ff + factordb(v/(k-1)))
  41. check_partial_factors(sf, n) || next
  42. }
  43. var f = factordb(v)
  44. check_partial_factors(f, n) || next
  45. var f3 = gcd_factors(v, sf + tf + ff + f)
  46. check_partial_factors(f3, n) || next
  47. if ((f3.last > 1e65) && f3.last.is_composite) {
  48. tf = v.trial_factor(1e9)
  49. check_partial_factors(tf, n) || next
  50. tf.len.dec + tf.last.ilog(1e9) + 1 >= n || next
  51. say "Strong candidate..."
  52. f3 = gcd_factors(v, tf + ff + f)
  53. check_partial_factors(f3, n) || next
  54. var pf = f3.grep{.is_prime}
  55. var c = (v / pf.prod)
  56. pf.len + c.ilog(1e9) + 1 >= n || next
  57. say "Factoring C#{c.len}: #{c}"
  58. }
  59. f3 = f3.map{ .is_prime ? _ : factordb(_) }.flat
  60. check_partial_factors(f3, n) || next
  61. f3 = f3.map{ .factor }.flat
  62. check_partial_factors(f3, n) || next
  63. if (f3.len == n) {
  64. return k
  65. }
  66. }
  67. }
  68. var from = 206593+1
  69. for n in (23) {
  70. say "a(#{n}) = #{a(n, from)}"
  71. }