1 ! Copyright (c) 2008 Aaron Schaefer.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: combinators.short-circuit kernel math
4 project-euler.common sequences sorting ;
7 ! http://projecteuler.net/index.php?section=problems&id=52
12 ! It can be seen that the number, 125874, and its double, 251748, contain
13 ! exactly the same digits, but in a different order.
15 ! Find the smallest positive integer, x, such that 2x, 3x, 4x, 5x, and 6x,
16 ! contain the same digits.
22 ! Analysis shows the number must be odd, divisible by 3, and larger than 123456
26 : map-nx ( n x -- seq )
27 [ 1+ * ] with map ; inline
29 : all-same-digits? ( seq -- ? )
30 [ number>digits natural-sort ] map all-equal? ;
32 : candidate? ( n -- ? )
33 { [ odd? ] [ 3 mod 0 = ] } 1&& ;
35 : next-all-same ( x n -- n )
37 2dup swap map-nx all-same-digits?
38 [ nip ] [ 1+ next-all-same ] if
45 : euler052 ( -- answer )
46 6 123456 next-all-same ;
48 ! [ euler052 ] 100 ave-time
49 ! 92 ms ave run time - 6.29 SD (100 trials)