project-euler.087: adding description and SOLUTION:.

clean-macosx-x86-64
John Benediktsson 2019-11-06 19:56:56 -08:00
parent 6492f1c9cb
commit fae208f67f
1 changed files with 40 additions and 14 deletions

View File

@ -1,29 +1,53 @@
USING: locals math math.primes sequences math.functions sets kernel ; USING: locals math math.functions math.primes
project-euler.common sequences sets ;
IN: project-euler.087 IN: project-euler.087
<PRIVATE ! https://projecteuler.net/index.php?section=problems&id=87
: remove-duplicates ( seq -- seq' ) ! DESCRIPTION
dup intersect ; ! -----------
! The smallest number expressible as the sum of a prime square,
! prime cube, and prime fourth power is 28. In fact, there are
! exactly four numbers below fifty that can be expressed in such
! a way:
! 28 = 2^2 + 2^3 + 2^4
! 33 = 3^2 + 2^3 + 2^4
! 49 = 5^2 + 2^3 + 2^4
! 47 = 2^2 + 3^3 + 2^4
! How many numbers below fifty million can be expressed as the
! sum of a prime square, prime cube, and prime fourth power?
<PRIVATE
:: prime-powers-less-than ( primes pow n -- prime-powers ) :: prime-powers-less-than ( primes pow n -- prime-powers )
primes [ pow ^ ] map [ n <= ] filter ; primes [ pow ^ ] map [ n <= ] filter ;
! You may think to make a set of all possible sums of a prime square and cube ! You may think to make a set of all possible sums of a prime
! and then subtract prime fourths from numbers ranging from 1 to 'n' to find ! square and cube and then subtract prime fourths from numbers
! this. As n grows large, this is actually more inefficient! ! ranging from 1 to 'n' to find this. As n grows large, this is
! actually more inefficient!
!
! Prime numbers grow ~ n / log n ! Prime numbers grow ~ n / log n
!
! Thus there are (n / log n)^(1/2) prime squares <= n, ! Thus there are (n / log n)^(1/2) prime squares <= n,
! (n / log n)^(1/3) prime cubes <= n, ! (n / log n)^(1/3) prime cubes <= n,
! and (n / log n)^(1/4) prime fourths <= n. ! and (n / log n)^(1/4) prime fourths <= n.
! If we compute the cartesian product of these, this takes !
! If we compute the cartesian product of these, this takes
! O((n / log n)^(13/12)). ! O((n / log n)^(13/12)).
! If we instead precompute sums of squares and cubes, and iterate up to n, !
! checking each fourth power against it, this takes ! If we instead precompute sums of squares and cubes, and
! iterate up to n, checking each fourth power against it, this
! takes:
!
! O(n * (n / log n)^(1/4)) = O(n^(5/4)/(log n)^(1/4)) >> O((n / log n)^(13/12)) ! O(n * (n / log n)^(1/4)) = O(n^(5/4)/(log n)^(1/4)) >> O((n / log n)^(13/12))
! !
! When n = 50000000, the first equation is approximately 10 million and ! When n = 50,000,000, the first equation is approximately 10
! the second is approximately 2 billion. ! million and the second is approximately 2 billion.
:: prime-triples ( n -- answer ) :: prime-triples ( n -- answer )
n sqrt primes-upto :> primes n sqrt primes-upto :> primes
@ -32,9 +56,11 @@ IN: project-euler.087
primes 4 n prime-powers-less-than :> primes^4 primes 4 n prime-powers-less-than :> primes^4
primes^2 primes^3 [ + ] cartesian-map concat primes^2 primes^3 [ + ] cartesian-map concat
primes^4 [ + ] cartesian-map concat primes^4 [ + ] cartesian-map concat
[ n <= ] filter remove-duplicates length ; [ n <= ] filter members length ;
PRIVATE> PRIVATE>
:: euler087 ( -- answer ) :: euler087 ( -- answer )
50000000 prime-triples ; 50,000,000 prime-triples ;
SOLUTION: euler087