Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. <?php
  2.  
  3. #fibonacci formula
  4. function fibonacciTest($param){
  5. #Using Binet's Formula for efficiency
  6. return ((1 + (sqrt(5)))**$param - (1 - (sqrt(5)))**$param)/(2**$param * sqrt(5));
  7. }
  8.  
  9. #list fibonacci numbers and sum only even numbers
  10. function eulerFib($param){
  11. #create an array
  12. $array = array();
  13. #start a loop
  14. for($i = 0; $i <= $param; $i++){
  15. #call a recursive function
  16. $results = fibonacciTest($i);
  17. #list fibonacci numbers
  18. echo "#" . $i . " = " . $results . "\n";
  19. #only push even numbers to array
  20. if($results % 2 === 0){
  21. array_push($array, $results);
  22. }
  23. }
  24. #print results and answer
  25. print_r($array);
  26. Print "Total = " . array_sum($array) . "\n";
  27. }
  28.  
  29. #call function
  30. eulerFib(33); #for fibonacci values less than 4,000,000 the answer is 4,613,762
  31.  
  32. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement