Let ϕ ( n ) denote the Euler's totient function .
There exists only one positive integer a ≤ 1 0 1 0 such that ϕ ( a ) = ϕ ( a + 1 ) = ϕ ( a + 2 ) .
Find the remainder when that number a is divided by 1729.
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.
I think there is some optimizations you can do, first the obvious : you compute phi 4 times for each value, you should store the previous results in 2 variables, then phi should compute faster by using the primes factors of n , as you have to test divisibility n times in the worst case, instead of computing n times the gcd. The formula tu use is ϕ ( n ) = ∑ ( p − 1 ) p i − 1 where p are primes factors of n and i their power.
I have come with two algorithms using this formula and storing the previous value of ϕ The first one use this fact : if p divide n then ϕ ( n ) = ( p − δ ) ϕ ( n / p ) where δ is 1 if p doesn't divide n / p, else 0.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
|
Another method is to fill the array by multiplying each multiple of a prime p by p - 1 and multiplying by p each multiple of a power ≥ 2 . This is faster because we travel the array in simple and predictable way, there is no cache miss nor division test. The p values are found when a 1 is encountered in the array (meaning p is not multiple of a previous prime hence p is prime)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
As nobody has 80 Gigabytes of ram, we cannot compute the full 1 0 1 0 using 64 bits integers. But by storing the primes ≤ 1 0 5 and their power ≤ 1 0 1 0 , and using chunks of 1000000 integer, we can construct ϕ and reconstruct the n's by using the previous code. We reconstruct the n's as if they have not been fully reconstructed, let say m ≤ n , we know that n / m must be a prime outside the list, and we finish p h i construction by multiplying by n / m − 1
Yes, I may have overthink this one :)
Log in to reply
Thanks for your inputs. I actually used brute force method but I see how efficient your algorithms are.
Here's quite efficient code, using two sieves.
I can just change N from 1 0 5 to bigger value if no value is found.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
|
Python code: (seems to be pretty efficient. My trick was to brute force ϕ , instead of wasting bytes on finding factors.)
import fractions
def phi(n):
amount = 0
for k in range(1, n + 1):
if fractions.gcd(n, k) == 1:
amount += 1
return amount
for i in range(1, 10000000):
if phi(i) == phi(i + 1) == phi(i + 2):
print i % 1729
I could only do 2 9 because my IDE wouldn't let me go higher.
I am not a very good coder, and while I wrote a program from this, I could not check upto 1 0 1 0 because I am comfortable in Python where if we give such a big range it shows Memory Error, so I just extended the range till I was getting a solution.
I got 5186 that way, and I must say, that Nitish's code is exactly what I had written( we only have to change the syntax from C++ to python ).
But more interestingly, there is another problem for which I needed to know ϕ ( 1 ) and I had forgotten whether the definition of Totient had a 'less than equal to n' or 'less than n' condition. So I went here . On scrolling down I found some nice looking properties, and I found a statement which stated exactly what this problem asked and remembered about this problem.
The properties there seem quite interesting exercises to prove.
My code in java ``class sam
{
public static void main(String ar[])
{
long i=0l;
for(i=2;i<=10000000000l;i++)
if(phi(i,1)==phi(i+1,1) && phi(i,1)==phi(i+2,1))
{
System.out.println(i%1729);
}
}
public static long phi(long i,long m)
{
long j=0l,k=0l;
for(j=m+1;j<=i;j++)
{
if(i%j==0)
{
for(k=2;k<=j/2;k++)
{
if(j%k==0)
break;
}
if(k==((j/2)+1))
return phi((i*(j-1))/j,j);
}
}
return i;
}
}
``
this gives the answer as 5186 and can be used till (i+2) is under the limit of long
for clarity anf formatting, I would give this an extra four spaces. It keeps going in an out of code.
Problem Loading...
Note Loading...
Set Loading...
I have solved it using C++. I am pretty new to programming, so my program was not at all efficient. Could anyone suggest an efficient way/algorithm to solve this problem?
Answer: 5186
Note that I have checked the numbers only till 1 0 8 as I was having some problems using long int data type for numbers greater than that.