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.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 v+ 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
115 ! triangle after the propagation
116 : propagate-all ( triangle -- new-triangle )
117 reverse unclip dup rot
118 [ propagate dup ] map nip
119 reverse swap suffix ;
121 : permutations? ( n m -- ? )
122 [ count-digits ] same? ;
124 : sum-divisors ( n -- sum )
125 dup 4 < [ { 0 1 3 4 } nth ] [ (sum-divisors) ] if ;
127 : sum-proper-divisors ( n -- sum )
128 [ sum-divisors ] keep - ;
130 : abundant? ( n -- ? )
131 dup sum-proper-divisors < ;
133 : deficient? ( n -- ? )
134 dup sum-proper-divisors > ;
136 : perfect? ( n -- ? )
137 dup sum-proper-divisors = ;
139 ! The divisor function, counts the number of divisors
141 group-factors flip second 1 [ 1 + * ] reduce ;
143 ! Optimized brute-force, is often faster than prime factorization
145 factor-2s dup [ 1 + ]
146 [ perfect-square? -1 0 ? ]
147 [ dup sqrt >fixnum [1,b] ] tri* [
148 dupd divisor? [ [ 2 + ] dip ] when
151 ! These transforms are for generating primitive Pythagorean triples
152 : u-transform ( triple -- new-triple )
153 { { 1 2 2 } { -2 -1 -2 } { 2 2 3 } } transform ;
154 : a-transform ( triple -- new-triple )
155 { { 1 2 2 } { 2 1 2 } { 2 2 3 } } transform ;
156 : d-transform ( triple -- new-triple )
157 { { -1 -2 -2 } { 2 1 2 } { 2 2 3 } } transform ;
161 [ name>> "-main" append create-in ] keep
162 [ drop current-vocab main<< ]
163 [ [ . ] swap prefix ( -- ) define-declared ]