squarefree_lucas_U_pseudoprimes_in_range.sf 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/ruby
  2. # Daniel "Trizen" Șuteu
  3. # Date: 06 September 2022
  4. # https://github.com/trizen
  5. # Generate all the squarefree Lucas pseudoprimes to the U_n(P,Q) sequence 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://en.wikipedia.org/wiki/Lucas_sequence
  9. # https://trizenx.blogspot.com/2020/08/pseudoprimes-construction-methods-and.html
  10. func lucas_znorder(P,Q,D,n) {
  11. n - kronecker(D, n) -> divisors.first_by {|d| lucasUmod(P, Q, d, n) == 0 }
  12. }
  13. func squarefree_lucas_U_pseudoprimes_in_range(a, b, k, P, Q, callback) {
  14. a = max(k.pn_primorial, a)
  15. var D = (P*P - 4*Q)
  16. func (m, L, lo, k) {
  17. var hi = idiv(b,m).iroot(k)
  18. return nil if (lo > hi)
  19. if (k == 1) {
  20. lo = max(lo, idiv_ceil(a, m))
  21. lo > hi && return nil
  22. for j in (1, -1) {
  23. var t = mulmod(m.invmod(L), j, L)
  24. t > hi && next
  25. t += L*idiv_ceil(lo - t, L) if (t < lo)
  26. t > hi && next
  27. for p in (range(t, hi, L)) {
  28. p.is_prime || next
  29. with (m*p) {|n|
  30. with (n - kronecker(D, n)) {|w|
  31. if ((L `divides` w) && (lucas_znorder(P, Q, D, p) `divides` w)) {
  32. callback(n)
  33. }
  34. }
  35. }
  36. }
  37. }
  38. return nil
  39. }
  40. each_prime(lo, hi, {|p|
  41. p.divides(D) && next
  42. var z = lucas_znorder(P, Q, D, p)
  43. m.is_coprime(z) || next
  44. __FUNC__(m*p, lcm(L, z), p+1, k-1)
  45. })
  46. }(1, 1, 2, k)
  47. return callback
  48. }
  49. # Generate all the squarefree Fibonacci pseudoprimes in the range [1, 15251]
  50. var from = 1
  51. var upto = 15251
  52. var P = 1
  53. var Q = -1
  54. var arr = []
  55. for k in (2..100) {
  56. break if k.pn_primorial>upto
  57. squarefree_lucas_U_pseudoprimes_in_range(from, upto, k, P, Q, { arr << _ })
  58. }
  59. say arr.sort
  60. __END__
  61. [323, 377, 1891, 3827, 4181, 5777, 6601, 6721, 8149, 10877, 11663, 13201, 13981, 15251]