메뉴 건너뛰기

app

[PHP] prime number generator

박영식2010.05.25 18:42조회 수 3819댓글 0

  • 1
    • 글자 크기
어디서 찾았는지 기억이 안난다.

원리는 1부터 입력값까지의 소수를 구하는 소스이다.

풀 소스는 zip 파일로 첨부한다.

약간 변형하면 임의의 범위에서의 소수를 생성할 수 있게 할 수 있다.

범위가 너무 크면 maximum execution time을 초과해 강제 종료될 수도 있다.

 This software is developed and copyrighted by Xantus WEBdevelopment.

if ($_POST['count'] != ""){
 if (is_numeric($_POST['count'])){
  $time_start = microtime(1);
  $x = $_POST['count'];
  for($i=1;$i<=$x;$i++) {
   $f=sqrt($i);
   for($z=1;$z<=$f;$z++) {
    $y=$i%$z;
    if($y==0){
     $l++;
    }
    if($l>=2){
     break;
    }
   }
   if ($l<=1){
    echo "$i, ";
    $b++;
   }
   $l=0;
  }
  $time_end = microtime(1);
  $time = $time_end - $time_start;
  echo "<br><b>[ Found first $x prime numbers in $time seconds ]</b>";
 }
 else
 {
  echo "<br><b>Please enter a number!</b>";
 }
}
박영식 (비회원)
  • 1
    • 글자 크기
Google form trigger (by lispro06) 안드로이드 스튜디오 사용 및 웹뷰 ssl(https) 에러 (by lispro06)

댓글 달기

이전 1 2 3 4 5 6 7 8 9 10... 14다음
첨부 (1)
prime-number-calculator.zip
3.6KB / Download 24
위로