Solution to Project Euler problem 24
parent
75c126fef0
commit
0a6975c423
|
@ -31,25 +31,29 @@ IN: project-euler.023
|
|||
! SOLUTION
|
||||
! --------
|
||||
|
||||
<PRIVATE
|
||||
|
||||
! The upper limit can be dropped to 20161 which reduces our search space
|
||||
! and every even number > 46 can be expressed as a sum of two abundants
|
||||
|
||||
<PRIVATE
|
||||
|
||||
: source-023 ( -- seq )
|
||||
46 [1,b] 47 20161 2 <range> append ;
|
||||
|
||||
: abundants-below ( n -- seq )
|
||||
: abundants-upto ( n -- seq )
|
||||
[1,b] [ abundant? ] subset ;
|
||||
|
||||
: possible-sums ( seq -- seq )
|
||||
dup { } -rot [
|
||||
dupd [ + ] curry map rot append prune swap 1 tail
|
||||
dupd [ + ] curry map
|
||||
rot append prune swap 1 tail
|
||||
] each drop natural-sort ;
|
||||
|
||||
PRIVATE>
|
||||
|
||||
: euler023 ( -- answer )
|
||||
20161 abundants-below possible-sums source-023 seq-diff sum ;
|
||||
20161 abundants-upto possible-sums source-023 seq-diff sum ;
|
||||
|
||||
! TODO: solution is still too slow, although it takes under 1 minute
|
||||
|
||||
! [ euler023 ] time
|
||||
! 52780 ms run / 3839 ms GC
|
||||
|
|
|
@ -0,0 +1,48 @@
|
|||
! Copyright (c) 2007 Aaron Schaefer.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: kernel math math.parser math.ranges namespaces sequences ;
|
||||
IN: project-euler.024
|
||||
|
||||
! http://projecteuler.net/index.php?section=problems&id=24
|
||||
|
||||
! DESCRIPTION
|
||||
! -----------
|
||||
|
||||
! A permutation is an ordered arrangement of objects. For example, 3124 is one
|
||||
! possible permutation of the digits 1, 2, 3 and 4. If all of the permutations
|
||||
! are listed numerically or alphabetically, we call it lexicographic order. The
|
||||
! lexicographic permutations of 0, 1 and 2 are:
|
||||
|
||||
! 012 021 102 120 201 210
|
||||
|
||||
! What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4,
|
||||
! 5, 6, 7, 8 and 9?
|
||||
|
||||
|
||||
! SOLUTION
|
||||
! --------
|
||||
|
||||
<PRIVATE
|
||||
|
||||
: (>permutation) ( seq n -- seq )
|
||||
[ [ dupd >= [ 1+ ] when ] curry map ] keep add* ;
|
||||
|
||||
: >permutation ( factoradic -- permutation )
|
||||
reverse 1 cut [ (>permutation) ] each ;
|
||||
|
||||
PRIVATE>
|
||||
|
||||
: factoradic ( k order -- factoradic )
|
||||
[ [1,b] [ 2dup mod , /i ] each ] { } make reverse nip ;
|
||||
|
||||
: permutation ( k seq -- seq )
|
||||
dup length swapd factoradic >permutation
|
||||
[ [ dupd swap nth , ] each drop ] { } make ;
|
||||
|
||||
: euler024 ( -- answer )
|
||||
999999 10 permutation 10 swap digits>integer ;
|
||||
|
||||
! [ euler024 ] 100 ave-time
|
||||
! 0 ms run / 0 ms GC ave time - 100 trials
|
||||
|
||||
MAIN: euler024
|
|
@ -7,7 +7,8 @@ USING: definitions io io.files kernel math.parser sequences vocabs
|
|||
project-euler.009 project-euler.010 project-euler.011 project-euler.012
|
||||
project-euler.013 project-euler.014 project-euler.015 project-euler.016
|
||||
project-euler.017 project-euler.018 project-euler.019 project-euler.020
|
||||
project-euler.021 project-euler.022 project-euler.067 project-euler.134 ;
|
||||
project-euler.021 project-euler.022 project-euler.023 project-euler.024
|
||||
project-euler.067 project-euler.134 ;
|
||||
IN: project-euler
|
||||
|
||||
<PRIVATE
|
||||
|
|
Loading…
Reference in New Issue