]> gitweb.factorcode.org Git - factor.git/blob - extra/project-euler/common/common.factor
more minor cleanup.
[factor.git] / extra / project-euler / common / common.factor
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
11
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.
14
15 ! Problems using each public word
16 ! -------------------------------
17 ! alpha-value - #22, #42
18 ! cartesian-product - #4, #27, #29, #32, #33, #43, #44, #56
19 ! log10 - #25, #134
20 ! max-path - #18, #67
21 ! mediant - #71, #73
22 ! nth-prime - #7, #69
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
32 ! tau* - #12
33 ! [uad]-transform - #39, #75
34
35
36 : nth-pair ( seq n -- nth next )
37     tail-slice first2 ;
38
39 : perfect-square? ( n -- ? )
40     dup sqrt mod zero? ;
41
42 <PRIVATE
43
44 : count-digits ( n -- byte-array )
45     10 <byte-array> [
46         '[ 10 /mod _ [ 1 + ] change-nth dup 0 > ] loop drop
47     ] keep ;
48
49 HINTS: count-digits fixnum ;
50
51 : max-children ( seq -- seq )
52     [ dup length 1 - iota [ nth-pair max , ] with each ] { } make ;
53
54 ! Propagate one row into the upper one
55 : propagate ( bottom top -- newtop )
56     [ over rest rot first2 max rot + ] map nip ;
57
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
62     ] { } make sum ;
63
64 : transform ( triple matrix -- new-triple )
65     [ 1array ] dip m. first ;
66
67 PRIVATE>
68
69 : alpha-value ( str -- n )
70     >lower [ CHAR: a - 1 + ] map-sum ;
71
72 : mediant ( a/c b/d -- (a+b)/(c+d) )
73     2>fraction [ + ] 2bi@ / ;
74
75 : max-path ( triangle -- n )
76     dup length 1 > [
77         2 cut* first2 max-children v+ suffix max-path
78     ] [
79         first first
80     ] if ;
81
82 : number>digits ( n -- seq )
83     [ dup 0 = not ] [ 10 /mod ] produce reverse! nip ;
84
85 : number-length ( n -- m )
86     abs [
87         1
88     ] [
89         1 0 [ 2over >= ]
90         [ [ 10 * ] [ 1 + ] bi* ] while 2nip
91     ] if-zero ;
92
93 : nth-place ( x n -- y )
94     10^ [ * round >integer ] keep /f ;
95
96 : nth-prime ( n -- n )
97     1 - lprimes lnth ;
98
99 : nth-triangle ( n -- n )
100     dup 1 + * 2 / ;
101
102 : palindrome? ( n -- ? )
103     number>string dup reverse = ;
104
105 : pandigital? ( n -- ? )
106     number>string natural-sort >string "123456789" = ;
107
108 : pentagonal? ( n -- ? )
109     dup 0 > [ 24 * 1 + sqrt 1 + 6 / 1 mod zero? ] [ drop f ] if ; inline
110
111 : penultimate ( seq -- elt )
112     dup length 2 - swap nth ;
113
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 ;
120
121 : permutations? ( n m -- ? )
122     [ count-digits ] same? ;
123
124 : sum-divisors ( n -- sum )
125     dup 4 < [ { 0 1 3 4 } nth ] [ (sum-divisors) ] if ;
126
127 : sum-proper-divisors ( n -- sum )
128     [ sum-divisors ] keep - ;
129
130 : abundant? ( n -- ? )
131     dup sum-proper-divisors < ;
132
133 : deficient? ( n -- ? )
134     dup sum-proper-divisors > ;
135
136 : perfect? ( n -- ? )
137     dup sum-proper-divisors = ;
138
139 ! The divisor function, counts the number of divisors
140 : tau ( m -- n )
141     group-factors flip second 1 [ 1 + * ] reduce ;
142
143 ! Optimized brute-force, is often faster than prime factorization
144 : tau* ( m -- n )
145     factor-2s dup [ 1 + ]
146     [ perfect-square? -1 0 ? ]
147     [ dup sqrt >fixnum [1,b] ] tri* [
148         dupd divisor? [ [ 2 + ] dip ] when
149     ] each drop * ;
150
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 ;
158
159 SYNTAX: SOLUTION:
160     scan-word
161     [ name>> "-main" append create-in ] keep
162     [ drop current-vocab main<< ]
163     [ [ . ] swap prefix ( -- ) define-declared ]
164     2bi ;