]> gitweb.factorcode.org Git - factor.git/blob - extra/project-euler/common/common.factor
Using "same?" in more places.
[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     namespaces parser prettyprint quotations sequences sorting strings
8     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 [ + ] 2map 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 triangle after the
115 ! propagation
116 : propagate-all ( triangle -- new-triangle )
117     reverse [ first dup ] [ rest ] bi
118     [ propagate dup ] map nip reverse swap suffix ;
119
120 : permutations? ( n m -- ? )
121     [ count-digits ] same? ;
122
123 : sum-divisors ( n -- sum )
124     dup 4 < [ { 0 1 3 4 } nth ] [ (sum-divisors) ] if ;
125
126 : sum-proper-divisors ( n -- sum )
127     dup sum-divisors swap - ;
128
129 : abundant? ( n -- ? )
130     dup sum-proper-divisors < ;
131
132 : deficient? ( n -- ? )
133     dup sum-proper-divisors > ;
134
135 : perfect? ( n -- ? )
136     dup sum-proper-divisors = ;
137
138 ! The divisor function, counts the number of divisors
139 : tau ( m -- n )
140     group-factors flip second 1 [ 1 + * ] reduce ;
141
142 ! Optimized brute-force, is often faster than prime factorization
143 : tau* ( m -- n )
144     factor-2s dup [ 1 + ]
145     [ perfect-square? -1 0 ? ]
146     [ dup sqrt >fixnum [1,b] ] tri* [
147         dupd divisor? [ [ 2 + ] dip ] when
148     ] each drop * ;
149
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 ;
157
158 SYNTAX: SOLUTION:
159     scan-word
160     [ name>> "-main" append create-in ] keep
161     [ drop current-vocab main<< ]
162     [ [ . ] swap prefix ( -- ) define-declared ]
163     2bi ;