1 ! Copyright (c) 2009 Aaron Schaefer.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: combinators kernel math math.primes math.primes.factors
4 ranges project-euler.common sequences sequences.extras ;
7 ! https://projecteuler.net/problem=69
12 ! Euler's Totient function, φ(n) [sometimes called the phi
13 ! function], is used to determine the number of numbers less
14 ! than n which are relatively prime to n. For example, as 1, 2,
15 ! 4, 5, 7, and 8, are all less than nine and relatively prime to
18 ! +----+------------------+------+-----------+
19 ! | n | Relatively Prime | φ(n) | n / φ(n) |
20 ! +----+------------------+------+-----------+
22 ! | 3 | 1,2 | 2 | 1.5 |
24 ! | 5 | 1,2,3,4 | 4 | 1.25 |
26 ! | 7 | 1,2,3,4,5,6 | 6 | 1.1666... |
27 ! | 8 | 1,3,5,7 | 4 | 2 |
28 ! | 9 | 1,2,4,5,7,8 | 6 | 1.5 |
29 ! | 10 | 1,3,7,9 | 4 | 2.5 |
30 ! +----+------------------+------+-----------+
32 ! It can be seen that n = 6 produces a maximum n / φ(n) for n ≤
35 ! Find the value of n ≤ 1,000,000 for which n / φ(n) is a
46 : totient-ratio ( n -- m )
51 : euler069 ( -- answer )
52 2 1000000 [a..b] [ totient-ratio ] map
55 ! [ euler069 ] 10 ave-time
56 ! 25210 ms ave run time - 115.37 SD (10 trials)
62 ! In order to obtain maximum n / φ(n), φ(n) needs to be low and n needs to be
63 ! high. Hence we need a number that has the most factors. A number with the
64 ! most unique factors would have fewer relatively prime.
68 : primorial ( n -- m )
70 { [ dup 0 = ] [ drop V{ 1 } ] }
71 { [ dup 1 = ] [ drop V{ 2 } ] }
72 [ nth-prime primes-upto ]
75 : primorial-upto ( limit -- m )
76 1 swap '[ dup primorial _ <= ] [ 1 + dup primorial ] produce
81 : euler069a ( -- answer )
82 1000000 primorial-upto ;
84 ! [ euler069a ] 100 ave-time
85 ! 0 ms ave run time - 0.01 SD (100 trials)