×

SIMPLE

# PREREQUISITES

Primality Testing

# PROBLEM

Given 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 co-prime to i. The largest value of φ(i)/i is always achieved at prime values of i (primes of course are co-prime to all values less than them). Note that for a prime i, φ(i) = i-1.

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 1018. If we test primality via Trial Division, the minimum number of divisions we do for testing the primality of a single number is about 109. 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.

# EXPLANATION

Although 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 1018.

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/4k)

There are possibly two hurdles that you may face in implementing Miller Rabin.

1. Finding ar 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 64-bit 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 % 109
2. a_high = a / 109
3.
4. b_low = b % 109
5. b_high = b / 109
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

• The product in line 7, 12 and 17 are betwee two 30-bit integers and will not overflow 64-bit integers.
• The product in line 10 and 15 are between a 60-bit integer and 4-bit integer and will not overflow 64-bit integers. (although we must use unsigned)

# SETTER'S SOLUTION

Setter's solution will be updated soon.

# TESTER'S SOLUTION

Can be found here.

This question is marked "community wiki".

2.4k128183169
accept rate: 14%

19.8k350498541

2

Setter and Tester's solution links are not working

(14 May '13, 21:27)

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.

(15 May '13, 10:50)
2

tester's solution...classic

(17 May '13, 21:10) 3★

Setter's solution not yet uploaded ?

(22 May '13, 00:07) 2★

 0 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 3-4 sec in my cases. Now only 31 ms. Still TLE :( Please chk.. answered 27 May '13, 00:12 16●1●2●3 accept rate: 0%
 1 Let N = p1r1p2r2...pkrk φ(N)/N = ((p1 - 1) / p1)((p2 - 1) / p2)...((pk - 1) / pk) 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 = q1s1q2s2...qtst Where each qi ≤ P φ(C)/C = ((q1 - 1) / q1)((q2 - 1) / q2)...((qk - 1) / qk) 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 2.4k●128●183●169 accept rate: 14% @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)
 0 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 21●1●3●9 accept rate: 0%
 0 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 #include 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 299●3●5●10 accept rate: 0% 2.4k●128●183●169
 0 I too used http://oeis.org/A014233 (mentioned in the editorial) during my approach. But that page says, "the primality of numbers < 264 can be determined by asserting strong pseudo-primality to all prime bases ≤ 37 (12th prime)". 1018 is almost 260. 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 4.2k●5●23●64 accept rate: 15% 1 In the sequence, the 9th term is more than 3 x 1018. The 8th term is much less than 1 x 1018. 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)
 0 @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 299●3●5●10 accept rate: 0% 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)
 9 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 16.9k●49●115●225 accept rate: 11% 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) junior944★
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported
• mathemetical formulas in Latex between \$ symbol

Question tags:

×15,852
×1,191
×51
×18
×16

question asked: 14 May '13, 15:21

question was seen: 7,486 times

last updated: 27 May '13, 00:12