prog.sf 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/ruby
  2. # Number of Lucas-Carmichael numbers less than 10^n.
  3. # https://oeis.org/A216929
  4. # Known terms:
  5. # 0, 0, 2, 8, 26, 60, 135, 323, 791, 1840, 4216, 9967
  6. # New terms (12 November 2023):
  7. # a(13) = 23070 (took 5 minutes)
  8. # a(14) = 54356 (took ~1 hour)
  9. # a(15) = 129125 (took ~5 hours)
  10. #`{
  11. # PARI/GP programs:
  12. # Generate Lucas-Carmichael numbers <= n (slow)
  13. isok(n) = my(f=factor(n)); for(k=1, #f~, if ((n+1) % (f[k,1]+1) != 0 || f[k,2] >= 2, return(0))); #f~ > 1;
  14. upto(n) = my(A=List()); forprime(p=3, sqrtint(n+1)-1, my(t = p*(p+1), u = p+t); while(u <= n, if(isok(u), listput(A, u)); u += t)); vecsort(Set(A));
  15. # Count the number of Lucas-Carmichael numbers <= n (slow)
  16. islc(n) = my(f=factor(n)); for(k=1, #f~, if ((n+1) % (f[k,1]+1) != 0 || f[k,2] >= 2, return(0))); #f~ > 1;
  17. a(n) = my(A=List()); forprime(p=3, sqrtint(n+1)-1, my(t = p*(p+1), u = p+t); while(u <= n, if(islc(u), listput(A, u)); u += t)); #Set(A);
  18. # Count the number of Lucas-Carmichael numbers <= n (fast)
  19. lucas_carmichael(A, B, k) = A=max(A, vecprod(primes(k+1))\2); (f(m, l, lo, k) = my(list=List()); my(hi=min(sqrtint(B+1)-1, 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);
  20. a(n) = my(N=10^n); my(count=0); for(k=3, oo, if(vecprod(primes(k+1))\2 > N, break); count += #lucas_carmichael(1, N, k)); count;
  21. # Generate Lucas-Carmichael numbers <= n (fast)
  22. lucas_carmichael(A, B, k) = A=max(A, vecprod(primes(k+1))\2); (f(m, l, lo, k) = my(list=List()); my(hi=min(sqrtint(B+1)-1, 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);
  23. 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));
  24. }
  25. var min = 1
  26. var max = 10
  27. var n = 0
  28. var count = 0
  29. loop {
  30. for k in (3..Inf) {
  31. if (pn_primorial(k+1)/2 > max) {
  32. break
  33. }
  34. count += k.lucas_carmichael(min, max).len
  35. }
  36. say "a(#{++n}) = #{count}"
  37. (min, max) = (max, max*10)
  38. }
  39. __END__
  40. a(1) = 0
  41. a(2) = 0
  42. a(3) = 2
  43. a(4) = 8
  44. a(5) = 26
  45. a(6) = 60
  46. a(7) = 135
  47. a(8) = 323
  48. a(9) = 791
  49. a(10) = 1840
  50. a(11) = 4216
  51. a(12) = 9967
  52. a(13) = 23070
  53. a(14) = 54356
  54. a(15) = 129125