PHP의 소수

王林
풀어 주다: 2024-08-29 13:13:40
원래의
242명이 탐색했습니다.

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

각 프로그램에서 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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
php
원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!