-
Notifications
You must be signed in to change notification settings - Fork 0
/
almost_primes_in_range.jl
54 lines (36 loc) · 994 Bytes
/
almost_primes_in_range.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/julia
# Generate k-almost primes in range [A,B].
# See also:
# https://en.wikipedia.org/wiki/Almost_prime
using Primes
const BIG = false # true to use big integers
function almost_primes_in_range(A, B, n::Int64)
A = max(A, (BIG ? big"2" : 2)^n)
F = function(m, lo::Int64, j::Int64)
lst = []
hi = round(Int64, fld(B, m)^(1/j))
if (lo > hi)
return lst
end
if (j == 1)
lo = round(Int64, max(lo, cld(A, m)))
if (lo > hi)
return lst
end
for q in (primes(lo, hi))
push!(lst, m*q)
end
else
for q in (primes(lo, hi))
lst = vcat(lst, F(m*q, q, j-1))
end
end
return lst
end
return sort(F((BIG ? big"1" : 1), 2, n))
end
# Generate 5-almost in the range [300, 1000]
k = 5
from = 300
upto = 1000
println(almost_primes_in_range(from, upto, k))