2010-02-18 21:46:18 -05:00
|
|
|
! Copyright (c) 2007-2010 Aaron Schaefer.
|
2010-02-20 10:15:05 -05:00
|
|
|
! The contents of this file are licensed under the Simplified BSD License
|
|
|
|
! A copy of the license is available at http://factorcode.org/license.txt
|
|
|
|
USING: accessors arrays byte-arrays fry hints kernel lists make math
|
|
|
|
math.functions math.matrices math.order math.parser math.primes.factors
|
|
|
|
math.primes.lists math.primes.miller-rabin math.ranges math.ratios
|
2014-11-17 21:12:41 -05:00
|
|
|
math.vectors namespaces parser prettyprint quotations sequences sorting
|
2016-03-31 02:29:48 -04:00
|
|
|
strings unicode vocabs vocabs.parser words ;
|
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
|
|
|
! log10 - #25, #134
|
|
|
|
! max-path - #18, #67
|
2008-11-10 12:51:43 -05:00
|
|
|
! mediant - #71, #73
|
2009-04-08 02:41:02 -04:00
|
|
|
! nth-prime - #7, #69
|
2008-02-03 18:42:45 -05:00
|
|
|
! nth-triangle - #12, #42
|
2010-02-18 21:46:18 -05:00
|
|
|
! number>digits - #16, #20, #30, #34, #35, #38, #43, #52, #55, #56, #92, #206
|
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
|
2009-04-08 02:41:02 -04:00
|
|
|
! penultimate - #69, #71
|
2008-01-06 21:18:59 -05:00
|
|
|
! propagate-all - #18, #67
|
2010-02-20 10:15:05 -05:00
|
|
|
! permutations? - #49, #70
|
2008-01-06 21:18:59 -05:00
|
|
|
! 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
|
|
|
|
2008-11-03 20:14:37 -05:00
|
|
|
: nth-pair ( seq n -- nth next )
|
|
|
|
tail-slice first2 ;
|
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
|
|
|
|
|
2010-02-20 10:15:05 -05:00
|
|
|
: count-digits ( n -- byte-array )
|
|
|
|
10 <byte-array> [
|
|
|
|
'[ 10 /mod _ [ 1 + ] change-nth dup 0 > ] loop drop
|
|
|
|
] keep ;
|
|
|
|
|
|
|
|
HINTS: count-digits fixnum ;
|
|
|
|
|
2007-12-25 00:13:01 -05:00
|
|
|
: max-children ( seq -- seq )
|
2017-06-01 17:59:35 -04:00
|
|
|
[ dup length 1 - <iota> [ nth-pair max , ] with each ] { } make ;
|
2007-12-25 00:13:01 -05:00
|
|
|
|
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
|
|
|
: (sum-divisors) ( n -- sum )
|
2008-12-17 21:04:24 -05:00
|
|
|
dup sqrt >integer [1,b] [
|
2009-04-01 21:53:18 -04:00
|
|
|
[ 2dup divisor? [ 2dup / + , ] [ drop ] if ] each
|
2008-01-06 21:18:59 -05:00
|
|
|
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 )
|
2009-10-29 15:34:04 -04:00
|
|
|
>lower [ CHAR: a - 1 + ] map-sum ;
|
2008-02-03 17:18:10 -05:00
|
|
|
|
2008-11-10 12:51:43 -05:00
|
|
|
: mediant ( a/c b/d -- (a+b)/(c+d) )
|
|
|
|
2>fraction [ + ] 2bi@ / ;
|
|
|
|
|
2007-12-24 04:32:19 -05:00
|
|
|
: max-path ( triangle -- n )
|
|
|
|
dup length 1 > [
|
2014-11-17 21:12:41 -05:00
|
|
|
2 cut* first2 max-children v+ 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 )
|
2012-04-24 21:42:34 -04:00
|
|
|
[ dup 0 = not ] [ 10 /mod ] produce reverse! nip ;
|
2007-12-25 00:13:01 -05:00
|
|
|
|
2015-07-19 21:05:29 -04:00
|
|
|
: digits>number ( seq -- n )
|
|
|
|
0 [ [ 10 * ] [ + ] bi* ] reduce ;
|
|
|
|
|
2009-01-07 18:47:32 -05:00
|
|
|
: number-length ( n -- m )
|
2009-09-14 14:47:37 -04:00
|
|
|
abs [
|
|
|
|
1
|
|
|
|
] [
|
|
|
|
1 0 [ 2over >= ]
|
|
|
|
[ [ 10 * ] [ 1 + ] bi* ] while 2nip
|
|
|
|
] if-zero ;
|
2009-01-07 18:47:32 -05:00
|
|
|
|
2010-02-20 12:20:21 -05:00
|
|
|
: nth-place ( x n -- y )
|
|
|
|
10^ [ * round >integer ] keep /f ;
|
|
|
|
|
2009-04-08 02:41:02 -04:00
|
|
|
: nth-prime ( n -- n )
|
2009-08-13 20:21:44 -04:00
|
|
|
1 - lprimes lnth ;
|
2009-04-08 02:41:02 -04:00
|
|
|
|
2008-02-03 18:42:45 -05:00
|
|
|
: nth-triangle ( n -- n )
|
2009-08-13 20:21:44 -04:00
|
|
|
dup 1 + * 2 / ;
|
2008-02-03 18:42:45 -05:00
|
|
|
|
2008-02-08 22:29:12 -05:00
|
|
|
: palindrome? ( n -- ? )
|
|
|
|
number>string dup reverse = ;
|
|
|
|
|
2008-02-01 14:45:29 -05:00
|
|
|
: pandigital? ( n -- ? )
|
2008-10-31 01:35:37 -04:00
|
|
|
number>string natural-sort >string "123456789" = ;
|
2008-02-01 14:45:29 -05:00
|
|
|
|
2008-02-08 19:28:30 -05:00
|
|
|
: pentagonal? ( n -- ? )
|
2009-09-11 21:59:54 -04:00
|
|
|
dup 0 > [ 24 * 1 + sqrt 1 + 6 / 1 mod zero? ] [ drop f ] if ; inline
|
2008-02-08 19:28:30 -05:00
|
|
|
|
2009-04-08 02:41:02 -04:00
|
|
|
: penultimate ( seq -- elt )
|
|
|
|
dup length 2 - swap nth ;
|
|
|
|
|
2014-11-30 22:26:23 -05:00
|
|
|
! Not strictly needed, but it is nice to be able to dump the
|
|
|
|
! triangle after the propagation
|
2008-11-03 20:14:37 -05:00
|
|
|
: propagate-all ( triangle -- new-triangle )
|
2014-11-30 22:26:23 -05:00
|
|
|
reverse unclip dup rot
|
|
|
|
[ propagate dup ] map nip
|
|
|
|
reverse swap suffix ;
|
2007-12-29 14:09:50 -05:00
|
|
|
|
2010-02-20 10:15:05 -05:00
|
|
|
: permutations? ( n m -- ? )
|
2012-07-21 13:22:44 -04:00
|
|
|
[ count-digits ] same? ;
|
2010-02-20 10:15:05 -05:00
|
|
|
|
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 )
|
2014-11-30 22:26:23 -05:00
|
|
|
[ sum-divisors ] keep - ;
|
2007-12-29 14:09:50 -05:00
|
|
|
|
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 )
|
2009-08-13 20:21:44 -04: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 )
|
2009-08-13 20:21:44 -04:00
|
|
|
factor-2s dup [ 1 + ]
|
2008-11-03 20:14:37 -05:00
|
|
|
[ perfect-square? -1 0 ? ]
|
|
|
|
[ dup sqrt >fixnum [1,b] ] tri* [
|
2009-04-01 21:53:18 -04:00
|
|
|
dupd divisor? [ [ 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 ;
|
|
|
|
|
2009-03-21 02:27:50 -04:00
|
|
|
SYNTAX: SOLUTION:
|
2009-03-19 00:05:32 -04:00
|
|
|
scan-word
|
2015-06-08 15:38:38 -04:00
|
|
|
[ name>> "-main" append create-word-in ] keep
|
2010-05-05 16:52:54 -04:00
|
|
|
[ drop current-vocab main<< ]
|
2011-10-18 16:18:42 -04:00
|
|
|
[ [ . ] swap prefix ( -- ) define-declared ]
|
2009-03-21 02:27:50 -04:00
|
|
|
2bi ;
|