Utility to determine whether some number is prime. - Java java.lang

Java examples for java.lang:int prime

Description

Utility to determine whether some number is prime.

Demo Code


//package com.java2s;

public class Main {
    /**/*from  ww  w.  j  ava2s .  c  o m*/
     * Utility to determine whether some number is prime.
     *
     * @param n the number to check
     * @return true if <code>n</code> is prime, false if composite
     */
    public static boolean isPrime(int n) {
        // Scan for non-trivial divisors of n.
        for (int k = 2; k < n; k++) {
            if ((n % k) == 0) // if anything divides us, we're composite
                return false;
        }
        return true;
    }
}

Related Tutorials