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);

100
primes100.txt Normal file
View File

@ -0,0 +1,100 @@
1, 2, 2
2, 3, 1
3, 5, 2
4, 7, 2
5, 11, 4
6, 13, 2
7, 17, 4
8, 19, 2
9, 23, 4
10, 29, 6
11, 31, 2
12, 37, 6
13, 41, 4
14, 43, 2
15, 47, 4
16, 53, 6
17, 59, 6
18, 61, 2
19, 67, 6
20, 71, 4
21, 73, 2
22, 79, 6
23, 83, 4
24, 89, 6
25, 97, 8
26, 101, 4
27, 103, 2
28, 107, 4
29, 109, 2
30, 113, 4
31, 127, 14
32, 131, 4
33, 137, 6
34, 139, 2
35, 149, 10
36, 151, 2
37, 157, 6
38, 163, 6
39, 167, 4
40, 173, 6
41, 179, 6
42, 181, 2
43, 191, 10
44, 193, 2
45, 197, 4
46, 199, 2
47, 211, 12
48, 223, 12
49, 227, 4
50, 229, 2
51, 233, 4
52, 239, 6
53, 241, 2
54, 251, 10
55, 257, 6
56, 263, 6
57, 269, 6
58, 271, 2
59, 277, 6
60, 281, 4
61, 283, 2
62, 293, 10
63, 307, 14
64, 311, 4
65, 313, 2
66, 317, 4
67, 331, 14
68, 337, 6
69, 347, 10
70, 349, 2
71, 353, 4
72, 359, 6
73, 367, 8
74, 373, 6
75, 379, 6
76, 383, 4
77, 389, 6
78, 397, 8
79, 401, 4
80, 409, 8
81, 419, 10
82, 421, 2
83, 431, 10
84, 433, 2
85, 439, 6
86, 443, 4
87, 449, 6
88, 457, 8
89, 461, 4
90, 463, 2
91, 467, 4
92, 479, 12
93, 487, 8
94, 491, 4
95, 499, 8
96, 503, 4
97, 509, 6
98, 521, 12
99, 523, 2
100, 541, 18

1000
primes1000.txt Normal file

File diff suppressed because it is too large Load Diff

10000
primes10000.txt Normal file

File diff suppressed because it is too large Load Diff

1000000
primes1000000.txt Normal file

File diff suppressed because it is too large Load Diff