Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    Is it a prime number?

    EasyAbout 15 minFreeLearn it: Repeat useful work with loops

    The task

    A puzzle game gives bonus points for prime scores, and a score can be as large as int.MaxValue.

    Complete IsPrime(int number). Return whether number is prime: an integer greater than 1 whose only positive divisors are 1 and itself. 0 and 1 are not prime. The input is a non-negative int.

    Signature

    public static bool IsPrime(int number)

    Examples

    • 29 → true. No number from 2 to 5 divides it, and 6 × 6 is already more than 29.
    • 21 → false. 21 = 3 × 7.

    Limits: Any non-negative int, up to int.MaxValue = 2,147,483,647.

    The tests check: 0, 1, 2 and 3, an even number, an odd number with a divisor, squares of primes (49, 9,409 and 46,337 × 46,337), int.MaxValue, and the ten largest primes below 2^31 in time.

    Hints

      The solution opens by itself when all tests pass. You remember more if you try first.

      Sign in to see the solution

      Your code

      C#Solution.csLoading the editor…

      Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;

      public static class Solution
      {
          // Returns whether the number is prime.
          public static bool IsPrime(int number)
          {
              throw new NotImplementedException();
          }
      }

      Sign in to run and submit your code. It stays here while you do.