1 ! Copyright (c) 2007 Samuel Tardieu.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays kernel lists lists.lazy math.algebra math math.functions
4 math.order math.primes.lists math.ranges project-euler.common sequences ;
7 ! http://projecteuler.net/index.php?section=problems&id=134
12 ! Consider the consecutive primes p1 = 19 and p2 = 23. It can be verified that
13 ! 1219 is the smallest number such that the last digits are formed by p1 whilst
14 ! also being divisible by p2.
16 ! In fact, with the exception of p1 = 3 and p2 = 5, for every pair of
17 ! consecutive primes, p2 p1, there exist values of n for which the last digits
18 ! are formed by p1 and n is divisible by p2. Let S be the smallest of these
21 ! Find S for every pair of consecutive primes with 5 p1 1000000.
27 ! Compute the smallest power of 10 greater than or equal to m
28 : next-power-of-10 ( m -- n )
29 10 swap log10 ceiling >integer ^ ; foldable
33 ! Compute S for a given pair (p1, p2) -- that is the smallest positive
34 ! number such that X = p1 [npt] and X = 0 [p2] (npt being the smallest
35 ! power of 10 above p1)
37 over 0 2array rot next-power-of-10 rot 2array chinese-remainder ;
41 : euler134 ( -- answer )
42 0 5 lprimes-from uncons swap [ 1000000 > ] luntil
43 [ [ s + ] keep ] leach drop ;
45 ! [ euler134 ] 10 ave-time
46 ! 933 ms ave run timen - 19.58 SD (10 trials)