prog.sf 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #!/usr/bin/ruby
  2. # Smallest palindrome with exactly n distinct prime factors.
  3. # https://oeis.org/A335645
  4. # Known terms:
  5. # 1, 2, 6, 66, 858, 6006, 222222, 20522502, 244868442, 6172882716, 231645546132, 49795711759794, 2415957997595142, 495677121121776594, 22181673755737618122
  6. # New term found:
  7. # a(15) = 5521159517777159511255 (took 3h, 40min, 22,564 ms.)
  8. #`( PARI/GP program:
  9. omega_palindromes(A, B, n) = A=max(A, vecprod(primes(n))); (f(m, p, j) = my(list=List()); forprime(q=p, sqrtnint(B\m, j), my(v=m*q); if(q == 5 && v%2 == 0, next); while(v <= B, if(j==1, if(v>=A && fromdigits(Vecrev(digits(v))) == v, listput(list, v)), if(v*(q+1) <= B, list=concat(list, f(v, q+1, j-1)))); v *= q)); list); vecsort(Vec(f(1, 2, n)));
  10. a(n) = if(n==0, return(1)); my(x=vecprod(primes(n)), y=2*x); while(1, my(v=omega_palindromes(x, y, n)); if(#v >= 1, return(v[1])); x=y+1; y=2*x); \\ ~~~~
  11. )
  12. func a(n, from = 2, upto = 2*from) {
  13. say "\n:: Searching an upper-bound for a(#{n})\n"
  14. loop {
  15. var count = n.omega_prime_count(from, upto)
  16. if (count > 0) {
  17. say "Sieving range: [#{from}, #{upto}]"
  18. say "This range contains: #{count.commify} elements\n"
  19. n.omega_primes(from, upto).each {|v|
  20. if (v.is_palindrome) {
  21. say "a(#{n}) = #{v}"
  22. return v
  23. }
  24. }
  25. }
  26. from = upto+1
  27. upto *= 2
  28. }
  29. }
  30. a(11)