versioninfo()
Julia Version 1.7.0-DEV.1129 Commit 9117b4d6d6 (2021-05-20 16:42 UTC) Platform Info: OS: Windows (x86_64-w64-mingw32) CPU: Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz WORD_SIZE: 64 LIBM: libopenlibm LLVM: libLLVM-11.0.1 (ORCJIT, skylake) Environment: JULIA_NUM_THREADS = 12 JULIA_PYTHONCALL_EXE = C:\Users\genkuroki\.julia\conda\3\python.exe
module A
function is_prime(num)
if num <= 1
return false
end
div = (num % i == 0 for i = 2:num-1)
return !(true in div)
end
function num_primes(maxnum)
primes = [n for n = 1:maxnum if is_prime(n)]
return length(primes)
end
end
Main.A
https://smooth-pudding.hatenablog.com/entry/2021/05/30/180849
Rust version:
fn main() {
println!("{}", num_primes(&100000));
}
fn is_prime(num: &u32) -> bool {
if num <= &1 {return false};
for i in 2..*num {
if num % i == 0 {
return false;
}
}
return true;
}
fn num_primes(maxnum: &u32) -> u32 {
let mut count = 0;
for i in 1..=*maxnum {
if is_prime(&i) {count += 1;}
};
return count;
}
module B
function is_prime(num)
if num ≤ 1 return false end
for i in oftype(num, 2):num-one(num)
if iszero(num % i)
return false
end
end
return true
end
num_primes(maxnum) = count(is_prime, one(maxnum):maxnum)
end
Main.B
module C
rem_int(x::T, y::T) where T<:Signed = Base.srem_int(x, y)
rem_int(x::T, y::T) where T<:Unsigned = Base.urem_int(x, y)
function is_prime(num)
if num ≤ 1 return false end
for i in oftype(num, 2):num-one(num)
if iszero(rem_int(num, i))
return false
end
end
return true
end
num_primes(maxnum) = count(is_prime, one(maxnum):maxnum)
end
Main.C
using BenchmarkHistograms
@benchmark A.num_primes(100000) seconds=30
samples: 28; evals/sample: 1; memory estimate: 256.72 KiB; allocs estimate: 15 ns (1.086e9 - 1.091e9] ████████████████████████ 8 (1.091e9 - 1.095e9] ████████████ 4 (1.095e9 - 1.1e9 ] ███ 1 (1.1e9 - 1.105e9] ██████ 2 (1.105e9 - 1.109e9] ██████████████████████████████ 10 (1.109e9 - 1.114e9] █████████ 3 Counts min: 1.086 s (0.00% GC); mean: 1.100 s (0.00% GC); median: 1.103 s (0.00% GC); max: 1.114 s (0.00% GC).
@benchmark B.num_primes(100000) seconds=30
samples: 29; evals/sample: 1; memory estimate: 0 bytes; allocs estimate: 0 ns (1.043e9 - 1.047e9] ███████▌2 (1.047e9 - 1.052e9] ██████████████████████████▎7 (1.052e9 - 1.057e9] ██████████████████████████████ 8 (1.057e9 - 1.062e9] ██████████████████████▌6 (1.062e9 - 1.066e9] ███████████████ 4 (1.066e9 - 1.071e9] ███████▌2 Counts min: 1.043 s (0.00% GC); mean: 1.056 s (0.00% GC); median: 1.056 s (0.00% GC); max: 1.071 s (0.00% GC).
@benchmark C.num_primes(100000) seconds=30
samples: 35; evals/sample: 1; memory estimate: 0 bytes; allocs estimate: 0 ns (8.66e8 - 8.71e8] ██████████████████████████▎7 (8.71e8 - 8.76e8] ██████████████████▊5 (8.76e8 - 8.8e8 ] ██████████████████████████████ 8 (8.8e8 - 8.85e8] ██████████████████████████▎7 (8.85e8 - 8.89e8] ███████████████ 4 (8.89e8 - 8.94e8] ███████████▎3 (8.94e8 - 8.99e8] ███▊1 Counts min: 866.414 ms (0.00% GC); mean: 878.994 ms (0.00% GC); median: 878.092 ms (0.00% GC); max: 898.586 ms (0.00% GC).
1.086/0.866
1.2540415704387993
@time A.num_primes(10^6)
91.498245 seconds (18 allocations: 2.001 MiB)
78498
@time B.num_primes(10^6)
82.322474 seconds
78498
@time C.num_primes(10^6)
69.557197 seconds
78498
91.5/69.6
1.3146551724137931