almost_primes.sf 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/ruby
  2. # a(n) is the smallest centered n-gonal number with exactly n prime factors (counted with multiplicity).
  3. # https://oeis.org/A358926
  4. # Known terms:
  5. # 316, 1625, 456, 3964051, 21568, 6561, 346528
  6. # PARI/GP program:
  7. # a(n) = if(n<3, return()); for(k=1, oo, my(t=((n*k*(k+1))/2+1)); if(bigomega(t) == n, return(t))); \\ ~~~~
  8. # PARI/GP generation program:
  9. #`(
  10. bigomega_polygonals(A, B, n, k) = A=max(A, 2^n); (f(m, p, n) = my(list=List()); if(n==1, forprime(q=max(p,ceil(A/m)), B\m, my(t=m*q); if(issquare((8*(t-1))/k + 1) && ((sqrtint((8*(t-1))/k + 1)-1)%2 == 0), listput(list, t))), forprime(q = p, sqrtnint(B\m, n), my(t=m*q); if(ceil(A/t) <= B\t, list=concat(list, f(t, q, n-1))))); list); vecsort(Vec(f(1, 2, n)));
  11. a(n, k=n) = if(k < 3, return()); my(x=2^n, y=2*x); while(1, my(v=bigomega_polygonals(x, y, n, k)); if(#v >= 1, return(v[1])); x=y+1; y=2*x); \\ ~~~~
  12. # Version with primes in certain residue classes
  13. bigomega_polygonals(A, B, n, k) = A=max(A, 2^n); (f(m, p, n) = my(list=List()); if(n==1, forprime(q=max(p,ceil(A/m)), B\m, my(s=q%k); if(s==1 || s==7 || s==9 || s==15, my(t=m*q); if(issquare((8*(t-1))/k + 1) && ((sqrtint((8*(t-1))/k + 1)-1)%2 == 0), listput(list, t)))), forprime(q = p, sqrtnint(B\m, n), my(s=q%k); if(s==1 || s==7 || s==9 || s==15, my(t=m*q); if(ceil(A/t) <= B\t, list=concat(list, f(t, q, n-1)))))); list); vecsort(Vec(f(1, 2, n)));
  14. a(n, k=n) = if(k < 3, return()); my(x=2^n, y=2*x); while(1, my(v=bigomega_polygonals(x, y, n, k)); if(#v >= 1, return(v[1])); x=y+1; y=2*x); \\ ~~~~
  15. )
  16. func upper_bound(n, from = 2, upto = 2*from) {
  17. say "\n:: Searching an upper-bound for a(#{n})\n"
  18. loop {
  19. var count = n.almost_prime_count(from, upto)
  20. if (count > 0) {
  21. say "Sieving range: [#{from}, #{upto}]"
  22. say "This range contains: #{count.commify} elements\n"
  23. n.almost_primes_each(from, upto, {|v|
  24. if (v.is_centered_polygonal(n)) {
  25. say "a(#{n}) = #{v}"
  26. return v
  27. }
  28. })
  29. }
  30. from = upto+1
  31. upto *= 2
  32. }
  33. }
  34. upper_bound(16)
  35. __END__
  36. a(3) = 316
  37. a(4) = 1625
  38. a(5) = 456
  39. a(6) = 3964051
  40. a(7) = 21568
  41. a(8) = 6561
  42. a(9) = 346528
  43. a(10) = 3588955448828761
  44. a(11) = 1684992
  45. a(12) = 210804461608463437
  46. a(13) = 36865024
  47. a(14) = 835904150390625
  48. a(15) = 2052407296