-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_1.2.6.exs
60 lines (49 loc) · 1.53 KB
/
example_1.2.6.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
defmodule PrimeChecker do
def is_prime?(n, :divisors) do
n == smallest(n)
end
def is_prime?(n, _) do
a = random(n)
expmod(a, n, n) == a
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)
defp expmod(_, 0, _), do: 1
defp expmod(base, exp, m) when rem(exp, 2) == 0 do
rem(square(expmod(base, div(exp, 2), m)), m)
end
defp expmod(base, exp, m) do
rem(base * expmod(base, exp - 1, m), m)
end
defp square(n), do: n * n
defp random(n) do
:random.seed(:os.timestamp)
:random.uniform * (n - 1) |> round
end
end
ExUnit.start
defmodule PrimeCheckerTests do
use ExUnit.Case, async: true
test "detects prime numbers using divisors" do
assert PrimeChecker.is_prime? 5, :divisors
assert PrimeChecker.is_prime? 17, :divisors
assert PrimeChecker.is_prime? 71, :divisors
end
test "detects non-prime numbers using divisors" do
refute PrimeChecker.is_prime? 6, :divisors
refute PrimeChecker.is_prime? 100, :divisors
refute PrimeChecker.is_prime? 65536, :divisors
end
test "detects prime numbers using fermat" do
assert PrimeChecker.is_prime? 5, :fermat
assert PrimeChecker.is_prime? 17, :fermat
assert PrimeChecker.is_prime? 71, :fermat
end
test "detects non-prime numbers using fermat" do
refute PrimeChecker.is_prime? 6, :fermat
refute PrimeChecker.is_prime? 100, :fermat
refute PrimeChecker.is_prime? 65536, :fermat
end
end