1

I have been reading a competitive programming book for one month. The book is written by one of the world finalists of our country (Bangladesh). Point to be noted, the book is written in our native language (Bengali) and that is not so popular worldwide. Because of having the content in Bengali I can't refer it here. That's why I am sorry first of all.

In Number theory chapter of that book, there are given many algorithms to test Primality. The most optimal he has shown, is "Sieve of Eratosthenes" in O(nloglogn). But he wrote one line. I am translating it. "There is a more efficient method of testing primality in O(logn). Think it yourself. And if you are undone, just google it!!"

I have googled about it.But I didn't find anything satisfactory.

Is it really possible to test the primality of a number in O(logn) ?? And if it is possible then up to which range it can be concluded ??

Kazi Ziaul Hassan
  • 280
  • 1
  • 4
  • 17
  • 3
    He probably means that once the sieve has been pre-computed, you can test for primality in O(logn). The method has been discussed [here](https://www.geeksforgeeks.org/prime-factorization-using-sieve-olog-n-multiple-queries/). – Vishnu Dasu Jun 25 '19 at 11:20
  • 1
    What you gave is not about primality test but prime factorization. @VishnuDasu – Kazi Ziaul Hassan Jun 25 '19 at 13:07
  • You can easily modify the code to determine whether the number is prime or not. One simple way is to just count the number of factors. Complexity would still be O(logn). @KaziZiaulHassan – Vishnu Dasu Jun 25 '19 at 13:44

2 Answers2

6

The statement is incorrect. For a number N, the number of digits is O(log N), so the statement means that there is an algorithm that's linear in the number of digits. The best known result is polynomial in the number of digits. (Agrawal–Kayal–Saxena primality test, Õ(logN 12). That's logN to the power of twelve, not one.

Still, Õ(logN 12) ⊂ O(N)

MSalters
  • 173,980
  • 10
  • 155
  • 350
0

Yes, Book author is correct you can do primality test of a number n which is large in 0(logn) using Miller-Rabin primality test for large numbers. You can search this test on google to know more.