More progress
This commit is contained in:
61
solutions/ProjectEuler/046/solve.php
Normal file
61
solutions/ProjectEuler/046/solve.php
Normal file
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
/*
|
||||
It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square.
|
||||
|
||||
9 = 7 + 2 * 1^2
|
||||
15 = 7 + 2 * 2^2
|
||||
21 = 3 + 2 * 3^2
|
||||
25 = 7 + 2 * 3^2
|
||||
27 = 19 + 2 * 2^2
|
||||
33 = 31 + 2 * 1^2
|
||||
|
||||
It turns out that the conjecture was false.
|
||||
|
||||
What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?
|
||||
*/
|
||||
|
||||
$primes = [2];
|
||||
$composites = [];
|
||||
$twicesquares = [];
|
||||
|
||||
function is_prime(int $n) :bool{for($i=$n**.5|1;$i&&$n%$i--;);return!$i&&$n>1;}
|
||||
|
||||
for ($i= 3; $i<6000; $i += 2) {
|
||||
if (is_prime($i))
|
||||
{
|
||||
$primes[] = $i;
|
||||
}
|
||||
else
|
||||
{
|
||||
$composites[] = $i;
|
||||
}
|
||||
}
|
||||
|
||||
for($i=1; $i<100; $i++)
|
||||
{
|
||||
$twicesquares[] = 2*($i*$i);
|
||||
}
|
||||
|
||||
foreach($composites as $composite)
|
||||
{
|
||||
$count = 0;
|
||||
foreach($primes as $prime)
|
||||
{
|
||||
if($prime > $composite)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if(in_array($composite-$prime, $twicesquares))
|
||||
{
|
||||
$count++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if($count == 0)
|
||||
{
|
||||
echo "Found $composite\n";
|
||||
die;
|
||||
}
|
||||
}
|
||||
|
16
solutions/ProjectEuler/047/solve.php
Normal file
16
solutions/ProjectEuler/047/solve.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
/*
|
||||
The first two consecutive numbers to have two distinct prime factors are:
|
||||
14 = 2 * 7
|
||||
15 = 3 * 5
|
||||
|
||||
|
||||
The first three consecutive numbers to have three distinct prime factors are:
|
||||
|
||||
644 = 2^2 * 7 * 23
|
||||
645 = 3 * 5 * 43
|
||||
646 = 2 * 17 * 19
|
||||
|
||||
Find the first four consecutive integers to have four distinct prime factors each. What is the first of these numbers?
|
||||
*/
|
||||
|
18
solutions/other/fibonacci.php
Normal file
18
solutions/other/fibonacci.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
function fibonacci($n) {
|
||||
if ($n <= 1)
|
||||
return $n;
|
||||
return fibonacci($n - 1) + fibonacci($n - 2);
|
||||
}
|
||||
|
||||
echo fibonacci(1).PHP_EOL;
|
||||
echo fibonacci(2).PHP_EOL;
|
||||
echo fibonacci(3).PHP_EOL;
|
||||
echo fibonacci(4).PHP_EOL;
|
||||
echo fibonacci(5).PHP_EOL;
|
||||
echo fibonacci(6).PHP_EOL;
|
||||
echo fibonacci(7).PHP_EOL;
|
||||
echo fibonacci(8).PHP_EOL;
|
||||
|
||||
//1 1 2 3 5 8 13 21 34
|
9
solutions/other/multipleof3and5.php
Normal file
9
solutions/other/multipleof3and5.php
Normal file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
//Write a program or solve manually to find the smallest positive number that is evenly divisible by all of the numbers from 1 to 20.
|
||||
$m3 = range(3, 1000, 3);
|
||||
$m5 = range(5, 1000, 5);
|
||||
|
||||
$all = array_unique(array_merge($m3,$m5));
|
||||
|
||||
echo array_sum($all);
|
||||
|
26
solutions/other/numbergame.php
Normal file
26
solutions/other/numbergame.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
//$argv = ['', 556, 25, 75, 9, 4, 2, 9];
|
||||
//$argv = ['', 134, 100, 6, 1, 1, 6, 6 ];
|
||||
//$argv = ['', 306, 50, 1, 5, 1, 8, 7];
|
||||
$sum = (int)$argv[1];
|
||||
$input_numbers = array_slice($argv, 2);
|
||||
$operation = ['+','-','*','/'];
|
||||
|
||||
$best_diff = $sum;
|
||||
while($best_diff != 0) {
|
||||
shuffle($input_numbers);
|
||||
$numbers = array_slice($input_numbers, random_int(1, sizeof($input_numbers)));
|
||||
|
||||
$exp = "";
|
||||
foreach($numbers as $n){
|
||||
$exp .= $n;
|
||||
$exp .= $operation[random_int(0,3)];
|
||||
}
|
||||
$exp = substr($exp, 0, -1);
|
||||
$s = eval("return $exp;");
|
||||
|
||||
if(abs($sum - $s) < $best_diff) {
|
||||
$best_diff = abs($sum - $s);
|
||||
echo "Found: $best_diff =\t$exp\n";
|
||||
}
|
||||
}
|
21
solutions/other/primesum.php
Normal file
21
solutions/other/primesum.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
function primeCheck($number){
|
||||
if ($number == 1)
|
||||
return 0;
|
||||
for ($i = 2; $i <= $number/2; $i++){
|
||||
if ($number % $i == 0)
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
$sum = 0;
|
||||
for($i=2; $i<=1000; $i++)
|
||||
{
|
||||
if(primeCheck($i)) {
|
||||
$sum += $i;
|
||||
}
|
||||
}
|
||||
|
||||
echo "Sum: $sum\n";
|
27
solutions/other/reversemultiple9.php
Normal file
27
solutions/other/reversemultiple9.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
//Find a three-digit number such that when the digits are reversed and subtracted from the original number, the result is a multiple of 9.
|
||||
function numreverse($number) {
|
||||
|
||||
$numberStr = (string)$number;
|
||||
$numberStr = strrev($numberStr);
|
||||
return (int)$numberStr;
|
||||
}
|
||||
$i = 100;
|
||||
$good = 0;
|
||||
$bad = 0;
|
||||
while($i <=999) {
|
||||
$num = $i - numreverse($i);
|
||||
|
||||
if($num != 0 && $num % 9 == 0) {
|
||||
echo("\nFound $i");
|
||||
$good++;
|
||||
} else {
|
||||
echo("\nNo $i");
|
||||
$bad++;
|
||||
}
|
||||
|
||||
|
||||
$i++;
|
||||
}
|
||||
|
||||
echo "\nGood: $good Bad: $bad";
|
24
solutions/other/sum44.php
Normal file
24
solutions/other/sum44.php
Normal file
@ -0,0 +1,24 @@
|
||||
<?php
|
||||
//12857 multiplied by 7 gives 90099, and the sum of the digits of 90099 is indeed 27, not 44. Keep trying!
|
||||
|
||||
function sumDigits($number) {
|
||||
$sum = 0;
|
||||
// converting number to string to access digits easily
|
||||
$numberStr = (string)$number;
|
||||
for ($i = 0; $i < strlen($numberStr); $i++) {
|
||||
$digit = (int)$numberStr[$i];
|
||||
$sum += $digit;
|
||||
}
|
||||
return $sum;
|
||||
}
|
||||
$i = 0;
|
||||
while(true) {
|
||||
$i++;
|
||||
|
||||
$var = $i*7;
|
||||
|
||||
if(sumDigits($var) == 44)
|
||||
{
|
||||
die("Found $i");
|
||||
}
|
||||
}
|
21
solutions/other/sumdiv20.php
Normal file
21
solutions/other/sumdiv20.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
//Write a function to find the smallest number that is divisible by each of the numbers from 1 to 20.
|
||||
|
||||
function sumdiv20() {
|
||||
$i = 20;
|
||||
while(true) {
|
||||
$i += 20;
|
||||
|
||||
if($i %19== 0) {
|
||||
if($i %18== 0) {
|
||||
if($i %17== 0) {
|
||||
if($i %16== 0) {
|
||||
if($i %15== 0) {
|
||||
if($i %14== 0) {
|
||||
if($i %13== 0) {
|
||||
if($i %12== 0) {
|
||||
if($i %11== 0) {
|
||||
return $i;
|
||||
}}}}}}}}}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user