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

Vite + Vue + Bun on Rails

Vue.js is one of frontend frameworks gaining popularity among rapidly emerging JavaScript technologies. The combination of Vue.js and Rails is becoming more popular as well, however, Vue.js development on Rails is not so straightforward. The reason would be that Vue.js relies on Vite for a development environment such as HMR (Hot Module Replacement) and bundling.

Bun + React on Rails

In the frontend world, new technologies keep emerging rapidly these years. Still, React is a well-established and very popular frontend framework, Vue.js, Svelte, Astro and more frameworks are gaining popularity. Not just the frameworks, tools for a transpiler/bundler or sort are also under a rapid development. In JavaScript domain, esbuild, rollup, vite and some more are out.

Ruby on Rails Secrets Management

A web application needs various kinds of values, params and etc which should not be revealed to the public, say GitHub repo. For example, API keys, tokens, passwords, and endpoints, all those should be kept secret. Another important factor is that such secrets should be shared among the team members. Additionally, all those secrets should come along with the deployment.