2008-10-30 22:04:44 -04:00
|
|
|
! Copyright (c) 2007-2008 Aaron Schaefer.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
|
|
|
USING: arrays kernel math math.functions math.miller-rabin math.matrices
|
|
|
|
math.order math.parser math.primes.factors math.ranges make namespaces
|
|
|
|
sequences sequences.lib sorting unicode.case ;
|
2007-12-18 20:57:16 -05:00
|
|
|
IN: project-euler.common
|
|
|
|
|
2008-01-06 21:18:59 -05:00
|
|
|
! A collection of words used by more than one Project Euler solution
|
|
|
|
! and/or related words that could be useful for future problems.
|
|
|
|
|
|
|
|
! Problems using each public word
|
|
|
|
! -------------------------------
|
2008-02-03 17:18:10 -05:00
|
|
|
! alpha-value - #22, #42
|
2008-02-08 22:29:12 -05:00
|
|
|
! cartesian-product - #4, #27, #29, #32, #33, #43, #44, #56
|
2008-01-06 21:18:59 -05:00
|
|
|
! collect-consecutive - #8, #11
|
|
|
|
! log10 - #25, #134
|
|
|
|
! max-path - #18, #67
|
2008-02-03 18:42:45 -05:00
|
|
|
! nth-triangle - #12, #42
|
2008-02-10 22:11:31 -05:00
|
|
|
! number>digits - #16, #20, #30, #34, #35, #38, #43, #52, #55, #56
|
2008-02-08 22:29:12 -05:00
|
|
|
! palindrome? - #4, #36, #55
|
2008-02-01 14:45:29 -05:00
|
|
|
! pandigital? - #32, #38
|
2008-02-08 19:28:30 -05:00
|
|
|
! pentagonal? - #44, #45
|
2008-01-06 21:18:59 -05:00
|
|
|
! propagate-all - #18, #67
|
|
|
|
! sum-proper-divisors - #21
|
|
|
|
! tau* - #12
|
2008-02-02 17:22:20 -05:00
|
|
|
! [uad]-transform - #39, #75
|
2008-01-06 21:18:59 -05:00
|
|
|
|
2007-12-18 20:57:16 -05:00
|
|
|
|
2007-12-24 04:32:19 -05:00
|
|
|
: nth-pair ( n seq -- nth next )
|
2008-10-30 22:04:44 -04:00
|
|
|
2dup [ 1+ ] dip [ nth ] 2bi@ ;
|
2007-12-24 04:32:19 -05:00
|
|
|
|
2008-01-06 21:18:59 -05:00
|
|
|
: perfect-square? ( n -- ? )
|
|
|
|
dup sqrt mod zero? ;
|
|
|
|
|
2007-12-18 20:57:16 -05:00
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
: count-shifts ( seq width -- n )
|
2008-10-30 22:04:44 -04:00
|
|
|
[ length 1+ ] dip - ;
|
2007-12-18 20:57:16 -05:00
|
|
|
|
2007-12-25 00:13:01 -05:00
|
|
|
: max-children ( seq -- seq )
|
|
|
|
[ dup length 1- [ over nth-pair max , ] each ] { } make nip ;
|
|
|
|
|
2008-01-06 21:18:59 -05:00
|
|
|
! Propagate one row into the upper one
|
|
|
|
: propagate ( bottom top -- newtop )
|
2008-04-26 03:01:43 -04:00
|
|
|
[ over rest rot first2 max rot + ] map nip ;
|
2007-12-18 20:57:16 -05:00
|
|
|
|
2008-01-06 21:18:59 -05:00
|
|
|
: shift-3rd ( seq obj obj -- seq obj obj )
|
2008-04-26 03:01:43 -04:00
|
|
|
rot rest -rot ;
|
2008-01-06 21:18:59 -05:00
|
|
|
|
|
|
|
: (sum-divisors) ( n -- sum )
|
|
|
|
dup sqrt >fixnum [1,b] [
|
|
|
|
[ 2dup mod zero? [ 2dup / + , ] [ drop ] if ] each
|
|
|
|
dup perfect-square? [ sqrt >fixnum neg , ] [ drop ] if
|
|
|
|
] { } make sum ;
|
|
|
|
|
2008-02-02 17:22:20 -05:00
|
|
|
: transform ( triple matrix -- new-triple )
|
|
|
|
[ 1array ] dip m. first ;
|
|
|
|
|
2007-12-18 20:57:16 -05:00
|
|
|
PRIVATE>
|
|
|
|
|
2008-02-03 17:18:10 -05:00
|
|
|
: alpha-value ( str -- n )
|
|
|
|
>lower [ CHAR: a - 1+ ] sigma ;
|
|
|
|
|
2008-01-17 12:25:43 -05:00
|
|
|
: cartesian-product ( seq1 seq2 -- seq1xseq2 )
|
|
|
|
swap [ swap [ 2array ] map-with ] map-with concat ;
|
|
|
|
|
2007-12-24 04:32:19 -05:00
|
|
|
: collect-consecutive ( seq width -- seq )
|
|
|
|
[
|
|
|
|
2dup count-shifts [ 2dup head shift-3rd , ] times
|
|
|
|
] { } make 2nip ;
|
2007-12-18 20:57:16 -05:00
|
|
|
|
2008-01-06 21:18:59 -05:00
|
|
|
: log10 ( m -- n )
|
|
|
|
log 10 log / ;
|
2007-12-18 20:57:16 -05:00
|
|
|
|
2007-12-24 04:32:19 -05:00
|
|
|
: max-path ( triangle -- n )
|
|
|
|
dup length 1 > [
|
2008-03-31 20:18:05 -04:00
|
|
|
2 cut* first2 max-children [ + ] 2map suffix max-path
|
2007-12-24 04:32:19 -05:00
|
|
|
] [
|
|
|
|
first first
|
|
|
|
] if ;
|
|
|
|
|
2007-12-25 00:13:01 -05:00
|
|
|
: number>digits ( n -- seq )
|
2008-07-10 02:00:27 -04:00
|
|
|
[ dup zero? not ] [ 10 /mod ] [ ] produce reverse nip ;
|
2007-12-25 00:13:01 -05:00
|
|
|
|
2008-02-03 18:42:45 -05:00
|
|
|
: nth-triangle ( n -- n )
|
|
|
|
dup 1+ * 2 / ;
|
|
|
|
|
2008-02-08 22:29:12 -05:00
|
|
|
: palindrome? ( n -- ? )
|
|
|
|
number>string dup reverse = ;
|
|
|
|
|
2008-02-01 14:45:29 -05:00
|
|
|
: pandigital? ( n -- ? )
|
|
|
|
number>string natural-sort "123456789" = ;
|
|
|
|
|
2008-02-08 19:28:30 -05:00
|
|
|
: pentagonal? ( n -- ? )
|
|
|
|
dup 0 > [ 24 * 1+ sqrt 1+ 6 / 1 mod zero? ] [ drop f ] if ;
|
|
|
|
|
2008-01-06 21:18:59 -05:00
|
|
|
! Not strictly needed, but it is nice to be able to dump the triangle after the
|
|
|
|
! propagation
|
|
|
|
: propagate-all ( triangle -- newtriangle )
|
2008-04-26 03:01:43 -04:00
|
|
|
reverse [ first dup ] keep rest [ propagate dup ] map nip reverse swap suffix ;
|
2007-12-29 14:09:50 -05:00
|
|
|
|
|
|
|
: sum-divisors ( n -- sum )
|
|
|
|
dup 4 < [ { 0 1 3 4 } nth ] [ (sum-divisors) ] if ;
|
|
|
|
|
|
|
|
: sum-proper-divisors ( n -- sum )
|
|
|
|
dup sum-divisors swap - ;
|
|
|
|
|
2007-12-31 14:47:24 -05:00
|
|
|
: abundant? ( n -- ? )
|
|
|
|
dup sum-proper-divisors < ;
|
|
|
|
|
|
|
|
: deficient? ( n -- ? )
|
|
|
|
dup sum-proper-divisors > ;
|
|
|
|
|
|
|
|
: perfect? ( n -- ? )
|
|
|
|
dup sum-proper-divisors = ;
|
|
|
|
|
2007-12-18 20:57:16 -05:00
|
|
|
! The divisor function, counts the number of divisors
|
2008-01-06 21:18:59 -05:00
|
|
|
: tau ( m -- n )
|
2008-01-14 11:33:08 -05:00
|
|
|
group-factors flip second 1 [ 1+ * ] reduce ;
|
2007-12-18 20:57:16 -05:00
|
|
|
|
|
|
|
! Optimized brute-force, is often faster than prime factorization
|
2008-01-06 21:18:59 -05:00
|
|
|
: tau* ( m -- n )
|
2008-01-14 15:04:21 -05:00
|
|
|
factor-2s [ 1+ ] dip [ perfect-square? -1 0 ? ] keep
|
2007-12-25 00:13:01 -05:00
|
|
|
dup sqrt >fixnum [1,b] [
|
2008-01-14 15:04:21 -05:00
|
|
|
dupd mod zero? [ [ 2 + ] dip ] when
|
2007-12-18 20:57:16 -05:00
|
|
|
] each drop * ;
|
2008-02-02 17:22:20 -05:00
|
|
|
|
|
|
|
! These transforms are for generating primitive Pythagorean triples
|
|
|
|
: u-transform ( triple -- new-triple )
|
|
|
|
{ { 1 2 2 } { -2 -1 -2 } { 2 2 3 } } transform ;
|
|
|
|
: a-transform ( triple -- new-triple )
|
|
|
|
{ { 1 2 2 } { 2 1 2 } { 2 2 3 } } transform ;
|
|
|
|
: d-transform ( triple -- new-triple )
|
|
|
|
{ { -1 -2 -2 } { 2 1 2 } { 2 2 3 } } transform ;
|
|
|
|
|