Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 42 lines (29 sloc) 0.914 kb
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
%% Problem
%% ---------------------
%% By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13,
%% we can see that the 6th prime is 13.
%%
%% What is the 10001st prime number?
%% ---------------------

-module(p007).
-export([solve/0]).
-include_lib("eunit/include/eunit.hrl").


solve() -> find_prime(10001).

%% Solution
%% ---------------------
%% Inspired by:
%% http://basildoncoder.com/blog/2008/10/26/project-euler-problem-7/
%% ---------------------

find_prime(N) ->
    lists:nth(N, sieve_with_atleast_n_primes(N)).

sieve_with_atleast_n_primes(N) ->
    mymath:primes_upto(upper_bound_estimate(N)).

upper_bound_estimate(N) ->
    trunc(N * math:log(N) + N * math:log(math:log(N))).


%% Tests

find_prime_10_test() ->
    ?assertEqual(29, find_prime(10)).

upper_bound_estimate_test() ->
    ?assertEqual(114319, upper_bound_estimate(10001)).


%% See also:
%% http://primes.utm.edu/lists/small/
Something went wrong with that request. Please try again.