From 4eb7aad50bd70a117873a5740d7df5146ae0d355 Mon Sep 17 00:00:00 2001 From: Samuel Tardieu Date: Wed, 26 Dec 2007 23:08:43 +0100 Subject: [PATCH] Use math.algebra to solve project Euler problem 134 --- extra/project-euler/134/134.factor | 33 ++++++------------------------ 1 file changed, 6 insertions(+), 27 deletions(-) diff --git a/extra/project-euler/134/134.factor b/extra/project-euler/134/134.factor index 9b623e7cb8..b30b0073d2 100644 --- a/extra/project-euler/134/134.factor +++ b/extra/project-euler/134/134.factor @@ -1,7 +1,7 @@ ! Copyright (c) 2007 Samuel Tardieu. ! See http://factorcode.org/license.txt for BSD license. -USING: arrays kernel math math.functions math.ranges math.primes.list namespaces - sequences vars ; +USING: arrays kernel math.algebra math math.functions math.primes.list + math.ranges sequences ; IN: project-euler.134 ! http://projecteuler.net/index.php?section=problems&id=134 @@ -23,36 +23,15 @@ IN: project-euler.134 ! SOLUTION ! -------- -integer [ 10 * ] times ; foldable -! Helper variables and words for the extended Euclidian algorithm -! See http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm - -VARS: r-1 u-1 v-1 r u v ; - -: init ( a b -- ) - >r >r-1 0 >u 1 >u-1 1 >v 0 >v-1 ; - -: advance ( r u v -- ) - v> >v-1 >v u> >u-1 >u r> >r-1 >r ; - -: step ( -- ) - r-1> r> 2dup / >integer [ * - ] keep u-1> over u> * - v-1> rot v> * - - advance ; - -! Compute the inverse of a in field Z/bZ where b is prime -: inverse ( a b -- a-1 ) - [ init [ r> 0 > ] [ step ] [ ] while u-1> ] with-scope ; - -! Compute S for a given pair (p1, p2) +! Compute S for a given pair (p1, p2) -- that is the smallest positive +! number such that X = p1 [npt] and X = 0 [p2] (npt being the smallest +! power of 10 above p1) : s ( p1 p2 -- s ) - over next-power-of-10 [ over inverse pick * neg swap rem ] keep * + ; - -PRIVATE> + over 0 2array rot next-power-of-10 rot 2array chinese-remainder ; : euler134 ( -- answer ) primes-under-million 2 tail dup 1 tail 1000003 add [ s ] 2map sum ;