← Back to challenges

Is the Number a Prime? (with a twist)

JavaScriptHardmathvalidation

Instructions

Write a function that takes a number and returns true if it's a prime; false otherwise. The number can be 2^64-1 (2 to the power of 63, not XOR). With the standard technique it would be O(2^64-1), which is much too large for the 10 second time limit imposed by Innokodakademija.

Sieve of Eratosthenes

Examples

prime(7) ➞ true

prime(56963) ➞ true

prime(5151512515524) ➞ false

Notes

A "prime" number is a number that can only be divided by itself and 1 (upon division the result is a whole number).

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Odds vs. Evens