ในบทความนี้ เราจะเข้าใจวิธีการแสดงจำนวนเฉพาะระหว่างสองช่วง จำนวนเฉพาะคือจำนวนพิเศษที่มีตัวประกอบเพียงสองตัวคือ 1 และตัวมันเอง และไม่สามารถหารด้วยหมายเลขอื่นได้
ตัวเลขเป็นจำนวนเฉพาะหากมีตัวประกอบเพียงตัวเดียวคือ 1 กับตัวมันเอง 11 เป็นจำนวนเฉพาะ ตัวประกอบคือ 1 และ 11 เอง ตัวอย่างของจำนวนเฉพาะ ได้แก่ 2, 3, 5, 7, 11, 13 เป็นต้น 2 เป็นจำนวนเฉพาะคู่เดียว จำนวนเฉพาะอื่นๆ ทั้งหมดเป็นเลขคี่
ด้านล่างนี้เป็นการสาธิตสิ่งเดียวกัน -
ป้อนข้อมูล
สมมติว่าข้อมูลที่เราป้อนคือ −
Starting number : 1 Ending number : 75
ผลผลิต
ผลลัพธ์ที่ต้องการจะเป็น −
The prime numbers between the interval 1 and 75 are: 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73
อัลกอริทึม
Step 1 - START Step 2 - Declare values namely Step 3 - Read the required values from the user/ define the values Step 4 - Run a while loop between the lower number and the higher number. Step 5 - Run a for loop, iterate over each number between the intervals and check if the number is divisible by any of its lower numbers except 1. Store the values. Step 6 - Display the result Step 7 - Stop
ตัวอย่างที่ 1
ที่นี่ ผู้ใช้ป้อนอินพุตตามข้อความแจ้ง คุณสามารถลองใช้ตัวอย่างนี้ในเครื่องมือกราวด์ของเรา
.
import java.util.Scanner;
public class PrimeNumber {
public static void main(String[] args) {
int my_high, my_low, i;
boolean my_temp;
System.out.println("Required packages have been imported");
Scanner my_scanner = new Scanner(System.in);
System.out.println("A reader object has been defined ");
System.out.print("Enter the starting number : ");
my_low = my_scanner.nextInt();
System.out.print("Enter an ending Number: ");
my_high = my_scanner.nextInt();
System.out.println("The prime numbers between the interval " + my_low + " and " + my_high + " are:");
while (my_low < my_high) {
my_temp = false;
for(i = 2; i <= my_low/2; ++i) {
if(my_low % i == 0) {
my_temp = true;
break;
}
}
if (!my_temp && my_low != 0 && my_low != 1)
System.out.print(my_low + " ");
++my_low;
}
}
} ผลลัพธ์
Required packages have been imported A reader object has been defined Enter the starting number : 1 Enter the ending number : 75 The prime numbers between the interval 1 and 75 are: 1 2 5 3 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73
ตัวอย่างที่ 2
ในที่นี้ มีการกำหนดจำนวนเต็มก่อนหน้านี้ และเข้าถึงและแสดงค่าบนคอนโซล
public class PrimeNumber {
public static void main(String[] args) {
int my_high, my_low, i;
boolean my_temp;
my_low = 1;
my_high = 75;
System.out.println("The starting and ending numbers are defined as " + my_low + " and " + my_high);
System.out.println("The prime numbers between the interval " + my_low + " and " + my_high + " are:");
while (my_low < my_high) {
my_temp = false;
for(i = 2; i <= my_low/2; ++i) {
if(my_low % i == 0) {
my_temp = true;
break;
}
}
if (!my_temp && my_low != 0 && my_low != 1)
System.out.print(my_low + " ");
++my_low;
}
}
} ผลลัพธ์
The starting and ending numbers are defined as 1 and 75 The prime numbers between the interval 1 and 75 are: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73