This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Check whether all elements from $needle array are present in $haystack array. | |
* Works for single-dimentional arrays only. | |
* If you use multi-dimentional arrays, result could be misterious. | |
* @param array $needle | |
* @param array $haystack | |
* @return boolean true if all elements in $needle are found in $haystack. false otherwise. | |
*/ | |
function array_in_array($needle, $haystack) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Problem 3: Largest prime factor | |
* | |
* The prime factors of 13195 are 5, 7, 13 and 29. | |
* What is the largest prime factor of the number 600851475143 ? | |
*/ | |
$n = 600851475143; | |
$factor = 2; | |
$lastFactor = 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Problem 4: Largest palindrome product | |
* | |
* A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. | |
* Find the largest palindrome made from the product of two 3-digit numbers. | |
*/ | |
$largest=101; | |
$l1 = $l2 = 999; | |
for ($i=999; $i>=100; $i--) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Problem 5: Smallest multiple | |
* | |
* 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. | |
* What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? | |
*/ | |
$evenlyDivisible = false; | |
$limit = 20; | |
for($i=$limit; !$evenlyDivisible; $i+=$limit) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Problem 6: Sum square difference | |
* | |
* The sum of the squares of the first ten natural numbers is, | |
* 1^2 + 2^2 + ... + 10^2 = 385 | |
* The square of the sum of the first ten natural numbers is, | |
* (1 + 2 + ... + 10)^2 = 552 = 3025 | |
* Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640. | |
* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Problem 7: 10001st prime | |
* By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. | |
* What is the 10001st prime number? | |
* | |
* Uses GMP - http://www.php.net/manual/en/book.gmp.php | |
*/ | |
for($count = 0, $prime=0; $count < 10001; $prime=gmp_intval(gmp_nextprime($prime)), $count++); | |
echo "10001st prime number is: $prime"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Problem 8: Largest product in a series | |
* Find the greatest product of five consecutive digits in the 1000-digit number: | |
* 73167176531330624919225119674426574742355349194934 | |
* 96983520312774506326239578318016984801869478851843 | |
* 85861560789112949495459501737958331952853208805511 | |
* 12540698747158523863050715693290963295227443043557 | |
* 66896648950445244523161731856403098711121722383113 | |
* 62229893423380308135336276614282806444486645238749 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Problem 9: Special Pythagorean triplet | |
* A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, | |
* a^2 + b^2 = c^2 | |
* For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. | |
* | |
* There exists exactly one Pythagorean triplet for which a + b + c = 1000. | |
* Find the product abc. | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Problem 10: Summation of primes | |
* The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. | |
* Find the sum of all the primes below two million. | |
* | |
* Uses GMP - http://www.php.net/manual/en/book.gmp.php | |
*/ | |
for($prime=2, $sum=0; $prime < 2000000; $sum+=$prime, $prime=gmp_intval(gmp_nextprime($prime))); | |
echo "Sum of all prime numbers < 2 million is: $sum"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Largest product in a grid | |
Problem 11 | |
In the 20×20 grid below, four numbers along a diagonal line have been marked in red. | |
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 | |
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 | |
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 | |
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 |