]> gitweb.factorcode.org Git - factor.git/commitdiff
project-euler.*: uses for arg-max and arg-min
authorBjörn Lindqvist <bjourne@gmail.com>
Thu, 8 Feb 2018 18:43:44 +0000 (19:43 +0100)
committerBjörn Lindqvist <bjourne@gmail.com>
Thu, 8 Feb 2018 18:48:08 +0000 (19:48 +0100)
extra/project-euler/039/039.factor
extra/project-euler/069/069.factor
extra/project-euler/070/070.factor
extra/project-euler/099/099.factor

index 1ad163d5070293ac853692077250587c14eac831..613e03a05cd437b142a4d2a1e3467d737efd2c9b 100644 (file)
@@ -1,7 +1,7 @@
 ! Copyright (c) 2008 Aaron Schaefer.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: arrays kernel math math.ranges
-    namespaces project-euler.common sequences ;
+USING: arrays kernel math math.ranges namespaces project-euler.common
+sequences sequences.extras ;
 IN: project-euler.039
 
 ! http://projecteuler.net/index.php?section=problems&id=39
@@ -56,7 +56,7 @@ PRIVATE>
 
 : euler039 ( -- answer )
     [
-        1000 count-perimeters p-count get [ supremum ] keep index
+        1000 count-perimeters p-count get arg-max
     ] with-scope ;
 
 ! [ euler039 ] 100 ave-time
index 5094dcd674df0fd1f2544c6a59f95a38b9f508be..1e7520f25c4366fbf9e78a5efb5eaf1545a5e933 100644 (file)
@@ -1,7 +1,7 @@
 ! Copyright (c) 2009 Aaron Schaefer.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: combinators fry kernel math math.primes math.primes.factors math.ranges
-    project-euler.common sequences ;
+USING: combinators fry kernel math math.primes math.primes.factors
+math.ranges project-euler.common sequences sequences.extras ;
 IN: project-euler.069
 
 ! http://projecteuler.net/index.php?section=problems&id=69
@@ -47,7 +47,7 @@ PRIVATE>
 
 : euler069 ( -- answer )
     2 1000000 [a,b] [ totient-ratio ] map
-    [ supremum ] keep index 2 + ;
+    arg-max 2 + ;
 
 ! [ euler069 ] 10 ave-time
 ! 25210 ms ave run time - 115.37 SD (10 trials)
index 499ce21c340a98eb82246dab18ee16a1598a192b..09f10986552b3bdb5e7a241d92a9e1533a1819b7 100644 (file)
@@ -1,8 +1,9 @@
 ! Copyright (c) 2010 Aaron Schaefer. All rights reserved.
 ! 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
-USING: arrays assocs combinators.short-circuit kernel math math.combinatorics
-    math.functions math.primes math.ranges project-euler.common sequences ;
+USING: arrays combinators.short-circuit kernel math math.combinatorics
+math.functions math.primes project-euler.common sequences
+sequences.extras ;
 FROM: project-euler.common => permutations? ;
 IN: project-euler.070
 
@@ -54,7 +55,7 @@ IN: project-euler.070
     first2 { [ drop 7 10^ < ] [ permutations? ] } 2&& ;
 
 : minimum-ratio ( seq -- n )
-    [ [ first2 / ] map [ infimum ] keep index ] keep nth first ;
+    [ [ first2 / ] map arg-min ] keep nth first ;
 
 PRIVATE>
 
index 64c5533a6980d417f2448089e8df7d68fb0d5161..36fae686d5c747aaf89e54119bfd14bed28bf1e2 100644 (file)
@@ -1,7 +1,8 @@
 ! Copyright (c) 2008 Aaron Schaefer.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: io.encodings.ascii io.files kernel math math.functions math.parser
-    math.vectors sequences splitting project-euler.common ;
+USING: io.encodings.ascii io.files kernel math math.functions
+math.parser math.vectors project-euler.common sequences
+sequences.extras splitting ;
 IN: project-euler.099
 
 ! http://projecteuler.net/index.php?section=problems&id=99
@@ -39,7 +40,7 @@ IN: project-euler.099
     flip first2 swap [ log ] map v* ;
 
 : solve ( seq -- index )
-    simplify [ supremum ] keep index 1 + ;
+    simplify arg-max 1 + ;
 
 PRIVATE>