Version without JavaScript, read-only: workshops, quizzes and running code are not available here. Full version · Catalogue · Sign in

Sequence

Prime number

Core exercise

Write estPremier(int n) that returns true if n is a prime number.

A workshop goes here: you write and run code in it. It requires JavaScript.

Starting code

Exercices.java

public class Exercices {

    public static void main(String[] args) {
        for (int i = 1; i <= 20; i++) {
            if (estPremier(i)) {
                System.out.print(i + " ");
            }
        }
        System.out.println();
    }

    public static boolean estPremier(int n) {
        // TODO
        return false;
    }
}

The core exercise is validated in the workshop, which requires JavaScript.

Hint 1

A prime number has exactly two divisors: 1 and itself. So 0, 1 and negative numbers are not prime. It is enough to test divisors up to the square root of n (i * i <= n).