Back
Close

Prime Tester

cehsu
1,046 views

The goal today is to write a function that determines whether a number is prime.

Check if a number is prime

Write an isPrime function that takes a number as parameter and returns true if this number is prime, false if it isn't.

Reminder: A prime number:

  • Can only be divided by 1 and itself
  • Is greater than 1
Write the isPrime function
function isPrime (number) {
return false;
// For demo purpose:
/*
var start = 2;
while (start <= Math.sqrt(number)) {
if (number % start++ < 1) return false;
}
return number > 1;
*/
}
module.exports = isPrime;
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Optimize

Take a look at the Sieve of Eratosthenes. How much faster did your implementation of the sieve run? How can you account for this in terms of time complexity?

See:

https://github.com/AlgorithmsMeetup/GetAllPrimes

How about non-deterministic approaches?

See:

http://mathworld.wolfram.com/PrimalityTest.html

https://www.topcoder.com/community/data-science/data-science-tutorials/primality-testing-non-deterministic-algorithms/

Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants