Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- #fibonacci formula
- function fibonacciTest($param){
- #Using Binet's Formula for efficiency
- return ((1 + (sqrt(5)))**$param - (1 - (sqrt(5)))**$param)/(2**$param * sqrt(5));
- }
- #list fibonacci numbers and sum only even numbers
- function eulerFib($param){
- #create an array
- $array = array();
- #start a loop
- for($i = 0; $i <= $param; $i++){
- #call a recursive function
- $results = fibonacciTest($i);
- #list fibonacci numbers
- echo "#" . $i . " = " . $results . "\n";
- #only push even numbers to array
- if($results % 2 === 0){
- array_push($array, $results);
- }
- }
- #print results and answer
- print_r($array);
- Print "Total = " . array_sum($array) . "\n";
- }
- #call function
- eulerFib(33); #for fibonacci values less than 4,000,000 the answer is 4,613,762
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement