Quantcast
Channel: Crunchify
Viewing all articles
Browse latest Browse all 1037

In Java How to Find If Number is Prime or not + Best way to Generate Prime number in Java

$
0
0

Check if Number is Prime or not in Java

What is Prime number?

As per definition, Number which is greater than 1 and has only 1 divider which is itself is called Prime number. Other numbers are called Composite Number. Let’s take a look: 3, 5, 7, 11, 13 are prime numbers.

Sometime back I’ve written an article on how to print fibonacci series. In this tutorial we will go over below points:

  • Two different ways to determine a prime number in Java
  • Best way to generate prime number in Java

Let’s get started:

  1. Create class CrunchifyIsPrimeAndGeneratePrime.java
  2. create crunchifyIsPrimeNumber(int) to check if number is prime?
  3. create crunchifyIsPrimeNumberMethod2(int) to check number is prime or not using diff approach
  4. crunchifyGeneratePrimeNumbers(int) generates prime number between 2 and provided number

package com.crunchify.tutorials;

/**
 * @author Crunchify.com
 * 
 */

public class CrunchifyIsPrimeAndGeneratePrime {
	public static void main(String args[]) {

		System.out.println(crunchifyIsPrimeNumber(11));
		System.out.println(crunchifyIsPrimeNumber(22));
		System.out.println(crunchifyIsPrimeNumber(37));
		System.out.println(crunchifyIsPrimeNumber(7));

		System.out.println("\n");
		System.out.println(crunchifyIsPrimeNumberMethod2(3));
		System.out.println(crunchifyIsPrimeNumberMethod2(43));
		System.out.println(crunchifyIsPrimeNumberMethod2(23));
		System.out.println(crunchifyIsPrimeNumberMethod2(88));

		// Here 120 is an upper limit
		crunchifyGeneratePrimeNumbers(120);
	}

	private static boolean crunchifyIsPrimeNumber(int crunchifyNumber) {
		System.out.println("Prime check started for number: " + crunchifyNumber);

		// Loop starts from 2
		for (int i = 2; i <= crunchifyNumber / 2; i++) {
			if (crunchifyNumber % i == 0) {
				return false;
			}
		}
		return true;
	}

	// Another way to check if Number is Prime
	public static boolean crunchifyIsPrimeNumberMethod2(int crunchifyNumber) {

		System.out.println("Prime check started for number: " + crunchifyNumber);

		boolean crunchifyPrime = true;
		int limit = (int) Math.sqrt(crunchifyNumber);

		for (int i = 2; i <= limit; i++) {
			if (crunchifyNumber % i == 0) {
				crunchifyPrime = false;
				break;
			}
		}

		return crunchifyPrime;
	}

	public static void crunchifyGeneratePrimeNumbers(int crunchifyUpperLimit) {

		System.out.println("\nGenerating all prime number between 2 to " + crunchifyUpperLimit);

		for (int i = 2; i < crunchifyUpperLimit; i++) {

			boolean crunchifyIsPrime = true;

			// check to see if the number is prime
			for (int j = 2; j < i; j++) {

				if (i % j == 0) {
					crunchifyIsPrime = false;
					break;
				}
			}
			// print the number
			if (crunchifyIsPrime)
				System.out.print(i + " ");
		}
	}

}

Output:

Prime check started for number: 11
true
Prime check started for number: 22
false
Prime check started for number: 37
true
Prime check started for number: 7
true

Prime check started for number: 3
true
Prime check started for number: 43
true
Prime check started for number: 23
true
Prime check started for number: 88
false

Generating all prime number between 2 to 120
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113

Have anything to add to this article? Please chime in and join the conversion.


The post In Java How to Find If Number is Prime or not + Best way to Generate Prime number in Java appeared first on Crunchify.
Author: App Shah


Viewing all articles
Browse latest Browse all 1037

Trending Articles