Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 42 lines (29 sloc) 0.914 kb
6241c3e Solved firest 10 problems
Andrey Paramonov authored
1 %% Problem
2 %% ---------------------
3 %% By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13,
4 %% we can see that the 6th prime is 13.
5 %%
6 %% What is the 10001st prime number?
7 %% ---------------------
8
9 -module(p007).
8b7a0ff Introduced solve function to all modules
Andrey Paramonov authored
10 -export([solve/0]).
6241c3e Solved firest 10 problems
Andrey Paramonov authored
11 -include_lib("eunit/include/eunit.hrl").
12
8b7a0ff Introduced solve function to all modules
Andrey Paramonov authored
13
14 solve() -> find_prime(10001).
15
6241c3e Solved firest 10 problems
Andrey Paramonov authored
16 %% Solution
17 %% ---------------------
18 %% Inspired by:
19 %% http://basildoncoder.com/blog/2008/10/26/project-euler-problem-7/
20 %% ---------------------
21
22 find_prime(N) ->
23 lists:nth(N, sieve_with_atleast_n_primes(N)).
24
25 sieve_with_atleast_n_primes(N) ->
26 mymath:primes_upto(upper_bound_estimate(N)).
27
28 upper_bound_estimate(N) ->
29 trunc(N * math:log(N) + N * math:log(math:log(N))).
30
31
32 %% Tests
33
34 find_prime_10_test() ->
35 ?assertEqual(29, find_prime(10)).
36
37 upper_bound_estimate_test() ->
38 ?assertEqual(114319, upper_bound_estimate(10001)).
39
40
41 %% See also:
42 %% http://primes.utm.edu/lists/small/
Something went wrong with that request. Please try again.