Selamlar,
http://projecteuler.net/ sitesindeki problemlerden yedi numaralı problemle devam ediyoruz.Problemimiz yine asal sayılar ile ilgili.
Problemin orijinali.
Bize altıncı asal sayının 13 olduğunu vermiş ve bizden 10001. asal sayının ne olduğunu bulmamızı istiyor.Bu problem için yine PHP ve Perl kodlarını vereceğim. Önceki yazılarımda asal sayının nasıl bulunacağı hakkında küçük bir hatırlatma yaptığım yazıma buradan ulaşabilirsiniz.
Gelelim direkt kodlara 😀
PHP Kodu :
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <!-- Ümit ŞEN izmir bornova evka-4 18 Haziran 2013 17:53 Problem 10001st prime Problem 7 By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10 001st prime number? --> <h2>Euler Problem 7-10001st prime</h2> <form action="" method="POST"> <input type="text" name="sayi"> <input type="submit" value="Hesapla" name="hesapla"> </form> <?php if (isset($_POST['hesapla']) && !empty($_POST['sayi'])) { $sayi=(int)$_POST['sayi']; echo "Girilen Sayı : <font color='red'>".$sayi."</font><br>"; //------------------------------------------------------------------------------------- //girilen sayı asal mı değil mi test etme function asalTest($n) { $asal=1; $s=(int)$n; //kontrol 1 if ($s < 2) { $asal=0; } // kontrol 2 if ($s==2) { $asal=1; } //Asal hesabı $kok=(int)($s^0.5+1); for ($i=3; $i < $kok; $i+=1) { if ($s % $i==0) { $asal=0; } } return $asal; } //--------------------------------------------------------------------------------------------- $say=1; $n=0; while (1==1) { if (asalTest($n)) { $say ++; } if ($say==$sayi+1) { break; } $n++; } echo "İstenen Sonuç : $n<br>"; }else { echo "Lütfen sayı giriniz...<br>"; } ?>Perl Kodu :
#!/usr/bin/perl #Ümit ŞEN #izmir bornova evka-4 #18 Haziran 2013 17:53 #Problem #10001st prime #Problem 7 #By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. #What is the 10 001st prime number? $sayi=10001; sub asalTest { $asal=1; $s=int($_[0]); #kontrol 1 if ($s<2) { $asal=0; } #kontrol 2 if ($s==2) { $asal=1; } #asallar $kok=int(($s**0.5)+1); for ($i=2; $i<$kok; $i+=1) { if ($s % $i == 0) { $asal=0; } } return $asal; } $say=1; $n=0; while (1==1) { $son=asalTest($n); if ($son==1) { $say++; } if ($say==$sayi+1) { print("$n\n"); exit; } $n++; }Bu problemde bizden istenen sonuç : 104743
Perl kodlarının nasıl çalıştığına dair program videosu : burada
Teşekkürler.