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 fry hints kernel lists make math
5 math.functions math.matrices math.order math.parser math.primes.factors
6 math.primes.lists math.primes.miller-rabin math.ranges math.ratios
7 math.vectors namespaces parser prettyprint quotations sequences sorting
8 strings unicode vocabs 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 -- ? )
43 : count-digits ( n -- byte-array )
45 '[ 10 /mod _ [ 1 + ] change-nth dup 0 > ] loop drop
48 HINTS: count-digits fixnum ;
50 : max-children ( seq -- seq )
51 [ dup length 1 - iota [ nth-pair max , ] with each ] { } make ;
53 ! Propagate one row into the upper one
54 : propagate ( bottom top -- newtop )
55 [ over rest rot first2 max rot + ] map nip ;
57 : (sum-divisors) ( n -- sum )
58 dup sqrt >integer [1,b] [
59 [ 2dup divisor? [ 2dup / + , ] [ drop ] if ] each
60 dup perfect-square? [ sqrt >fixnum neg , ] [ drop ] if
63 : transform ( triple matrix -- new-triple )
64 [ 1array ] dip m. first ;
68 : alpha-value ( str -- n )
69 >lower [ CHAR: a - 1 + ] map-sum ;
71 : mediant ( a/c b/d -- (a+b)/(c+d) )
72 2>fraction [ + ] 2bi@ / ;
74 : max-path ( triangle -- n )
76 2 cut* first2 max-children v+ suffix max-path
81 : number>digits ( n -- seq )
82 [ dup 0 = not ] [ 10 /mod ] produce reverse! nip ;
84 : digits>number ( seq -- n )
85 0 [ [ 10 * ] [ + ] bi* ] reduce ;
87 : number-length ( n -- m )
92 [ [ 10 * ] [ 1 + ] bi* ] while 2nip
95 : nth-place ( x n -- y )
96 10^ [ * round >integer ] keep /f ;
98 : nth-prime ( n -- n )
101 : nth-triangle ( n -- n )
104 : palindrome? ( n -- ? )
105 number>string dup reverse = ;
107 : pandigital? ( n -- ? )
108 number>string natural-sort >string "123456789" = ;
110 : pentagonal? ( n -- ? )
111 dup 0 > [ 24 * 1 + sqrt 1 + 6 / 1 mod zero? ] [ drop f ] if ; inline
113 : penultimate ( seq -- elt )
114 dup length 2 - swap nth ;
116 ! Not strictly needed, but it is nice to be able to dump the
117 ! triangle after the propagation
118 : propagate-all ( triangle -- new-triangle )
119 reverse unclip dup rot
120 [ propagate dup ] map nip
121 reverse swap suffix ;
123 : permutations? ( n m -- ? )
124 [ count-digits ] same? ;
126 : sum-divisors ( n -- sum )
127 dup 4 < [ { 0 1 3 4 } nth ] [ (sum-divisors) ] if ;
129 : sum-proper-divisors ( n -- sum )
130 [ sum-divisors ] keep - ;
132 : abundant? ( n -- ? )
133 dup sum-proper-divisors < ;
135 : deficient? ( n -- ? )
136 dup sum-proper-divisors > ;
138 : perfect? ( n -- ? )
139 dup sum-proper-divisors = ;
141 ! The divisor function, counts the number of divisors
143 group-factors flip second 1 [ 1 + * ] reduce ;
145 ! Optimized brute-force, is often faster than prime factorization
147 factor-2s dup [ 1 + ]
148 [ perfect-square? -1 0 ? ]
149 [ dup sqrt >fixnum [1,b] ] tri* [
150 dupd divisor? [ [ 2 + ] dip ] when
153 ! These transforms are for generating primitive Pythagorean triples
154 : u-transform ( triple -- new-triple )
155 { { 1 2 2 } { -2 -1 -2 } { 2 2 3 } } transform ;
156 : a-transform ( triple -- new-triple )
157 { { 1 2 2 } { 2 1 2 } { 2 2 3 } } transform ;
158 : d-transform ( triple -- new-triple )
159 { { -1 -2 -2 } { 2 1 2 } { 2 2 3 } } transform ;
163 [ name>> "-main" append create-word-in ] keep
164 [ drop current-vocab main<< ]
165 [ [ . ] swap prefix ( -- ) define-declared ]