Author: | ElasticDog |
---|---|
Mode: | factor |
Date: | Fri, 21 Nov 2008 08:30:29 |
! Copyright (c) 2008 Aaron Schaefer. ! See http://factorcode.org/license.txt for BSD license. USING: byte-arrays fry hints kernel math math.primes sequences ; IN: project-euler.049 ! http://projecteuler.net/index.php?section=problems&id=49 ! DESCRIPTION ! ----------- ! The arithmetic sequence, 1487, 4817, 8147, in which each of the terms ! increases by 3330, is unusual in two ways: (i) each of the three terms are ! prime, and, (ii) each of the 4-digit numbers are permutations of one another. ! There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, ! exhibiting this property, but there is one other 4-digit increasing sequence. ! What 12-digit number do you form by concatenating the three terms in this ! sequence? ! SOLUTION ! -------- <PRIVATE : count-digits ( n -- byte-array ) 10 <byte-array> [ '[ 10 /mod _ [ 1+ ] change-nth dup 0 > ] loop drop ] keep ; HINTS: count-digits fixnum ; : permutations? ( n m -- ? ) [ count-digits ] bi@ = ; : collect-permutations ( seq -- seq ) [ V{ } clone ] [ dup length ] bi* [ unclip-slice 2dup '[ _ permutations? ] filter swap prefix pick push ] times drop ; PRIVATE> : euler049 ( -- answer ) 1000 9999 primes-between collect-permutations ; ! [ euler049 ] 20 ave-time ! 420 ms ave run time - 17.38 SD (20 trials) MAIN: euler049