PROBLEM LINKSDIFFICULTYSIMPLE PREREQUISITESPrimality Testing PROBLEMGiven some N, find the i for which φ(i)/i is the largest among all 2≤i≤N. φ(i) is the Euler Totient Function. QUICK EXPLANATIONφ(i) is equal to the number of positive integers less than i, which are coprime to i. The largest value of φ(i)/i is always achieved at prime values of i (primes of course are coprime to all values less than them). Note that for a prime i, φ(i) = i1. Thus, for some N, we need to find the largest prime number we can find, which is less than N. N can be up to 10^{18}. If we test primality via Trial Division, the minimum number of divisions we do for testing the primality of a single number is about 10^{9}. Considering we have to consider several numbers and try to pluck the prime one (and do so for several cases) we are forced to find a faster way to test primality. Miller Rabin is such a test. EXPLANATIONAlthough a probabilistic test, Miller Rabin has been successfully executed up till large enough numbers to show that performing the test while taking the first 9 prime numbers { 2, 3, 5, 7, 11, 13, 17, 19, 23 } as the bases is sufficient to accurately test for primality of all numbers less than 10^{18}. If you end up falling in love with Miller Rabin (the algorithm of course), you will find this link very useful to decide how many of the first few prime numbers to use as bases. It is not necessary to use small primes of course. Random bases can be chosen as well. But you might have to choose more than 9 of them to reduce the probability of a falty answer to a comfortably low level (testing with k random bases reduces the probability of a composite being reported as prime by a factor of 1/4^{k}) There are possibly two hurdles that you may face in implementing Miller Rabin. 1. Finding a^{r} mod n, for a large r This step must be performed using repeated squaring. 2. Multiplying two large integers mod n The product of the two large integers would overflow the 64bit integer data type as well. The only way to get around this is to use some arbitrary precision math. We can assume that the two integers, say a and b, are less than n. One way to perform the multiplication is as follows 1. a_low = a % 10^{9} 2. a_high = a / 10^{9} 3. 4. b_low = b % 10^{9} 5. b_high = b / 10^{9} 6. 7. result = (a_high * b_high) % n 8. 9. repeat 9 times 10. result = (result * 10) % n 11. 12. result = (result + a_low*b_high + b_low*a_high) % n 13. 14. repeat 9 times 15. result = (result * 10) % n 16. 17. result = (result + a_low*b_low) % n The reason the above procedure would work is
SETTER'S SOLUTIONSetter's solution will be updated soon. TESTER'S SOLUTIONCan be found here.
This question is marked "community wiki".
asked 14 May '13, 15:21

This is one of such cases when Java beats C/C++ O:) My AC solution  http://ww2.codechef.com/viewsolution/2081192 answered 14 May '13, 15:40
1
I did the same thing: http://ww2.codechef.com/viewsolution/2102038 BigInteger rocks!
(14 May '13, 15:51)
1
same here hehe... one of the shortest solutions I have ever written. Just one while loop with only one statement
(14 May '13, 22:08)

where are you getting the links to editorials from ???? There are no links on May Challenge page and neither there are links on the Wiki page . The Wiki page is so outdated that it does not have links to Editorials after January contest . @admin : please update wiki page . @admin : Please tell me how others get to know about editorials page and their links earlier than even when these links are posted on contest page . @betlista : Can you please tell where you got link to this editorial . answered 14 May '13, 15:43
links available under recent questions on home page
(14 May '13, 15:48)
2
In my settings I have notifications enabled:
(14 May '13, 15:53)

@gamabunta : when we are multiplying two large numbers which can be upto 10^18 cant we instead multiplying by breaking them into parts ... use (ab)%MOD=(a%MODb%MOD)%MOD. here also the ans will remain within 64 bit in the intermediate results. answered 14 May '13, 16:40
sorry it should be (a%MOD*b%MOD)%MOD
(14 May '13, 16:41)
In the question the MOD is the number n which we are checking for primality . Thus , n<= 10^18 so the terms a%MOD and b%MOD will at max be 10^18  1 and (a%mod * b%mod) will still overflow
(14 May '13, 16:45)

I too used http://oeis.org/A014233 (mentioned in the editorial) during my approach. But that page says, "the primality of numbers < 2^{64} can be determined by asserting strong pseudoprimality to all prime bases ≤ 37 (12^{th} prime)". 10^{18} is almost 2^{60}. Is it because of this small a difference, that testing with 9 primes will suffice? Or, am I missing something?? Here is my AC solution in Python (which tests with 12 primes of course). answered 14 May '13, 16:48
1
In the sequence, the 9th term is more than 3 x 10^{18}. The 8th term is much less than 1 x 10^{18}. So the smallest composite number that Miller Rabin will call prime with the first 8 bases is smaller than the numbers we are considering and we may run in to numbers that fail the test. Using 9 primes assures that the smallest number that will fail the test is larger than the largest number we are considering.
(14 May '13, 20:23)
Oops! I didn't check out the numbers in the sequence. Somehow, just managed to read the comment there!!
(15 May '13, 10:34)

I submitted my code in python of WITMATH during the contest and got AC. When i was submitting the same using C++ it was giving TLE though i had taken all steps to prevent overflow. Please can anyone help. Here's my part of the code where repeated squarring is done: #include<cstdio> #include<cstdlib> using namespace std; unsigned long long int mult ( unsigned long long int a, unsigned long long int b, unsigned long long int c ) { unsigned long long int x=0,y=a%c; while(b>0) { if(b%2==1) { x=(x+y); if(x>c) x=x%c; } y=(y*2); if(y>c) y=y%c; b/=2; } return x%c; } unsigned long long int modulo ( unsigned long long int a, unsigned long long int b, unsigned long long int c ) { unsigned long long int x = 1; unsigned long long int y = a; while (b>0) { if (b%2==1) x = mult(x,y,c); y = mult(y,y,c); b = b/2; } return x%c; } answered 15 May '13, 19:39

I was able to figure out that v should find out the largest prime number <= the given number. How ever I dint know to get rid of tle. Also I was not able to prove that fi(i)/i is max for only prime numbers. 4/5 < 97/99. answered 17 May '13, 10:44

Let N = p_{1}^{r1}p_{2}^{r2}...p_{k}^{rk} φ(N)/N = ((p_{1}  1) / p_{1})((p_{2}  1) / p_{2})...((p_{k}  1) / p_{k}) For some N, let P be the largest prime less than or equal to N, and C be some composite number less than or equal to N. But by definition of P (largest prime less than N), C must be a product of primes less than or equal to P. C = q_{1}^{s1}q_{2}^{s2}...q_{t}^{st} Where each q_{i} ≤ P φ(C)/C = ((q_{1}  1) / q_{1})((q_{2}  1) / q_{2})...((q_{k}  1) / q_{k}) We know that ((a  1) / a) ≤ ((b  1) / b) if a ≤ b Thus φ(C)/C ≤ ((P  1) / P)^{t} ... (because there were t terms) where t ≥ 2 Lastly, we know that since ((P  1) / P) < 1 ((P  1) / P) > ((P  1) / P)^{t} ... for any t ≥ 2 Thus φ(C)/C < ((P  1) / P) or φ(C)/C < φ(P)/P Hence the answer will always be the largest prime less than (or equal to) N. answered 17 May '13, 13:37
@gamabunta : please provide an explanation to my question which i had also posted before.
(17 May '13, 21:32)
@gamabunta : thanx
(18 May '13, 17:26)

Admin can u plz chk http://www.codechef.com/viewsolution/2181171 Even after using miller rabin and optimised modular exponation and product, I m getting TLE.. Before my code used to take 34 sec in my cases. Now only 31 ms. Still TLE :( Please chk.. answered 27 May '13, 00:12

Setter and Tester's solution links are not working
I was able to figure out that v should find out the largest prime number <= the given number. How ever I dint know to get rid of tle. Also I was not able to prove that fi(i)/i is max for only prime numbers. 4/5 < 97/99.
tester's solution...classic
Setter's solution not yet uploaded ?