Skip to content

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

42 lines (29 sloc) 0.914 kb
%% 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/
Jump to Line
Something went wrong with that request. Please try again.