Project Euler 69 largest Totient

Euler function \ (\ varphi (n) \ ) is calculated smaller than \ (n-\) a natural number and \ (n-\) number of prime numbers, such as 1, 2, 4, 5, 7 and 8 are smaller than 9 and 9 and qualities, so \ (\ varphi (9). 6 = \) . The following table shows the Euler function value less than or equal to the number ten:

Can be seen that for \ (n \ le10 \) when \ (n = 6 \) when \ (n / \ varphi (n ) \) to obtain a maximum value, for seeking \ (n-\ le1,000,000 \) , in \ ( n \) when the number is equal to \ (n / \ varphi (n ) \) to obtain the maximum value?

Analysis: Euler function is a function of number theory often encounter, we can sum it through a formula \ (n \) prime factors connected. In general, for a positive integer \ (n \) , set \ (p | n \) indicates that all divisible by \ (n \) prime number is actually \ (n \) prime factors, then we have:
\ [{ \ displaystyle \ varphi (n) =
n \ prod _ {p \ mid n} \ left (1 - {\ frac {1} {p}} \ right),} \] as for \ (\ varphi (36) \ ) can be calculated as follows:
\ [{\ DisplayStyle \ varphi (36) = \ varphi \ left (2 ^ {2}. 3 ^ {2} \ right) = 36 \ left (. 1 - {\ tfrac {. 1} {2 }} \ right) \ left ( 1 - {\ tfrac {1} {3}} \ right) = 36 \ cdot {\ tfrac {1} {2}} \ cdot {\ tfrac {2} {3}} = 12.} \]
Accordingly, required title \ (n / \ varphi (n ) \) of the maximum value, there are:
\ [n-/ \ varphi (n-) = \ {} n-n-FRAC {\ _ {P Prod \ mid n} \ left (1 - {\ frac {1} {p}} \ right)} = \ frac {1} {\ prod _ {p \ mid n} \ left (1 - {\ frac {1} {p}} \ right)} = \ prod \ limits_ {p | n} \ dfrac {p} {p-1} \]
Since \ (P / (P-. 1)>. 1 \) , then make \ (n / \ varphi (n ) \) maximum, so that only calculated for \ (\ n le10 ^ 6 \ ) up to the prime factor the number can be. We can pass a lot of small to prime numbers multiplied in order to ensure that the product is not more than one million, but multiplied by a prime number will be over one million. After attempts, we found that this number is \ (2 \ cdot3 \ cdot5 \ cdot7 \ cdot11 \ cdot13 \ cdot17 = 510510 \) , is the subject of the request. Obviously, this question can only written calculation, but I wrote a little code as follows:

# time cost = 6.91 ms ± 154 µs

from sympy import prime

def main(N=10**6):
    i,prod,arr = 1,1,[]
    while prod <= N:
        prod *= prime(i)
        arr.append(prod)
        i += 1
    return arr[-2]

Guess you like

Origin www.cnblogs.com/metaquant/p/11958828.html