0 ratings0% found this document useful (0 votes) 7 views3 pagesChapter 23 - Identify Prime Numbers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
4
SUV NSS) SS59) 25 ESSENTIAL PROGRAMS rey
FOR INTERVIEW SUCCESS, ec eter
Statement 23: Program to identify Prime Numbers
Algorithm: {sar
1.8tep-by-Step Algorithm
© Initialize Input and Storage:
© Define an array of integers containing the numbers to be checked.
© Create an empty list (e.g,, primeNumbers) to store identified prime numbers.
2.Tterate Through Input Array:
© For each number in the input arra
= Skip the number i
3.Check for Primality:
© Define a method isPrime(int number):
jess than or equal to 1 (as prime numbers are greater than 1),
= Initialize a counter divisorCount to count the divisors of the number.
= Loop through integers from 1 to the number itself.
+ For each integer, check if it divides the number evenly (.e., number % i == 0).
+ Increment the divisorCount for each divisor found.
= IfdivisorCount equals 2 (div self), the number
le only by 1 and s prime. Return true. Otherwis
return false,
4.Store Prime Numbers:
© If isPrime(number) returns true for a number in the array, add it to the primeNumbers list.
5.Output Results:
© Print the list of prime numbers.
Key Concepts:
1.Prime Number Definition:
© A prime number is greater than 1 and divisible only by 1 and itself.
2.Loop Optimization:
© Understanding the trade-offs between readability and performance in primality checks.
3.Reusability:
© Encapsulating logic into separate methods makes code modular and easier to test.
4. ArrayList Usage:
© Using ArrayList to dynamieally store and manage prime numbers.
(S) +91 9011 176 678Program1:
package org. Linknentors;
import java.util.ArrayList;
D> pubtie class Java2s_tdentifyPrinetusber
Dv public static void main(Steing{} args) {
int] numbers = {1, 2, 3, 4 5, 6, 7, 8, 9% 48, 21, 22, 238, 14, 25, 16, 17, 18, 19,
AnrayList prinelunbers = new ArrayList<>();
for Gint num : numbers) {
e Sf (num > 2) {
if (isPrine(oum)) {
primeNuabers.add(nun)
Systen.out.printin("Prine Nusbers: " + primeNunbers);
public static boolean isPrineCint nunber) {
int divisorcount =
for (int 4 = 1; 4 <= number; ies) {
if (number % i == 0) {
divisorcount++;
return divisorCount
tm roms Output:
‘/Library/ Java/ JavavirtvatNachines /jak-22. jdk/Contents/Home/bin/java
@ Prine numbers: (2, 3, 5, 7, 11, 18, 17, 19, 23, 29)
Process finished with exit code 0
+91 9011 176 678 CER)
20, 23, 294;Program2: Optimized Approach with Square Root
Why the Square Root Approach Works?
+ Anon-prime number must have at least one factor less than or equal to its square root.
+ For example:
© For 36: Factors are (1, 86), (2, 18), (8, 12), (4, 9) (6, 6).
© Beyond the square root (6 in this case), the factor pairs repeat in reverse.
* So, ifa number has no divisors in the range 2 to sqrt(n), it is prime.
package org.Linknentors;
import java.util. ArrayList:
> public class Java23_tdentityPrimedunberwithsquareRoot {
D public static void main(stringl] args) {
intl] a= {1, 2, 3, 4 5, 6, 7% 8, 9% 10, 21, 12, 13, 14, 25, 16, 17, 18, 19, 20, 23, 29}
ArrayList prineNuabers = new ArrayList<>();
for (int nua : a) {
if CisPrime(nun)) {
prinelunbers.add(nun)
+
»
Systen.out.printUn("Prine Numbers: \n* + primeNumbers)
public static boolean isPrine(int number) {
if (number <= 1) {
return false;
»
for (int i = 2; i <= Math.sqnt(number); i++) {
if (number % 4 == 0) {
return false;
+
}
return true;
Output =
/Library/Javo/ Jevavirtusttachines/jek-22.5dk/Contents/Hone/bin/java ~jevaagent: /Applications/IntelLid IDEA CE.opp/Con
Prine Nunbers:
(2, 3, 5, 7, 42, 43, 17, 19, 23, 29)
Process finished with exit code 0
ae ee ery L:)