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