Primzahlen in PHP

王林
Freigeben: 2024-08-29 13:13:40
Original
241 Leute haben es durchsucht

Primzahlen in PHP sind im Vergleich zu anderen Zahlenreihen/-folgen immer eindeutig. Was auch immer die Programmiersprache sein mag, die Logik ist für alle Arten von Programmiersprachen, die es bisher in der Internetwelt rund um den Globus gibt, gleich. Lediglich die Syntax der Programmiersprache unterscheidet sich.

WERBUNG Beliebter Kurs in dieser Kategorie PHP-ENTWICKLER - Spezialisierung | 8-Kurs-Reihe | 3 Probetests

Starten Sie Ihren kostenlosen Softwareentwicklungskurs

Webentwicklung, Programmiersprachen, Softwaretests und andere

Die Logik hinter einer Primzahl

Primzahlen/Primzahlen sind die einzigen Zahlen, die nur durch die 1 und die Zahl selbst teilbar sind. Die Reihe/Folge der Primzahlen umfasst 2, 3, 5, 7, 11, 13, 17 und so weiter. Nummer 2 in der genannten Folge ist eine gerade Primzahl und auch eine natürliche Zahl nach Nummer 1. Nummer 1 und 0, die vor Nummer 2 stehen, sind überhaupt keine Primzahlen. Um dies also zunächst im Programm auszuführen, müssen wir die Schleife durchlaufen, um jede einzelne Zahl mit jeder einzelnen Zahl, die mit 2 beginnt, bis zur erforderlichen Zahl selbst zu überprüfen. Manchmal unterscheidet sich die Logik bei Programmiersprachen von den allgemeinen Logikbegriffen.

Wie finde ich Primzahlen in PHP mit verschiedenen Methoden?

(For-Schleife, While, do-while, foreach usw.):

Beispiel #1

Dies ist ein Beispiel für eine for-Schleife, um unsere erforderliche Anzahl an Primzahlen anzuzeigen: 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
}
?>
Nach dem Login kopieren

Ausgabe:

Primzahlen in PHP

Beispiel #2

Dies ist ein Beispiel für Primzahlen, bei dem die ersten 15 Primzahlen gedruckt werden. Hier wird eine While-Schleife verwendet.

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
}
?>
Nach dem Login kopieren

Ausgabe:

Primzahlen in PHP

Beispiel #3

DO WHILE-Schleife Primzahlprogrammbeispiel mit Syntax und der unten aufgeführten Ausgabe.

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
?>
Nach dem Login kopieren

Ausgabe:

Primzahlen in PHP

Beispiel #4

FÜR JEDES Programm zum Drucken von Primzahlen aus der Liste des Arrays 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. " ,";
}
}
?>
Nach dem Login kopieren

Ausgabe:

Primzahlen in PHP

Beispiel #5

Dies ist das PHP-Programm, um zu prüfen, ob die eingegebene Zahl eine Primzahl oder eine Nicht-Primzahl ist.

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";
?>
Nach dem Login kopieren

Ausgabe:

Primzahlen in PHP

Beispiel #6

Dies ist ein Beispiel für das Drucken von Primzahlen, die unter 123 liegen. Bitte überprüfen Sie die Syntax und Ausgabe des Programms unten

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;
}
?>
Nach dem Login kopieren

Ausgabe:

Primzahlen in PHP

Beispiel #7

Beispiel zum Drucken von Primzahlen/Werten, die kleiner als die Zahl „25“ sind

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
?>
Nach dem Login kopieren

Ausgabe:

Primzahlen in PHP

Das obige ist der detaillierte Inhalt vonPrimzahlen in PHP. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Verwandte Etiketten:
php
Quelle:php
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!