You are not logged in. Please login at to post your questions!


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 question is marked "community wiki".

asked 14 May '13, 15:21

gamabunta's gravatar image

accept rate: 14%

edited 16 May '13, 12:47

admin's gravatar image

0★admin ♦♦


Setter and Tester's solution links are not working

(14 May '13, 21:27) kuldeepfouzdar3★

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) timepass1232★

tester's solution...classic

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

Setter's solution not yet uploaded ?

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

Admin can u plz chk 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

swapniel99's gravatar image

accept rate: 0%


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


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


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


φ(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's gravatar image

accept rate: 14%

edited 17 May '13, 13:40

@gamabunta : please provide an explanation to my question which i had also posted before.

(17 May '13, 21:32) aayushagarwal2★

@gamabunta : thanx

(18 May '13, 17:26) timepass1232★

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

timepass123's gravatar image

accept rate: 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:


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;

answered 15 May '13, 19:39

aayushagarwal's gravatar image

accept rate: 0%

edited 18 May '13, 01:17

gamabunta's gravatar image


I too used (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

tijoforyou's gravatar image

accept rate: 15%


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) gamabunta1★

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

(15 May '13, 10:34) tijoforyou2★

@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

aayushagarwal's gravatar image

accept rate: 0%

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

(14 May '13, 16:41) aayushagarwal2★

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) deepankarak5★

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

vineetpaliwal's gravatar image

accept rate: 12%

links available under recent questions on home page

(14 May '13, 15:48) rishul_nsit4★

In my settings I have notifications enabled:

e-mail notifications

e-mail notifications settings

(14 May '13, 15:53) betlista ♦♦3★

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

My AC solution -


answered 14 May '13, 15:40

betlista's gravatar image

3★betlista ♦♦
accept rate: 11%


I did the same thing: BigInteger rocks!

(14 May '13, 15:51) rishul_nsit4★

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

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • 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:


question asked: 14 May '13, 15:21

question was seen: 7,486 times

last updated: 27 May '13, 00:12