java

checkPrime()

Parameters: int number

The number to check for primality

Returns: boolean indicating if the number is prime

The checkPrime function in Java determines if a given number is a prime number, these are numbers that have only two distinct positive divisors: 1 and the number itself.

variables
conditional statements
loops
Medium dificulty

Creating a Prime Number Checker in Java

Hello fellow programmer! Today's blog post will guide you step by step on how to code a function dedicated to check if a number is a prime number or not using Java. This well-documented guide will surely enhance your knowledge in function programming. As a fellow coder, I find it important to understand, learn and implement. Let's dive into the world of prime numbers using Java!

Step 1:

To start, let's define a function checkPrime() which takes in an integer num. This function is meant to check if num is a prime number or not.

public static boolean checkPrime(int num) {

}

Step 2:

Prime numbers are greater than 1, so if the number is less than 2, we can immediately return false.

public static boolean checkPrime(int num) {
    if (num < 2) {
        return false;
    }
}

Step 3:

Next, we can check if the number is divisible by any numbers less than it (besides 1), starting from 2, up until the square root of num (as any factor larger than this square root would have a counterpart smaller than the square root).

public static boolean checkPrime(int num) {
    if (num < 2) {
        return false;
    }
    for (int i = 2; i*i <= num; i++) {

    }
}

Step 4:

If num is divisible by any i, then num is not a prime number, and we should return false. Otherwise, if the loop finishes, then num must be prime, and we can return true.

public static boolean checkPrime(int num) {
    if (num < 2) {
        return false;
    }
    for (int i = 2; i*i <= num; i++) {
        if (num % i == 0) {
            return false;
        }
    }
    return true;
}

Step 5:

Our function checkPrime() is complete. By testing this function with various integers num, we can tell if each num is a prime number or not.

public class Main {
    public static boolean checkPrime(int num) {
        if (num < 2) {
            return false;
        }
        for (int i = 2; i*i <= num; i++) {
            if (num % i == 0) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(checkPrime(29)); // true
        System.out.println(checkPrime(15)); // false
    }
}

Learn function in:

Prime Number Check

Determines if a number is prime

Learn more

Mathematical principle

The checkPrime function applies the mathematical principle concerning prime numbers. In mathematics, a `prime number` is a natural number greater than 1 that is not a product of two smaller natural numbers. The function uses a loop to check if the given number can be divided evenly by numbers less than it and greater than 1. If it can, it is not a prime number.

Learn more