September 2013
Intermediate to advanced
548 pages
12h 25m
English
Easy. Here’s the code:
| lib_primes.erl | |
| Line 1 | -module(lib_primes). |
| - | -export([make_prime/1, is_prime/1, make_random_int/1]). |
| - | |
| - | make_prime(1) -> |
| 5 | lists:nth(random:uniform(4), [2,3,5,7]); |
| - | make_prime(K) when K > 0 -> |
| - | new_seed(), |
| - | N = make_random_int(K), |
| - | if N > 3 -> |
| 10 | io:format("Generating a ~w digit prime ",[K]), |
| - | MaxTries = N - 3, |
| - | P1 = make_prime(MaxTries, N+1), |
| - | io:format("~n",[]), |
| - | P1; |
| 15 | true -> |
| - | make_prime(K) |
| - | end. |
| - | |
| - | make_prime(0, _) -> |
| 20 | exit(impossible); |
| - | make_prime(K, P) -> |
| - | io:format(".",[]), |
| - | case is_prime(P) of |
| - | true -> P; |
| 25 | false -> make_prime(K-1, P+1) |
| - | end. |
| - | |
| - | is_prime(D) when D < 10 -> |
| - | lists:member(D, [2,3,5,7]); |
| 30 | is_prime(D) ... |
Read now
Unlock full access