Last active
November 24, 2017 21:16
-
-
Save savasdersimcelik/843b7892a188a1f01638ff8508709efe to your computer and use it in GitHub Desktop.
PHP Chen Sayıları Algoritması
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Author : Savaş Dersim ÇELİK | |
* Version: 1.0.0 | |
* Web: http://webinyo.com | |
* Mail: savasdersimcelik@gmail.com | |
* Description : PHP Chen Sayıları Algoritması | |
*/ | |
set_time_limit(0); | |
function asal($sayi) | |
{ | |
$kok=sqrt($sayi); | |
if ($sayi == 1 || $sayi == 0) { | |
return 0; | |
} | |
for ($i=2; $i<=$kok; $i++){ | |
if($sayi % $i==0) return 0; | |
} | |
return 1; | |
} | |
function asalCarpanlar($param) | |
{ | |
$asallar = array(); | |
for ($i=$param; $i >= 1; $i--) { | |
if ($param%$i==0) { | |
if (asal($i)) { | |
array_push($asallar, $i); | |
} | |
} | |
} | |
$s = 0; | |
if (count($asallar) == 2) { | |
foreach ($asallar as $key => $value) { | |
if (asal($value)) { | |
$s++; | |
} | |
} | |
}else{ | |
return false; | |
} | |
if ($s == 2 && ($asallar[0]*$asallar[1] == $param)) { | |
return true; | |
}else{ | |
return false; | |
} | |
} | |
$sayac = 0; | |
for ($i=0; $i < 100000; $i++) { | |
$acarpanlar = asalCarpanlar($i+2); | |
if (asal($i) && (asal($i+2) || $acarpanlar)) { | |
$sayac++; | |
} | |
} | |
echo $sayac; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment