For all positive integers n , the totient function ϕ ( n ) denotes the number of positive integers ≤ n coprime to n .
It turns out that if we apply the totient function successively on any positive integer n > 1 , after finitely many operations we get the number 1 . In other words, for all positive integers n > 1 , there exists a positive integer m such that m times ϕ ( ϕ ( ϕ ( ⋯ ϕ ( n ) ⋯ ) ) ) = 1 .
For all n ∈ N , let f ( n ) denote the minimum number of times we need to apply the totient function successively on n to get the number 1 . Find the last three digits of n = 1 ∑ 2 0 1 4 f ( n ) .
Details and assumptions
As an explicit example, here's how you'd find f ( 5 ) : ϕ ( 5 ) ϕ ( ϕ ( 5 ) ) ϕ ( ϕ ( ϕ ( 5 ) ) ) = = = 4 2 1 Note that we had to apply the totient function three times on 5 to get the number 1 , so f ( 5 ) = 3 .
By convention, f ( 1 ) = 0 .
Just to clarify, this is a computer science problem.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
That's exactly my intended solution!
You should be a bit careful with your wording though; ϕ ( n ) < n when n = 1 . :P
Also, shouldn't you import
math
before you use
sqrt
?
Log in to reply
oh, yes, you are absolutely right, when I copy pasted my code I copy pasted the meat inside of my timing function, and my "from math import sqrt" was outside of the timing function.
Yes,I did it the same way as well..I was familiar with the problem though..was it by anychance inspired from http://projecteuler.net/problem=214
Log in to reply
I'm not very active in ProjectEuler, so I wasn't aware of that. This problem is completely original.
Log in to reply
@Sreejato Bhattacharya – Oh ok..Sorry,my bad..you should have a look at the thread for that problem though(you should be able to solve it with this).They usually have pretty good solutions.There was an O ( n l o g l o g n ) solution if I remember correctly.
Log in to reply
@Thaddeus Abiy – Yes, I just saw it. Thanks for posting it! :)
Python!
from fractions import gcd
def Phi(n): # Computes the totient function!
Result = 0
for i in range (1, n+1):
if gcd(i,n) == 1:
Result += 1
return Result
Anything = 5
def f(n): # The function f given in the question!
Result = 1
Shot = Phi(n)
while 0 < Anything < 10:
if Shot == 1:
break
elif Phi(Shot) == 1:
Result += 1
break
else:
Result += 1
Shot = Phi(Shot)
return Result
Answer = 0
for n in range (2, 2015): # f(1) = 0 by convention, so we won't count that!
Answer += f(n)
Desired_Answer = Answer % 1000
print Answer, Desired_Answer
Output: 1 7 1 0 8 , 1 0 8 .
Okay first, you should establish that the problem statement is indeed true, i.e. f ( n ) is finite for all n ∈ N . This is easy to verify using strong induction. For the base case, note that φ ( 2 ) = 1 , so f ( 2 ) = 1 . Assume the statement is true for 1 , 2 , 3 , ⋯ , n for some n ∈ N , so f ( k ) is finite for all 1 ≤ k ≤ n . Note that φ ( n + 1 ) ≤ n , and hence f ( φ ( n + 1 ) ) is finite, which implies so must be f ( n + 1 ) . This completes the inductive step. ■
This logic also shows a way to make your code more efficient. Apparently your runtime is huge (atleast in my computer)! Note that f ( n ) = f ( φ ( n ) ) + 1 . Instead of using a function, declare an array with 2 0 1 4 elements whose n th element stores f ( n ) .
a= [0]
for x in range(2, 2015):
a.append(a[phi(x)-1]+1)
Just print the sum of the elements of this array now. This seems to be the most efficient way to solve this problem.
Problem Loading...
Note Loading...
Set Loading...
python:
Generate a list of the first 2014 primes via the Sieve of Eratosthenes. Define the function ϕ ( n ) by looping through each of the primes and checking for divisibility. Then, I defined f ( n ) recursively by f ( 1 ) = 0 , f ( n ) = f ( ϕ ( n ) ) + 1 , requiring me to only calculate the totient once per value. Note, ∀ n , ϕ ( n ) < n , so we won't run into any issues with the recursion.
Output: 1 7 1 0 8
Answer: 1 0 8