Home  >  Article  >  Backend Development  >  Prime Numbers in PHP

Prime Numbers in PHP

王林
王林Original
2024-08-29 13:13:40243browse

Prime Numbers in PHP are always unique in nature when compared to other series/sequence of numbers. Whatever the programming language might be, logic is the same for all type of programming languages which are in the internet world up-to-now around the Globe. Only the Syntax of the Programming Language differs.

ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

The Logic behind a Prime number

Prime Number/Prime numbers are the only numbers that are only divisible using the 1 and the number which is itself. Series/ Sequence of the Prime numbers includes 2, 3, 5, 7, 11, 13, 17 and so on. Number 2 in the mentioned sequence is an even prime number and it is also a natural number after number 1. Number 1, 0 which are before the number 2 are not prime numbers at all. So in order to execute this in the program first, we have to process the loop to check each and every number with each and every number which starts with 2 to the required number itself. Sometimes Logic may be different when it comes to Programming Languages when compared with the General Logic Terms.

How to find Prime numbers in PHP using various methods?

(For loop, While, do-while, foreach, etc..,):

Example #1

This is an example of for loop to show our required number of prime numbers: FOR LOOP

Syntax:

<?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
}
?>

Output:

Prime Numbers in PHP

Example #2

This is an example of Prime numbers which will print the first 15 prime numbers. While loop is used here.

Syntax:

<?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
}
?>

Output:

Prime Numbers in PHP

Example #3

DO WHILE loop Prime Number Program Example with syntax and the output of it listed below.

Syntax:

<?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
?>

Output:

Prime Numbers in PHP

Example #4

FOR EACH program to print Primes numbers from the list of the array nums_list

Syntax:

<?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. " ,";
}
}
?>

Output:

Prime Numbers in PHP

Example #5

This is the PHP program to check whether the number which is given input is a prime number or non-prime number.

Syntax:

<?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";
?>

Output:

Prime Numbers in PHP

Example #6

This is an example of printing prime numbers which are below 123. Kindly check the syntax and output of the program below

Syntax:

<?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;
}
?>

Output:

Prime Numbers in PHP

Example #7

Example to Print Prime Numbers/values which are less than the number “25”

Syntax:

<?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
?>

Output:

Prime Numbers in PHP

The above is the detailed content of Prime Numbers in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Palindrome in PHPNext article:Palindrome in PHP