lucas-carmichael_numbers_in_range.sf 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/ruby
  2. # Daniel "Trizen" Șuteu
  3. # Date: 29 August 2022
  4. # https://github.com/trizen
  5. # Generate all the Lucas-Carmichael numbers with n prime factors in a given range [a,b]. (not in sorted order)
  6. # See also:
  7. # https://en.wikipedia.org/wiki/Almost_prime
  8. # https://trizenx.blogspot.com/2020/08/pseudoprimes-construction-methods-and.html
  9. #`{
  10. # PARI/GP program (up to n) (slow):
  11. lucas_carmichael_upto(n, k) = my(A=vecprod(primes(k+1))\2, B=n); (f(m, l, p, k, u=0, v=0) = my(list=List()); if(k==1, forprime(p=u, v, my(t=m*p); if((t+1)%l == 0 && (t+1)%(p+1) == 0, listput(list, t))), forprime(q = p, sqrtnint(B\m, k), my(t = m*q); my(L=lcm(l, q+1)); if(gcd(L, t) == 1, my(u=ceil(A/t), v=B\t); if(u <= v, my(r=nextprime(q+1)); if(k==2 && r>u, u=r); list=concat(list, f(t, L, r, k-1, u, v)))))); list); vecsort(Vec(f(1, 1, 3, k)));
  12. # PARI/GP program (in range [A,B]) (slow):
  13. lucas_carmichael(A, B, k) = A=max(A, vecprod(primes(k+1))\2); (f(m, l, p, k, u=0, v=0) = my(list=List()); if(k==1, forprime(p=u, v, my(t=m*p); if((t+1)%l == 0 && (t+1)%(p+1) == 0, listput(list, t))), forprime(q = p, sqrtnint(B\m, k), my(t = m*q); my(L=lcm(l, q+1)); if(gcd(L, t) == 1, my(u=ceil(A/t), v=B\t); if(u <= v, my(r=nextprime(q+1)); if(k==2 && r>u, u=r); list=concat(list, f(t, L, r, k-1, u, v)))))); list); vecsort(Vec(f(1, 1, 3, k)));
  14. # PARI/GP program (in range [A, B]) (fast):
  15. lucas_carmichael(A, B, k) = A=max(A, vecprod(primes(k+1))\2); my(max_p=sqrtint(B+1)-1); (f(m, l, lo, k) = my(list=List()); my(hi=min(max_p, sqrtnint(B\m, k))); if(lo > hi, return(list)); if(k==1, lo=max(lo, ceil(A/m)); my(t=lift(-1/Mod(m,l))); while(t < lo, t += l); forstep(p=t, hi, l, if(isprime(p), my(n=m*p); if((n+1)%(p+1) == 0, listput(list, n)))), forprime(p=lo, hi, if(gcd(m, p+1) == 1, list=concat(list, f(m*p, lcm(l, p+1), p+1, k-1))))); list); vecsort(Vec(f(1, 1, 3, k)));
  16. # PARI/GP program to generate all the Lucas-Carmichael numbers <= n (fast):
  17. lucas_carmichael(A, B, k) = A=max(A, vecprod(primes(k+1))\2); my(max_p=sqrtint(B+1)-1); (f(m, l, lo, k) = my(list=List()); my(hi=min(max_p, sqrtnint(B\m, k))); if(lo > hi, return(list)); if(k==1, lo=max(lo, ceil(A/m)); my(t=lift(-1/Mod(m,l))); while(t < lo, t += l); forstep(p=t, hi, l, if(isprime(p), my(n=m*p); if((n+1)%(p+1) == 0, listput(list, n)))), forprime(p=lo, hi, if(gcd(m, p+1) == 1, list=concat(list, f(m*p, lcm(l, p+1), p+1, k-1))))); list); f(1, 1, 3, k);
  18. upto(n) = my(list=List()); for(k=3, oo, if(vecprod(primes(k+1))\2 > n, break); list=concat(list, lucas_carmichael(1, n, k))); vecsort(Vec(list));
  19. }
  20. func lucas_carmichael_numbers_in_range(A, B, k, callback) {
  21. A = max(pn_primorial(k+1)/2, A)
  22. # Largest possisble prime factor for Lucas-Carmichael numbers <= B
  23. var max_p = B.isqrt
  24. func (m, L, lo, k) {
  25. var hi = idiv(B,m).iroot(k)
  26. if (lo > hi) {
  27. return nil
  28. }
  29. if (k == 1) {
  30. hi = max_p if (hi > max_p)
  31. lo = max(lo, idiv_ceil(A, m))
  32. lo > hi && return nil
  33. var t = mulmod(m.invmod(L), -1, L)
  34. t > hi && return nil
  35. t += L*idiv_ceil(lo - t, L) if (t < lo)
  36. t > hi && return nil
  37. for p in (range(t, hi, L)) {
  38. p.is_prime || next
  39. with (m*p) {|n|
  40. if (p.inc `divides` n.inc) {
  41. callback(n)
  42. }
  43. }
  44. }
  45. return nil
  46. }
  47. each_prime(lo, hi, {|p|
  48. m.is_coprime(p+1) || next
  49. __FUNC__(m*p, lcm(L, p+1), p+1, k-1)
  50. })
  51. }(1, 1, 3, k)
  52. return callback
  53. }
  54. # High-level implementation (unoptimized):
  55. func lucas_carmichael_numbers_in_range_unoptimized(A, B, k, callback) {
  56. func F(m, L, p, k) {
  57. if (k == 1) {
  58. each_prime(max(p, ceil(A/m)), min(B.isqrt, floor(B/m)), {|q|
  59. if (lcm(L, q+1) `divides` (m*q + 1)) {
  60. callback(m*q)
  61. }
  62. })
  63. }
  64. elsif (k >= 2) {
  65. for q in (primes(p, (B/m)**(1/k))) {
  66. if (gcd(lcm(L, q+1), m*q) == 1) {
  67. F(m*q, lcm(L, q+1), q.next_prime, k-1)
  68. }
  69. }
  70. }
  71. }
  72. F(1, 1, 3, k)
  73. }
  74. # Generate all the 5-Lucas-Carmichael numbers in the range [100, 2*10^7]
  75. var k = 5
  76. var from = 100
  77. var upto = 2e7
  78. say gather { lucas_carmichael_numbers_in_range(from, upto, k, { take(_) }) }.sort
  79. say gather { lucas_carmichael_numbers_in_range_unoptimized(from, upto, k, { take(_) }) }.sort
  80. __END__
  81. [588455, 1010735, 2276351, 2756159, 4107455, 4874639, 5669279, 6539819, 8421335, 13670855, 16184663, 16868159]