omega_primes_in_range.jl 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/julia
  2. # Generate k-omega primes in range [A,B].
  3. # Definition:
  4. # k-omega primes are numbers n such that omega(n) = k.
  5. # See also:
  6. # https://en.wikipedia.org/wiki/Almost_prime
  7. # https://en.wikipedia.org/wiki/Prime_omega_function
  8. using Primes
  9. const BIG = false # true to use big integers
  10. function big_prod(arr)
  11. BIG || return prod(arr)
  12. r = big"1"
  13. for n in (arr)
  14. r *= n
  15. end
  16. return r
  17. end
  18. function omega_primes_in_range(A, B, n::Int64)
  19. A = max(A, big_prod(primes(prime(n))))
  20. F = function(m, lo::Int64, j::Int64)
  21. lst = []
  22. hi = round(Int64, fld(B, m)^(1/j))
  23. if (lo > hi)
  24. return lst
  25. end
  26. for q in (primes(lo, hi))
  27. v = m*q
  28. while (v <= B)
  29. if (j == 1)
  30. if (v >= A)
  31. push!(lst, v)
  32. end
  33. elseif (v*(q+1) <= B)
  34. lst = vcat(lst, F(v, q+1, j-1))
  35. end
  36. v *= q
  37. end
  38. end
  39. return lst
  40. end
  41. return sort(F((BIG ? big"1" : 1), 2, n))
  42. end
  43. # Generate 5-omega primes in the range [3000, 10000]
  44. k = 5
  45. from = 3000
  46. upto = 10000
  47. println(omega_primes_in_range(from, upto, k))