Prime Number Definition with Examples in Java

A primary number is any number that can only be divided by one other than itself. Prime numbers include 3, 5, 7, 11, 17, and 23. While 0 and 1 cannot be considered prime numbers, 2 will be the only even prime number in the entire set of prime numbers.

Java 8 makes it even easier for developers to find prime numbers.

Below are the easy and useful ways to understand the prime number and its logic.

  • Identify the prime number using While Loop
  • Identify the prime number used FOR Loop.
  • Identify the Prime Number using IntStream anyMatch()
  • Identify the Prime Number using IntStream noneMatch()
  • Print the prime numbers between 1 and 100 using Loops
  • Identify and Print the Prime Numbers using Instream from the List

Let’s take a look at each one separately.

1. Identify the prime number using While Loop

public static void main(String[] args) {
PrimeNumber pm = new PrimeNumber();
System.out.println("Identify Prime Number wiht While Loop : "+pm.verifyPrimeNumberWithWhile(9));
}
private boolean verifyPrimeNumberWithWhile(int number) {
	int position = 2; //This is because 0,1 are neglected from the Prime Number Logic.
	while (position< number/2 ) {
		if (number%position==0) return false;
		position++;
	}
	return true;
}

2. Identify the prime number used FOR Loop.

public static void main(String[] args) {
PrimeNumber pm = new PrimeNumber();
System.out.println("Identify Prime Number with For Loop : "+pm.verifyPrimeNumberWithFor(9));
}
private boolean verifyPrimeNumberWithFor(int number) {
	for (int i=2;i< number/2;i++ ) {
		if (number%i==0) return false;
	}
	return true;
}


3. Identify the Prime Number using IntStream anyMatch()

public static void main(String[] args) {
PrimeNumber pm = new PrimeNumber();
System.out.println("Identify Prime Number using Instream::anyMatch() : "+pm.primeNumberWithStreamsType1(9));
}
private boolean primeNumberWithStreamsType1(int element) {
	
    return !IntStream.rangeClosed(2, element/2).anyMatch(i -> element%i == 0); 
}


4. Identify the Prime Number using IntStream noneMatch()

public static void main(String[] args) {
PrimeNumber pm = new PrimeNumber();
System.out.println("Identify Prime Number using Instream::noneMatch() :" +pm.primeNumberWithStreamsType2(9));
}
private boolean primeNumberWithStreamsType2(int element) {
    return IntStream.rangeClosed(2, element/2).noneMatch(i -> element%i == 0);
}

5. Print the prime numbers between 1 and 100 using Loops

public static void main(String[] args) {
PrimeNumber pm = new PrimeNumber();
System.out.println("Prime Numbers between 1 and 100 : "+pm.printPrimeNumbers(1,100));
}
private List<Integer> printPrimeNumbers(int startingElement, int endingElement) {
  List<Integer> primeNumbers = new LinkedList<>();
	for(int number=startingElement;number<=endingElement;number++) {	
		if (number <= 2) { primeNumbers.add(number); 
		} else {
		for (int i=2;i< number/2;i++ ) {
			if (number%i==0)  break;
			if (i+1 == number/2) primeNumbers.add(number);
		}
	}
   }
	return primeNumbers;
}


6. Identify and Print the Prime Numbers using Instream from the List

public static void main(String[] args) {
PrimeNumber pm = new PrimeNumber();
List<Integer> numbersToVerifyPrime = Arrays.asList(2,6,78,9,0,13,17,23);
System.out.println("List Values : "+numbersToVerifyPrime );
System.out.println("Print the Prime Numbers from the List : "+numbersToVerifyPrime.stream().filter(PrimeNumber::isPrimeValue).collect(Collectors.toList()));
}
public static boolean isPrimeValue(int element) {
    IntPredicate predictValue = index -> element % index == 0;
    return element > 1 && IntStream.range(2, element).noneMatch(predictValue);
}

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *