More progress

This commit is contained in:
2024-07-23 09:13:29 +00:00
parent 3903a79785
commit 6b8afd49b9
9 changed files with 223 additions and 0 deletions

View 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

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

View 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";
}
}

View 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";

View 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
View 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");
}
}

View 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;
}}}}}}}}}
}
}