在解决了一段时间以来我一直被束缚的编程挑战之后,我总是在想,"它有效,那就足够了".
在我看来,我认为这不是真正正确的思维模式,我认为我应该始终尝试以最佳性能进行编码.
无论如何,有了这个说,我刚试了一个ProjectEuler问题.具体问题#2.
我怎么能改进这个解决方案.我觉得它真的很冗长.就像我在递归中传递前一个数字一样.
<?php
/* Each new term in the Fibonacci sequence is generated by adding the previous two
terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
Find the sum of all the even-valued terms in the sequence which do not exceed
four million.
*/
function fibonacci ( $number, $previous = 1 ) {
global $answer;
$fibonacci = $number + $previous; …Run Code Online (Sandbox Code Playgroud)