project-euler.*: uses for arg-max and arg-min

factor-shell
Björn Lindqvist 2018-02-08 19:43:44 +01:00
parent 603354a02e
commit 7b2c2a93f5
4 changed files with 14 additions and 12 deletions

View File

@ -1,7 +1,7 @@
! Copyright (c) 2008 Aaron Schaefer. ! Copyright (c) 2008 Aaron Schaefer.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: arrays kernel math math.ranges USING: arrays kernel math math.ranges namespaces project-euler.common
namespaces project-euler.common sequences ; sequences sequences.extras ;
IN: project-euler.039 IN: project-euler.039
! http://projecteuler.net/index.php?section=problems&id=39 ! http://projecteuler.net/index.php?section=problems&id=39
@ -56,7 +56,7 @@ PRIVATE>
: euler039 ( -- answer ) : euler039 ( -- answer )
[ [
1000 count-perimeters p-count get [ supremum ] keep index 1000 count-perimeters p-count get arg-max
] with-scope ; ] with-scope ;
! [ euler039 ] 100 ave-time ! [ euler039 ] 100 ave-time

View File

@ -1,7 +1,7 @@
! Copyright (c) 2009 Aaron Schaefer. ! Copyright (c) 2009 Aaron Schaefer.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: combinators fry kernel math math.primes math.primes.factors math.ranges USING: combinators fry kernel math math.primes math.primes.factors
project-euler.common sequences ; math.ranges project-euler.common sequences sequences.extras ;
IN: project-euler.069 IN: project-euler.069
! http://projecteuler.net/index.php?section=problems&id=69 ! http://projecteuler.net/index.php?section=problems&id=69
@ -47,7 +47,7 @@ PRIVATE>
: euler069 ( -- answer ) : euler069 ( -- answer )
2 1000000 [a,b] [ totient-ratio ] map 2 1000000 [a,b] [ totient-ratio ] map
[ supremum ] keep index 2 + ; arg-max 2 + ;
! [ euler069 ] 10 ave-time ! [ euler069 ] 10 ave-time
! 25210 ms ave run time - 115.37 SD (10 trials) ! 25210 ms ave run time - 115.37 SD (10 trials)

View File

@ -1,8 +1,9 @@
! Copyright (c) 2010 Aaron Schaefer. All rights reserved. ! Copyright (c) 2010 Aaron Schaefer. All rights reserved.
! The contents of this file are licensed under the Simplified BSD License ! The contents of this file are licensed under the Simplified BSD License
! A copy of the license is available at http://factorcode.org/license.txt ! A copy of the license is available at http://factorcode.org/license.txt
USING: arrays assocs combinators.short-circuit kernel math math.combinatorics USING: arrays combinators.short-circuit kernel math math.combinatorics
math.functions math.primes math.ranges project-euler.common sequences ; math.functions math.primes project-euler.common sequences
sequences.extras ;
FROM: project-euler.common => permutations? ; FROM: project-euler.common => permutations? ;
IN: project-euler.070 IN: project-euler.070
@ -54,7 +55,7 @@ IN: project-euler.070
first2 { [ drop 7 10^ < ] [ permutations? ] } 2&& ; first2 { [ drop 7 10^ < ] [ permutations? ] } 2&& ;
: minimum-ratio ( seq -- n ) : minimum-ratio ( seq -- n )
[ [ first2 / ] map [ infimum ] keep index ] keep nth first ; [ [ first2 / ] map arg-min ] keep nth first ;
PRIVATE> PRIVATE>

View File

@ -1,7 +1,8 @@
! Copyright (c) 2008 Aaron Schaefer. ! Copyright (c) 2008 Aaron Schaefer.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: io.encodings.ascii io.files kernel math math.functions math.parser USING: io.encodings.ascii io.files kernel math math.functions
math.vectors sequences splitting project-euler.common ; math.parser math.vectors project-euler.common sequences
sequences.extras splitting ;
IN: project-euler.099 IN: project-euler.099
! http://projecteuler.net/index.php?section=problems&id=99 ! http://projecteuler.net/index.php?section=problems&id=99
@ -39,7 +40,7 @@ IN: project-euler.099
flip first2 swap [ log ] map v* ; flip first2 swap [ log ] map v* ;
: solve ( seq -- index ) : solve ( seq -- index )
simplify [ supremum ] keep index 1 + ; simplify arg-max 1 + ;
PRIVATE> PRIVATE>