Few more challenges on ProjectEuler

This commit is contained in:
2024-06-26 13:16:36 +00:00
parent 2a2623a652
commit 27fd0bf2ea
9 changed files with 1011296 additions and 0 deletions

View File

@ -0,0 +1,30 @@
<?php
function is_prime($prime) {
if($prime < 1)
{
return false;
}
if($prime == 1)
return true;
if($prime == 2)
return true;
$sqrt = sqrt($prime);
for ($i = 3; $i <= $sqrt; $i+=2){
if ($prime%$i == 0) return false;
}
return true;
}
$high = 0;
$max_primes = 0;
for($a=-999; $a<1000; $a++) {
for($b=-999; $b<1000; $b=$b+2) {
$n=0;
while(is_prime($n*$n + $a * $n + $b)) {
$n++;
if($n > $max_primes) {
$max_primes = $n;
echo "max: $n a: $a b: $b\n";
}
}
}
}

View File

@ -0,0 +1,34 @@
<?php
define("TOTAL", 200);
$combo=0;
$sum = 200;
for ($a = 0; $a <= (TOTAL / 200); $a++) {
$sum_a = TOTAL - ($a * 200);
for ($b = 0; $b <= ($sum_a / 100); $b++) {
$sum_b = $sum_a - ($b * 100);
for ($c = 0; $c <= ($sum_b / 50); $c++) {
$sum_c = $sum_b - ($c * 50);
for ($d = 0; $d <= ($sum_c / 20); $d++) {
$sum_d = $sum_c - ($d * 20);
for ($e = 0; $e <= ($sum_d / 10); $e++) {
$sum_e = $sum_d - ($e * 10);
for ($f = 0; $f <= ($sum_e / 5); $f++) {
$sum_f = $sum_e - ($f * 5);
for ($g = 0; $g <= ($sum_e / 2); $g++) {
$sum_g = $sum_f - ($g * 2);
for ($h = 0; $h <= ($sum_g / 1); $h++) {
if ($a * 200 + $b * 100 + $c * 50 + $d * 20 + $e * 10 + $f * 5 + $g * 2 + $h == TOTAL) {
$combo++;
}
}
}
}
}
}
}
}
}
echo $combo;

View File

@ -0,0 +1,24 @@
<?php
$sum = [];
for($n = 10; $n <= 99; $n++)
{
for($d=$n+1;$d <= 99; $d++)
{
foreach([1,2,3,4,5,6,7,8,9] as $digit)
{
$ns = (int)str_replace($digit, '', $n);
$ds = (int)str_replace($digit, '', $d);
if($n != $ns && $d != $ds && $d != 0 && $ds != 0)
{
if($n/$d == $ns/$ds)
{
echo "$n/$d = $ns/$ds\n";
$sum[] = $ns/$ds;
}
}
}
}
}
echo array_product($sum).PHP_EOL;

View File

@ -0,0 +1,61 @@
<?php
function is_prime($prime) {
if($prime < 1)
return false;
if($prime == 1)
return true;
if($prime == 2)
return true;
$sqrt = sqrt($prime);
for ($i = 3; $i <= $sqrt; $i+=2){
if ($prime%$i == 0) return false;
}
return true;
}
if (($handle = fopen("primes1000000.txt", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if ($data[1] >= 1000000)
break;
//If there's an even digit (or a 5) we can instantly exclude it as when rotated it will not not be a prime.
if (
strpos((string) $data[1], "0") !== false ||
strpos((string) $data[1], "2") !== false ||
strpos((string) $data[1], "4") !== false ||
strpos((string) $data[1], "5") !== false ||
strpos((string) $data[1], "6") !== false ||
strpos((string) $data[1], "8") !== false
) {
continue;
}
$primes[] = (int) $data[1];
}
fclose($handle);
}
echo "File read\n";
$count = 1+1; //2 and 5 are primes but not included
foreach($primes as $prime)
{
//echo "Now testing $prime\n";
$newprime = $prime;
for($rotate = 1; $rotate < strlen($prime); $rotate++)
{
$newprime = substr($newprime, -1, 1).substr($newprime, 0, -1);
//echo "New prime: $newprime\n";
if(!in_array($newprime, $primes))
{
continue 2;
}
}
$count++;
echo $prime.PHP_EOL;
}
echo "Total: $count\n";

View File

@ -0,0 +1,47 @@
<?php
/**
* Triangle Tn = n*(n+1)/2
* Pentagonal Pn = n*(3n-1)/2
* Hexagonal Hn = n*(2n-1)
* T(285) = P(165) = H(143) = 40755
* Find next
*/
function Tn($n)
{
return $n*($n+1)/2;
}
function Pn($n)
{
return $n*(3*$n-1)/2;
}
function Hn($n)
{
return $n*(2*$n-1);
}
echo "Tn(285)=".Tn(285).PHP_EOL;
echo "Pn(165)=".Pn(165).PHP_EOL;
echo "Hn(143)=".Hn(143).PHP_EOL;
for($t = 285; $t < 100000; $t++)
{
$triangle[$t] = Tn($t);
}
for($p = 165; $p < 100000; $p++)
{
$pentagonal[$p] = Pn($p);
}
for($h = 143; $h < 100000; $h++)
{
$hexagonal[$h] = Hn($h);
}
$result = array_intersect($triangle, $pentagonal, $hexagonal);
print_r($result);