Prime Check of a Big Number

Published: Sep 5, 2019

“Given an integer n, find whether n is a prime number or not” – This is a typical algorithm question. If the given number is small, repeating a division from 2 to n gives the answer.

A better solution would be to stop looping at square root of n. Suppose the given number n is not a prime number (composite number), the n has two numbers a, b where n = ab. From the equation, both a and b should be equal to or less than square root of n. So, if the factor of n exists, the factor should be the equal to or less than square root of n. Checking the number up to square root of n is enough to find n is prime or not.

However, looping over up to square root of n runs slow still once the given number becomes bigger. Well-known faster algorithm is the Sieve of Eratosthenes (Wikipedia). The algorithm starts from a boolean array of size n initialized by true value. The iteration goes from 2 to n while crossing out all multiples of 2, then 3, 5, … (set false to all multiples of 2, 3, 5…). In the end, if the last array element is true, the given number n is confirmed the prime number.

The problem of the Sieve of Eratosthenes is a memory consumption. HackerRank problem has teasing test cases of 100000003 and 1000000007. Those numbers easily cause memory out of error. The naive looping approach ends up in a timeout failure.

Combination of Sieve and Looping

There should be plenty of solutions to find a given big number is a prime or not. What came up in my mind is a combination of the sieve and looping over. The first step is to find all prime numbers up to square root of the given number using the sieve. The second step is to loop over the division by prime numbers found in the first step. This solution has the time complexity of O(square root of n), which made all HackerRank test cases pass. The solution would be a good entry here as my memo.

Latest Posts

Application Development by Rails Action Cable

The previous two blog posts introduced WebSocket and how to implement a WebSocket application on Ruby on Rails. This blog post digs deeper. It is a memo on creating a more realistic application by Action Cable.

Real-time App on Rails by Action Cable

The previous blog post, WebSocket on Rails by Action Cable, focused on WebSocket as a protocol. As in the previous post, by default, Rails app responds to WebSocket connection requests without any hassle. However, other than connecting and sending ping frames, it doesn’t do anything. This blog post focuses on an application side and explains how we can create a full-duplex, bidirectional app.

WebSocket on Rails by Action Cable

In the web application domain, we hear some protocol names. Absolutely, HTTP or HTTPS is the most famous protocol that all web developers know. Although there’s a mechanism of Keep-Alive, a single request/response sequence with a single client/server is all done by HTTP. The client initiates the HTTP request to the server. Once the client receives the HTTP response from the server, communication finishes. As far as HTTP is used, the server just waits and waits. Only when the request comes in, the server can send back some data to the client. This communication style is surprisingly capable of doing many things, so most web applications are satisfied with HTTP.