与其他数字系列/序列相比,PHP 中的素数本质上始终是唯一的。无论编程语言是什么,迄今为止全球互联网世界中所有类型的编程语言的逻辑都是相同的。只是编程语言的语法不同。
广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
质数/质数是唯一只能用 1 和它本身整除的数字。素数的系列/序列包括 2、3、5、7、11、13、17 等。上述序列中的数字2是偶素数,也是数字1之后的自然数。数字2之前的数字1、0根本不是素数。因此,为了首先在程序中执行此操作,我们必须处理循环以检查每个以 2 开头的数字到所需数字本身。有时,与通用逻辑术语相比,编程语言的逻辑可能会有所不同。
(For 循环、While、do-while、foreach 等...,):
这是一个 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 } ?>
输出:
这是质数的示例,它将打印前 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 } ?>
输出:
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 ?>
输出:
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 程序,用于检查输入的数字是素数还是非素数。
语法:
<?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"; ?>
输出:
这是打印 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; } ?>
输出:
打印小于数字“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中文网其他相关文章!