1 ! Copyright (c) 2007-2008 Aaron Schaefer.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays kernel make math math.functions math.matrices math.miller-rabin
4 math.order math.parser math.primes.factors math.ranges math.ratios
5 sequences sorting strings unicode.case ;
6 IN: project-euler.common
8 ! A collection of words used by more than one Project Euler solution
9 ! and/or related words that could be useful for future problems.
11 ! Problems using each public word
12 ! -------------------------------
13 ! alpha-value - #22, #42
14 ! cartesian-product - #4, #27, #29, #32, #33, #43, #44, #56
18 ! nth-triangle - #12, #42
19 ! number>digits - #16, #20, #30, #34, #35, #38, #43, #52, #55, #56, #92
20 ! palindrome? - #4, #36, #55
21 ! pandigital? - #32, #38
22 ! pentagonal? - #44, #45
23 ! propagate-all - #18, #67
24 ! sum-proper-divisors - #21
26 ! [uad]-transform - #39, #75
29 : nth-pair ( seq n -- nth next )
32 : perfect-square? ( n -- ? )
37 : max-children ( seq -- seq )
38 [ dup length 1- [ nth-pair max , ] with each ] { } make ;
40 ! Propagate one row into the upper one
41 : propagate ( bottom top -- newtop )
42 [ over rest rot first2 max rot + ] map nip ;
44 : (sum-divisors) ( n -- sum )
45 dup sqrt >integer [1,b] [
46 [ 2dup mod 0 = [ 2dup / + , ] [ drop ] if ] each
47 dup perfect-square? [ sqrt >fixnum neg , ] [ drop ] if
50 : transform ( triple matrix -- new-triple )
51 [ 1array ] dip m. first ;
55 : alpha-value ( str -- n )
56 >lower [ CHAR: a - 1+ ] sigma ;
58 : cartesian-product ( seq1 seq2 -- seq1xseq2 )
59 swap [ swap [ 2array ] with map ] with map concat ;
64 : mediant ( a/c b/d -- (a+b)/(c+d) )
65 2>fraction [ + ] 2bi@ / ;
67 : max-path ( triangle -- n )
69 2 cut* first2 max-children [ + ] 2map suffix max-path
74 : number>digits ( n -- seq )
75 [ dup 0 = not ] [ 10 /mod ] [ ] produce reverse nip ;
77 : nth-triangle ( n -- n )
80 : palindrome? ( n -- ? )
81 number>string dup reverse = ;
83 : pandigital? ( n -- ? )
84 number>string natural-sort >string "123456789" = ;
86 : pentagonal? ( n -- ? )
87 dup 0 > [ 24 * 1+ sqrt 1+ 6 / 1 mod zero? ] [ drop f ] if ;
89 ! Not strictly needed, but it is nice to be able to dump the triangle after the
91 : propagate-all ( triangle -- new-triangle )
92 reverse [ first dup ] [ rest ] bi
93 [ propagate dup ] map nip reverse swap suffix ;
95 : sum-divisors ( n -- sum )
96 dup 4 < [ { 0 1 3 4 } nth ] [ (sum-divisors) ] if ;
98 : sum-proper-divisors ( n -- sum )
99 dup sum-divisors swap - ;
101 : abundant? ( n -- ? )
102 dup sum-proper-divisors < ;
104 : deficient? ( n -- ? )
105 dup sum-proper-divisors > ;
107 : perfect? ( n -- ? )
108 dup sum-proper-divisors = ;
110 ! The divisor function, counts the number of divisors
112 group-factors flip second 1 [ 1+ * ] reduce ;
114 ! Optimized brute-force, is often faster than prime factorization
117 [ perfect-square? -1 0 ? ]
118 [ dup sqrt >fixnum [1,b] ] tri* [
119 dupd mod 0 = [ [ 2 + ] dip ] when
122 ! These transforms are for generating primitive Pythagorean triples
123 : u-transform ( triple -- new-triple )
124 { { 1 2 2 } { -2 -1 -2 } { 2 2 3 } } transform ;
125 : a-transform ( triple -- new-triple )
126 { { 1 2 2 } { 2 1 2 } { 2 2 3 } } transform ;
127 : d-transform ( triple -- new-triple )
128 { { -1 -2 -2 } { 2 1 2 } { 2 2 3 } } transform ;