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 namespaces parser prettyprint quotations sequences sorting strings
8 unicode.case vocabs vocabs.parser words ;
9 FROM: sequences => change-nth ;
10 IN: project-euler.common
12 ! A collection of words used by more than one Project Euler solution
13 ! and/or related words that could be useful for future problems.
15 ! Problems using each public word
16 ! -------------------------------
17 ! alpha-value - #22, #42
18 ! cartesian-product - #4, #27, #29, #32, #33, #43, #44, #56
23 ! nth-triangle - #12, #42
24 ! number>digits - #16, #20, #30, #34, #35, #38, #43, #52, #55, #56, #92, #206
25 ! palindrome? - #4, #36, #55
26 ! pandigital? - #32, #38
27 ! pentagonal? - #44, #45
28 ! penultimate - #69, #71
29 ! propagate-all - #18, #67
30 ! permutations? - #49, #70
31 ! sum-proper-divisors - #21
33 ! [uad]-transform - #39, #75
36 : nth-pair ( seq n -- nth next )
39 : perfect-square? ( n -- ? )
44 : count-digits ( n -- byte-array )
46 '[ 10 /mod _ [ 1 + ] change-nth dup 0 > ] loop drop
49 HINTS: count-digits fixnum ;
51 : max-children ( seq -- seq )
52 [ dup length 1 - iota [ nth-pair max , ] with each ] { } make ;
54 ! Propagate one row into the upper one
55 : propagate ( bottom top -- newtop )
56 [ over rest rot first2 max rot + ] map nip ;
58 : (sum-divisors) ( n -- sum )
59 dup sqrt >integer [1,b] [
60 [ 2dup divisor? [ 2dup / + , ] [ drop ] if ] each
61 dup perfect-square? [ sqrt >fixnum neg , ] [ drop ] if
64 : transform ( triple matrix -- new-triple )
65 [ 1array ] dip m. first ;
69 : alpha-value ( str -- n )
70 >lower [ CHAR: a - 1 + ] map-sum ;
72 : mediant ( a/c b/d -- (a+b)/(c+d) )
73 2>fraction [ + ] 2bi@ / ;
75 : max-path ( triangle -- n )
77 2 cut* first2 max-children [ + ] 2map suffix max-path
82 : number>digits ( n -- seq )
83 [ dup 0 = not ] [ 10 /mod ] produce reverse! nip ;
85 : number-length ( n -- m )
90 [ [ 10 * ] [ 1 + ] bi* ] while 2nip
93 : nth-place ( x n -- y )
94 10^ [ * round >integer ] keep /f ;
96 : nth-prime ( n -- n )
99 : nth-triangle ( n -- n )
102 : palindrome? ( n -- ? )
103 number>string dup reverse = ;
105 : pandigital? ( n -- ? )
106 number>string natural-sort >string "123456789" = ;
108 : pentagonal? ( n -- ? )
109 dup 0 > [ 24 * 1 + sqrt 1 + 6 / 1 mod zero? ] [ drop f ] if ; inline
111 : penultimate ( seq -- elt )
112 dup length 2 - swap nth ;
114 ! Not strictly needed, but it is nice to be able to dump the triangle after the
116 : propagate-all ( triangle -- new-triangle )
117 reverse [ first dup ] [ rest ] bi
118 [ propagate dup ] map nip reverse swap suffix ;
120 : permutations? ( n m -- ? )
121 [ count-digits ] bi@ = ;
123 : sum-divisors ( n -- sum )
124 dup 4 < [ { 0 1 3 4 } nth ] [ (sum-divisors) ] if ;
126 : sum-proper-divisors ( n -- sum )
127 dup sum-divisors swap - ;
129 : abundant? ( n -- ? )
130 dup sum-proper-divisors < ;
132 : deficient? ( n -- ? )
133 dup sum-proper-divisors > ;
135 : perfect? ( n -- ? )
136 dup sum-proper-divisors = ;
138 ! The divisor function, counts the number of divisors
140 group-factors flip second 1 [ 1 + * ] reduce ;
142 ! Optimized brute-force, is often faster than prime factorization
144 factor-2s dup [ 1 + ]
145 [ perfect-square? -1 0 ? ]
146 [ dup sqrt >fixnum [1,b] ] tri* [
147 dupd divisor? [ [ 2 + ] dip ] when
150 ! These transforms are for generating primitive Pythagorean triples
151 : u-transform ( triple -- new-triple )
152 { { 1 2 2 } { -2 -1 -2 } { 2 2 3 } } transform ;
153 : a-transform ( triple -- new-triple )
154 { { 1 2 2 } { 2 1 2 } { 2 2 3 } } transform ;
155 : d-transform ( triple -- new-triple )
156 { { -1 -2 -2 } { 2 1 2 } { 2 2 3 } } transform ;
160 [ name>> "-main" append create-in ] keep
161 [ drop current-vocab main<< ]
162 [ [ . ] swap prefix ( -- ) define-declared ]