added naive impletation for project euler problem 87 in php
This commit is contained in:
parent
c4c0ebfb4d
commit
230faba608
2 changed files with 71 additions and 0 deletions
35
2017-04-11/ikselven_euler87-foreach.php
Executable file
35
2017-04-11/ikselven_euler87-foreach.php
Executable file
|
@ -0,0 +1,35 @@
|
|||
#!/usr/bin/php
|
||||
<?php
|
||||
define("MAX", 5*(10**7));
|
||||
|
||||
$nextPrime = 2;
|
||||
$primes = array($nextPrime);
|
||||
while ($nextPrime < sqrt(MAX)) {
|
||||
$nextPrime = gmp_intval(gmp_nextprime($nextPrime));
|
||||
$primes[] = $nextPrime;
|
||||
}
|
||||
|
||||
$sums = array();
|
||||
foreach($primes as $outer) {
|
||||
$fourth = $outer ** 4;
|
||||
if ($fourth >= MAX) {
|
||||
break;
|
||||
}
|
||||
|
||||
foreach($primes as $middle) {
|
||||
$cube = $fourth + $middle ** 3;
|
||||
if ($cube >= MAX) {
|
||||
break;
|
||||
}
|
||||
|
||||
foreach($primes as $inner) {
|
||||
$sum = $cube + $inner ** 2;
|
||||
if ($sum >= MAX) {
|
||||
break;
|
||||
}
|
||||
$sums[] = $sum;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
echo "Found ".count(array_unique($sums))." numbers\n";
|
36
2017-04-11/ikselven_euler87.php
Executable file
36
2017-04-11/ikselven_euler87.php
Executable file
|
@ -0,0 +1,36 @@
|
|||
#!/usr/bin/php
|
||||
<?php
|
||||
define("MAX", 5*(10**7));
|
||||
|
||||
$nextPrime = 2;
|
||||
$primes = array($nextPrime);
|
||||
while ($nextPrime < sqrt(MAX)) {
|
||||
$nextPrime = gmp_intval(gmp_nextprime($nextPrime));
|
||||
$primes[] = $nextPrime;
|
||||
}
|
||||
|
||||
$sums = array();
|
||||
$primeCount = count($primes);
|
||||
for($outer = 0; $outer < $primeCount; $outer++) {
|
||||
$fourth = $primes[$outer] ** 4;
|
||||
if ($fourth >= MAX) {
|
||||
break;
|
||||
}
|
||||
|
||||
for($middle = 0; $middle < $primeCount; $middle++) {
|
||||
$cube = $fourth + $primes[$middle] ** 3;
|
||||
if ($cube >= MAX) {
|
||||
break;
|
||||
}
|
||||
|
||||
for($inner = 0; $inner < $primeCount; $inner++) {
|
||||
$sum = $cube + $primes[$inner] ** 2;
|
||||
if ($sum >= MAX) {
|
||||
break;
|
||||
}
|
||||
$sums[] = $sum;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
echo "Found ".count(array_unique($sums))." numbers\n";
|
Reference in a new issue