Last active
August 15, 2017 11:19
-
-
Save drupol/22f022b1ccb8bbb9c226caff196b1e0c to your computer and use it in GitHub Desktop.
Approximate numbers e and pi using combinatorics and Fibonacci.
This file contains 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 | |
/** | |
* Find the number e (2.7182818284590452354 => M_E constant in PHP). | |
* | |
* To find the number e using permutations: | |
* 1: Generate a set of size S of random values, | |
* 2: Find the number of permutations of that set, let it be #P0, | |
* 3: With all the permutations of this set, let #P1 the count of the permutations where | |
* any items are not at the same index as in the original input, | |
* 4: Divide #P0 by #P1, that's your approximation, | |
* 5: Restart at 1 and increase the size of the set. | |
* | |
* Requirements: | |
* - https://packagist.org/packages/drupol/phpermutations | |
* | |
* Install: | |
* 1: Git clone phpermutations, | |
* 2: Run composer install, | |
* 3: Create this file at the root of the project, | |
* 4: Run php e.php. | |
*/ | |
include "./vendor/autoload.php"; | |
for ($i = 2; $i < 15; $i++) { | |
$input = range(1, $i); | |
$size = $i; | |
echo "**********************************************************" . "\n"; | |
echo "Size: " . $i . "\n"; | |
$Permutations = new \drupol\phpermutations\Generators\Permutations($input, $size); | |
$j = 0; | |
foreach ($Permutations->generator() as $permutation) { | |
if (!($array3 = array_intersect_assoc($input, $permutation))) { | |
$j++; | |
} | |
} | |
echo "Number of permutations (#P0): " . $Permutations->count() . "\n"; | |
echo "Number of permutations without item at the same place (#P1): " . $j . "\n"; | |
echo "(#P0) / (#P1): " . $Permutations->count()/$j . "\n"; | |
} |
This file contains 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 | |
/** | |
* Find the number pi (3.1415926535898 => M_PI constant in PHP). | |
* | |
* π/4 = arctan(1/2) + arctan(1/5) + arctan(1/13) + arctan(1/21) + ... | |
* | |
* You'll have already noticed the Fibonacci numbers here. | |
* However, not all the Fibonacci numbers appear on the left hand sides. | |
* For instance, we have no expansion for arctan(1/5) nor for arctan(1/13). | |
* Only the even numbered Fibonacci terms seem to be expanded: | |
* (F2=1, F4=3, F6=8, F8=21, ...) | |
* | |
* To find the number pi using Fibonacci: | |
* 1: Generate a set of size S of Fibonacci numbers, | |
* 2: Apply on each item of the set a function that will compute the inverse | |
* of the item, | |
* 3: Apply on each item of the set the atan() function and multiply by 4, | |
* 4: Filter out values where the key value is odd, | |
* 3: Sum all the terms of the array. | |
* | |
* Requirements: | |
* - https://packagist.org/packages/drupol/phpermutations | |
* | |
* Install: | |
* 1: Git clone phpermutations, | |
* 2: Run composer install, | |
* 3: Create this file at the root of the project, | |
* 4: Run php pi.php. | |
*/ | |
include "./vendor/autoload.php"; | |
$Fibonacci = new \drupol\phpermutations\Generators\Fibonacci(); | |
for ($i = 2; $i <= 50; $i = $i+2) { | |
$gen = $Fibonacci->generator(); | |
$input = array(); | |
for ($s = 0; $s <= $i; $s++) { | |
$input[] = $gen->current(); | |
$gen->next(); | |
} | |
$input = array_sum(array_filter(array_map(function($item) { | |
return 4 * atan(1/$item); | |
}, array_slice($input, 3)), function($key) { | |
return !($key%2); | |
}, ARRAY_FILTER_USE_KEY)); | |
echo "**********************************************************" . "\n"; | |
echo "Size: " . $i . "\n"; | |
echo "Pi estimation: " . $input . "\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment