-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise_1.22.exs
64 lines (57 loc) · 1.31 KB
/
exercise_1.22.exs
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
55
56
57
58
59
60
61
62
63
defmodule PrimeTime do
def search_for(0, _), do: :ok
def search_for(n_primes, minimum) when rem(minimum, 2) == 0, do: search_for(n_primes, minimum + 1)
def search_for(n_primes, minimum) do
result = timed_prime_test(minimum)
if result do
n_primes = n_primes - 1
end
search_for(n_primes, minimum + 2)
end
def timed_prime_test(n) do
IO.puts n
{ elapsed, result } = :timer.tc(fn -> is_prime?(n) end)
if result do
IO.puts " *** "
IO.puts "Checking took #{elapsed} milliseconds"
end
result
end
def is_prime?(n) do
n == smallest(n)
end
def smallest(n), do: find(n, 2)
defp find(n, test) when test * test > n, do: n
defp find(n, test) when rem(n, test) == 0, do: test
defp find(n, test), do: find(n, test + 1)
end
PrimeTime.search_for(3, 1_000)
IO.puts " ======== "
PrimeTime.search_for(3, 10_000)
IO.puts " ======== "
PrimeTime.search_for(3, 100_000)
IO.puts " ======== "
PrimeTime.search_for(3, 1_000_000)
IO.puts " ======== "
# Results:
# 1009 -> 2 ms
# 1013 -> 2 ms
# 1019 -> 2 ms
#
# 10007 -> 5 ms
# 10009 -> 5 ms
# 10037 -> 5 ms
#
# 10003 -> 15 ms
# 10019 -> 26 ms
# 10043 -> 16 ms
#
# 100003 -> 47 ms
# 100033 -> 46 ms
# 100037 -> 46 ms
# Growth:
# 1000 -> 10000 = 2.5x
# 10000 -> 100000 = 3.8x
# 100000 -> 1000000 = 2.4x
#
# sqrt(10) =~ 3.16