WITMATH - Editorial






Primality Testing


Given some N, find the i for which φ(i)/i is the largest among all 2≤i≤N.

φ(i) is the Euler Totient Function.


φ(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.


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
 4. b_low = b % 109
 5. b_high = b / 109
 7. result = (a_high * b_high) % n
 9. repeat 9 times
10.     result = (result * 10) % n
12. result = (result + a_low*b_high + b_low*a_high) % n
14. repeat 9 times
15.     result = (result * 10) % n
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 will be updated soon.


Can be found here.


This is one of such cases when Java beats C/C++ O:-)

My AC solution - http://ww2.codechef.com/viewsolution/2081192


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 .


@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.

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).

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:


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;
            if(x>c) x=x%c;
        if(y>c) y=y%c;
    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;

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.


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 = ((q1 - 1) / q1)((q2 - 1) / q2)…((qk - 1) / qk)

We know that

((a - 1) / a) ≤ ((b - 1) / b) if a ≤ b


φ©/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


φ©/C < ((P - 1) / P)


φ©/C < φ§/P

Hence the answer will always be the largest prime less than (or equal to) N.

1 Like

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 :frowning:

Please chk…

links available under recent questions on home page

I did the same thing: http://ww2.codechef.com/viewsolution/2102038
BigInteger rocks!

1 Like

In my settings I have notifications enabled:

e-mail notifications

e-mail notifications settings


sorry it should be (a%MOD*b%MOD)%MOD

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

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.

1 Like

Setter and Tester’s solution links are not working


same here hehe… one of the shortest solutions I have ever written. Just one while loop with only one statement

1 Like

Oops! I didn’t check out the numbers in the sequence. Somehow, just managed to read the comment there!!

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. Can some bosy plz tell how to prove it?

tester’s solution…classic