Merge commit 'elasticdog2/master'
commit
2a093912db
|
@ -1,6 +1,6 @@
|
|||
! Copyright (c) 2007 Aaron Schaefer, Daniel Ehrenberg.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: arrays combinators.lib hashtables kernel math math.parser math.ranges
|
||||
USING: hashtables kernel math math.parser math.ranges project-euler.common
|
||||
sequences sorting ;
|
||||
IN: project-euler.004
|
||||
|
||||
|
@ -21,9 +21,6 @@ IN: project-euler.004
|
|||
: palindrome? ( n -- ? )
|
||||
number>string dup reverse = ;
|
||||
|
||||
: cartesian-product ( seq1 seq2 -- seq1xseq2 )
|
||||
swap [ swap [ 2array ] map-with ] map-with concat ;
|
||||
|
||||
<PRIVATE
|
||||
|
||||
: source-004 ( -- seq )
|
||||
|
|
|
@ -0,0 +1,75 @@
|
|||
! Copyright (c) 2007 Aaron Schaefer.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: kernel math math.primes project-euler.common sequences ;
|
||||
IN: project-euler.027
|
||||
|
||||
! http://projecteuler.net/index.php?section=problems&id=27
|
||||
|
||||
! DESCRIPTION
|
||||
! -----------
|
||||
|
||||
! Euler published the remarkable quadratic formula:
|
||||
|
||||
! n² + n + 41
|
||||
|
||||
! It turns out that the formula will produce 40 primes for the consecutive
|
||||
! values n = 0 to 39. However, when n = 40, 402 + 40 + 41 = 40(40 + 1) + 41 is
|
||||
! divisible by 41, and certainly when n = 41, 41² + 41 + 41 is clearly
|
||||
! divisible by 41.
|
||||
|
||||
! Using computers, the incredible formula n² - 79n + 1601 was discovered, which
|
||||
! produces 80 primes for the consecutive values n = 0 to 79. The product of the
|
||||
! coefficients, -79 and 1601, is -126479.
|
||||
|
||||
! Considering quadratics of the form:
|
||||
|
||||
! n² + an + b, where |a| < 1000 and |b| < 1000
|
||||
|
||||
! where |n| is the modulus/absolute value of n
|
||||
! e.g. |11| = 11 and |-4| = 4
|
||||
|
||||
! Find the product of the coefficients, a and b, for the quadratic expression
|
||||
! that produces the maximum number of primes for consecutive values of n,
|
||||
! starting with n = 0.
|
||||
|
||||
|
||||
! SOLUTION
|
||||
! --------
|
||||
|
||||
! b must be prime since n = 0 must return a prime
|
||||
! a + b + 1 must be prime since n = 1 must return a prime
|
||||
! 1 - a + b must be prime as well, hence >= 2. Therefore:
|
||||
! 1 - a + b >= 2
|
||||
! b - a >= 1
|
||||
! a < b
|
||||
|
||||
<PRIVATE
|
||||
|
||||
: source-027 ( -- seq )
|
||||
1000 [ prime? ] subset [ dup [ neg ] map append ] keep
|
||||
cartesian-product [ first2 < ] subset ;
|
||||
|
||||
: quadratic ( b a n -- m )
|
||||
dup sq -rot * + + ;
|
||||
|
||||
: (consecutive-primes) ( b a n -- m )
|
||||
3dup quadratic prime? [ 1+ (consecutive-primes) ] [ 2nip ] if ;
|
||||
|
||||
: consecutive-primes ( a b -- m )
|
||||
swap 0 (consecutive-primes) ;
|
||||
|
||||
: max-consecutive ( seq -- elt n )
|
||||
dup [ first2 consecutive-primes ] map dup supremum
|
||||
over index [ swap nth ] curry 2apply ;
|
||||
|
||||
PRIVATE>
|
||||
|
||||
: euler027 ( -- answer )
|
||||
source-027 max-consecutive drop product ;
|
||||
|
||||
! [ euler027 ] 100 ave-time
|
||||
! 687 ms run / 23 ms GC ave time - 100 trials
|
||||
|
||||
! TODO: generalize max-consecutive/max-product (from #26) into a new word
|
||||
|
||||
MAIN: euler027
|
|
@ -0,0 +1,46 @@
|
|||
! Copyright (c) 2007 Aaron Schaefer.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: combinators.lib kernel math math.ranges ;
|
||||
IN: project-euler.028
|
||||
|
||||
! http://projecteuler.net/index.php?section=problems&id=28
|
||||
|
||||
! DESCRIPTION
|
||||
! -----------
|
||||
|
||||
! Starting with the number 1 and moving to the right in a clockwise direction a
|
||||
! 5 by 5 spiral is formed as follows:
|
||||
|
||||
! 21 22 23 24 25
|
||||
! 20 7 8 9 10
|
||||
! 19 6 1 2 11
|
||||
! 18 5 4 3 12
|
||||
! 17 16 15 14 13
|
||||
|
||||
! It can be verified that the sum of both diagonals is 101.
|
||||
|
||||
! What is the sum of both diagonals in a 1001 by 1001 spiral formed in the same way?
|
||||
|
||||
|
||||
! SOLUTION
|
||||
! --------
|
||||
|
||||
! For a square sized n by n, the sum of corners is 4n² - 6n + 6
|
||||
|
||||
<PRIVATE
|
||||
|
||||
: sum-corners ( n -- sum )
|
||||
dup 1 = [ [ sq 4 * ] keep 6 * - 6 + ] unless ;
|
||||
|
||||
: sum-diags ( n -- sum )
|
||||
1 swap 2 <range> [ sum-corners ] sigma ;
|
||||
|
||||
PRIVATE>
|
||||
|
||||
: euler028 ( -- answer )
|
||||
1001 sum-diags ;
|
||||
|
||||
! [ euler028 ] 100 ave-time
|
||||
! 0 ms run / 0 ms GC ave time - 100 trials
|
||||
|
||||
MAIN: euler028
|
|
@ -0,0 +1,37 @@
|
|||
! Copyright (c) 2007 Aaron Schaefer.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: hashtables kernel math.functions math.ranges project-euler.common
|
||||
sequences ;
|
||||
IN: project-euler.029
|
||||
|
||||
! http://projecteuler.net/index.php?section=problems&id=29
|
||||
|
||||
! DESCRIPTION
|
||||
! -----------
|
||||
|
||||
! Consider all integer combinations of a^b for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5:
|
||||
|
||||
! 2^2 = 4, 2^3 = 8, 2^4 = 16, 2^5 = 32
|
||||
! 3^2 = 9, 3^3 = 27, 3^4 = 81, 3^5 = 243
|
||||
! 4^2 = 16, 4^3 = 64, 4^4 = 256, 4^5 = 1024
|
||||
! 5^2 = 25, 5^3 = 125, 5^4 = 625, 5^5 = 3125
|
||||
|
||||
! If they are then placed in numerical order, with any repeats removed, we get
|
||||
! the following sequence of 15 distinct terms:
|
||||
|
||||
! 4, 8, 9, 16, 25, 27, 32, 64, 81, 125, 243, 256, 625, 1024, 3125
|
||||
|
||||
! How many distinct terms are in the sequence generated by a^b for 2 ≤ a ≤ 100
|
||||
! and 2 ≤ b ≤ 100?
|
||||
|
||||
|
||||
! SOLUTION
|
||||
! --------
|
||||
|
||||
: euler029 ( -- answer )
|
||||
2 100 [a,b] dup cartesian-product [ first2 ^ ] map prune length ;
|
||||
|
||||
! [ euler029 ] 100 ave-time
|
||||
! 951 ms run / 12 ms GC ave time - 100 trials
|
||||
|
||||
MAIN: euler029
|
|
@ -1,5 +1,5 @@
|
|||
USING: kernel math math.functions math.miller-rabin math.parser
|
||||
math.primes.factors math.ranges namespaces sequences ;
|
||||
USING: arrays combinators.lib kernel math math.functions math.miller-rabin
|
||||
math.parser math.primes.factors math.ranges namespaces sequences ;
|
||||
IN: project-euler.common
|
||||
|
||||
! A collection of words used by more than one Project Euler solution
|
||||
|
@ -7,6 +7,7 @@ IN: project-euler.common
|
|||
|
||||
! Problems using each public word
|
||||
! -------------------------------
|
||||
! cartesian-product - #4, #27
|
||||
! collect-consecutive - #8, #11
|
||||
! log10 - #25, #134
|
||||
! max-path - #18, #67
|
||||
|
@ -45,6 +46,9 @@ IN: project-euler.common
|
|||
|
||||
PRIVATE>
|
||||
|
||||
: cartesian-product ( seq1 seq2 -- seq1xseq2 )
|
||||
swap [ swap [ 2array ] map-with ] map-with concat ;
|
||||
|
||||
: collect-consecutive ( seq width -- seq )
|
||||
[
|
||||
2dup count-shifts [ 2dup head shift-3rd , ] times
|
||||
|
|
|
@ -8,8 +8,9 @@ USING: definitions io io.files kernel math.parser sequences vocabs
|
|||
project-euler.013 project-euler.014 project-euler.015 project-euler.016
|
||||
project-euler.017 project-euler.018 project-euler.019 project-euler.020
|
||||
project-euler.021 project-euler.022 project-euler.023 project-euler.024
|
||||
project-euler.025 project-euler.026 project-euler.067 project-euler.134
|
||||
project-euler.169 project-euler.173 project-euler.175 ;
|
||||
project-euler.025 project-euler.026 project-euler.027 project-euler.028
|
||||
project-euler.029 project-euler.067 project-euler.134 project-euler.169
|
||||
project-euler.173 project-euler.175 ;
|
||||
IN: project-euler
|
||||
|
||||
<PRIVATE
|
||||
|
|
Loading…
Reference in New Issue