! 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
! --------
! For n/φ(n) to be minimised, φ(n) must be as close to n as possible; that is,
-! we want to maximise φ(n). The minimal solution for n/φ(n) would be if n was
+! we want to maximize φ(n). The minimal solution for n/φ(n) would be if n was
! prime giving n/(n-1) but since n-1 never is a permutation of n it cannot be
! prime.
7 10^ sqrt >integer 1000 [ - ] [ + ] 2bi primes-between ; inline
: n-and-phi ( seq -- seq' )
- #! ( seq = { p1, p2 } -- seq' = { n, φ(n) } )
+ ! ( seq = { p1, p2 } -- seq' = { n, φ(n) } )
[ product ] [ [ 1 - ] map product ] bi 2array ;
: fit-requirements? ( seq -- ? )
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>