1 ! Copyright (c) 2007-2010 Aaron Schaefer.
2 ! The contents of this file are licensed under the Simplified BSD License
3 ! A copy of the license is available at http://factorcode.org/license.txt
4 USING: accessors arrays byte-arrays hints kernel lists make
5 math math.functions math.matrices math.order math.parser
6 math.primes.factors math.primes.lists ranges math.ratios
7 math.vectors parser prettyprint sequences sorting strings
8 unicode vocabs.parser words ;
9 IN: project-euler.common
11 ! A collection of words used by more than one Project Euler solution
12 ! and/or related words that could be useful for future problems.
14 ! Problems using each public word
15 ! -------------------------------
16 ! alpha-value - #22, #42
17 ! cartesian-product - #4, #27, #29, #32, #33, #43, #44, #56
22 ! nth-triangle - #12, #42
23 ! number>digits - #16, #20, #30, #34, #35, #38, #43, #52, #55, #56, #92, #206
24 ! palindrome? - #4, #36, #55
25 ! pandigital? - #32, #38
26 ! pentagonal? - #44, #45
27 ! penultimate - #69, #71
28 ! propagate-all - #18, #67
29 ! permutations? - #49, #70
30 ! sum-proper-divisors - #21
32 ! [uad]-transform - #39, #75
35 : nth-pair ( seq n -- nth next )
38 : perfect-square? ( n -- ? )
41 : alpha-value ( str -- n )
42 >lower [ CHAR: a - 1 + ] map-sum ;
44 : mediant ( a/c b/d -- (a+b)/(c+d) )
45 2>fraction [ + ] 2bi@ / ;
49 : max-children ( seq -- seq )
50 [ dup length 1 - <iota> [ nth-pair max , ] with each ] { } make ;
54 : max-path ( triangle -- n )
56 2 cut* first2 max-children v+ suffix max-path
61 : number>digits ( n -- seq )
62 [ dup 0 = not ] [ 10 /mod ] produce reverse! nip ;
64 : digits>number ( seq -- n )
65 0 [ [ 10 * ] [ + ] bi* ] reduce ;
67 : number-length ( n -- m )
72 [ [ 10 * ] [ 1 + ] bi* ] while 2nip
75 : nth-place ( x n -- y )
76 10^ [ * round >integer ] keep /f ;
78 : nth-prime ( n -- n )
81 : nth-triangle ( n -- n )
84 : palindrome? ( n -- ? )
85 number>string dup reverse = ;
87 : pandigital? ( n -- ? )
88 number>string natural-sort >string "123456789" = ;
90 : pentagonal? ( n -- ? )
91 dup 0 > [ 24 * 1 + sqrt 1 + 6 / 1 mod zero? ] [ drop f ] if ; inline
93 : penultimate ( seq -- elt )
94 dup length 2 - swap nth ;
98 ! Propagate one row into the upper one
99 : propagate ( bottom top -- newtop )
100 [ over rest rot first2 max rot + ] map nip ;
104 ! Not strictly needed, but it is nice to be able to dump the
105 ! triangle after the propagation
106 : propagate-all ( triangle -- new-triangle )
107 reverse unclip dup rot
108 [ propagate dup ] map nip
109 reverse swap suffix ;
113 : count-digits ( n -- byte-array )
115 '[ 10 /mod _ [ 1 + ] change-nth dup 0 > ] loop drop
118 HINTS: count-digits fixnum ;
122 : permutations? ( n m -- ? )
123 [ count-digits ] same? ;
127 : (sum-divisors) ( n -- sum )
128 dup sqrt >integer [1..b] [
129 [ 2dup divisor? [ 2dup / + , ] [ drop ] if ] each
130 dup perfect-square? [ sqrt >fixnum neg , ] [ drop ] if
135 : sum-divisors ( n -- sum )
136 dup 4 < [ { 0 1 3 4 } nth ] [ (sum-divisors) ] if ;
138 : sum-proper-divisors ( n -- sum )
139 [ sum-divisors ] keep - ;
141 : abundant? ( n -- ? )
142 dup sum-proper-divisors < ;
144 : deficient? ( n -- ? )
145 dup sum-proper-divisors > ;
147 : perfect? ( n -- ? )
148 dup sum-proper-divisors = ;
150 ! The divisor function, counts the number of divisors
152 group-factors flip second 1 [ 1 + * ] reduce ;
154 ! Optimized brute-force, is often faster than prime factorization
156 factor-2s dup [ 1 + ]
157 [ perfect-square? -1 0 ? ]
158 [ dup sqrt >fixnum [1..b] ] tri* [
159 dupd divisor? [ [ 2 + ] dip ] when
164 : transform ( triple matrix -- new-triple )
165 [ 1array ] dip mdot first ;
169 ! These transforms are for generating primitive Pythagorean triples
170 : u-transform ( triple -- new-triple )
171 { { 1 2 2 } { -2 -1 -2 } { 2 2 3 } } transform ;
172 : a-transform ( triple -- new-triple )
173 { { 1 2 2 } { 2 1 2 } { 2 2 3 } } transform ;
174 : d-transform ( triple -- new-triple )
175 { { -1 -2 -2 } { 2 1 2 } { 2 2 3 } } transform ;
179 [ name>> "-main" append create-word-in ] keep
180 [ drop current-vocab main<< ]
181 [ [ . ] swap prefix ( -- ) define-declared ]