首頁  >  文章  >  後端開發  >  PHP 中的質數

PHP 中的質數

王林
王林原創
2024-08-29 13:13:40244瀏覽

與其他數字系列/序列相比,PHP 中的質數本質上始終是唯一的。無論程式語言是什麼,迄今為止全球互聯網世界中所有類型的程式語言的邏輯都是相同的。只是程式語言的語法不同。

廣告 該類別中的熱門課程 PHP 開發人員 - 專業化 | 8 門課程系列 | 3次模擬測驗

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

素數背後的邏輯

質數/質數是唯一隻能用 1 和它本身整除的數字。質數的系列/序列包括 2、3、5、7、11、13、17 等。上述序列中的數字2是偶素數,也是數字1之後的自然數。數字2之前的數字1、0根本不是質數。因此,為了首先在程式中執行此操作,我們必須處理循環以檢查每個以 2 開頭的數字到所需數字本身。有時,與通用邏輯術語相比,程式語言的邏輯可能會有所不同。

如何在 PHP 中使用各種方法找出質數?

(For 迴圈、While、do-while、foreach 等...,):

範例#1

這是一個 for 迴圈的範例,用來顯示我們所需的素數數量:FOR LOOP

文法:

<?php
//For loop prime numbers program with only initialization and incrementation parameters
$count1 = 0;
//Number 0 is assigned to the variable count1
$number1 = 2;
//Number 2 is assigned to the variable number1
for($count1=0;$count1 < 22; )
//For loop with the condition to print only first 22 prime numbers at first
{
$div_count1=0;  //Number 0 is assigned to the variable div_count1 variable
for ( $i1=1; $i1<=$number1; $i1++)
//For loop with the initialization i1 =1 , condition - i1<=number1 and with invrement i1=i1+1
{
if (($number1%$i1)==0)
//Condition if number1 divides with changing i1 values and leaves remainder = 0 then the following below conditions will run
{
$div_count1++;  //assigning div_count1 = div_count1+1
}
}
if ($div_count1<3)// Condition ( div_count1 < 3 ) to proceed the programme
{
$b=$count1+1;//adding 1 to the count1 variable
echo $b." Prime Number:".$number1." , "; //echo to print the prime numbers as we need
echo "</br>"; // line break statement
$count1=$count1+1;  //incrementing the count1 variable's value
}
$number1=$number1+1;
//incrementing the number1 variable's value
}
?>

輸出:

PHP 中的質數

範例#2

這是質數的範例,它將列印前 15 個質數。這裡使用了While循環。

文法:

<?php
$count1 = 0;
//Number 0 is assigned to the variable count1
$number1 = 2;
//Number 2 is assigned to the variable number1
while ($count1 < 15 )
//While loop with the condition ( count1 < 15 ) to break the loop to print first 15 prime numbers ( series of prime numbers from 2 to 47 , )
{
$div_count1=0;
//Number 0 is assigned to the variable div_count1
for ( $i=1; $i<=$number1; $i++)
//For loop with the initialization i =1 , condition - i<=number1 and with invrement i=i+1
{
if (($number1%$i)==0)
//Condition if number1 divides with changing i values and leaves remainder = 0 then the following below conditions will run
{
$div_count1++;
//assigning div_count1 = div_count1+1
}
}
if ($div_count1<3)
// Condition ( div_count1 < 3 ) to proceed the programme
{
echo $number1." , ";
//Printing the Prime number and then , will be printed
$count1=$count1+1;
//assigning variable count1 = count1 + 1
}
$number1=$number1+1;
//assigning variable number1 = number1 +1
}
?>

輸出:

PHP 中的質數

範例#3

DO WHILE 迴圈質數程式範例,其語法及其輸出如下所列。

文法:

<?php
//DO WHILE PHP program
$count12 = 0 ;
$number12 = 2 ;
do
{
$div_count12=0;
for ( $i=1;$i<=$number12;$i++) //For loop to check each number for prime numbers in the natural number list
{
if (($number12%$i)==0)
{
$div_count12++;
}
}
if ($div_count12<3)
{
echo $number12." , ";
$count12=$count12+1;
}
$number12=$number12+1;
}while ($count12 <202 ); //while loop to print 202 prime numbers
?>

輸出:

PHP 中的質數

範例#4

FOR EACH 程式從陣列 nums_list 中列印質數

文法:

<?php
//Program to Print Prime numbers from the list of arrays using foreach()
$nums_list = array( 10, 11, 13, 12 , 18, 21, 22, 23, 26, 28, 27, 29);
foreach($nums_list as $prima){
//foreach() function her will list the array values into prima variable
//I(Pavan Kumar Sake) mentioned conditions below to check whether the prime numbers are available in the above mentioned array or not. If yes then prime numbers from the array list will be printed.
if($prima%2==0){
if($prima%3==0){
if($prima%5==0){
if($prima%7==0){
break;
}
}
}
}
else{
echo $prima. " ,";
}
}
?>

輸出:

PHP 中的質數

範例#5

這是一個 PHP 程序,用來檢查輸入的數字是素數還是非素數。

文法:

<?php
function IsPrime1($n1)
//Here  defining a IsPrime() function with the parameter n variable
{
for($x1=2; $x1<$n1; $x1++)
//For loop to check each number to know the prime numbers
{
if($n1 %$x1 ==0) // if n divides by x values i.e., from the number 2 to the number itself (natural number series)
{
return 0;
//returns 0 value if the above condition becomes true value
}
}
return 1;
//returns value 1 if the above IF condition fails i mean remainder is not 0 , it will be 1 so is the prime number
}
$a1 = IsPrime1(3); // assigning a prime number to n variable in the function by calling it and to check
if ($a1==0) // if the variable gives 0 as output it will be declared as non prime number
echo 'This is not a Prime Number.....'."\n";
else
// If the a variable value is non zero then it will be declared as a prime number
echo 'This is a Prime Number..'."\n";
?>

輸出:

PHP 中的質數

範例#6

這是列印 123 以下質數的範例。請檢查下面程式的語法和輸出

文法:

<?php
$number2 = 2 ;
while ($number2 < 123 )
{
$div_count2=0;
for ( $i=1;$i<=$number2;$i++)
{
if (($number2%$i)==0)
{
$div_count2++;
}
}
if ($div_count2<3)
{
echo $number2." , ";
}
$number2=$number2+1;
}
?>

輸出:

PHP 中的質數

範例#7

列印小於數字「25」的素數/值的範例

文法:

<?php
//List of Prime Numbers which are below 25
function primea1($n1){
for($k=1;$k<=$n1;$k++){  //To check prime values/numbers
$countera1 = 0;
for($l=1;$l<=$k;$l++){ //for accessing all divisible factors
if($k % $l==0){
$countera1++;
}
}
//prime requires/follows 2 rules/suggestions ( it is either divisible by 1 and then by itself)
if($countera1==2){
print $k." is a Prime Number <br/>";
}
}
}
primea1(25);  //finds the prime numbers from 1-25
?>

輸出:

PHP 中的質數

以上是PHP 中的質數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
上一篇:PHP 中的回文下一篇:PHP 中的回文