在解决了一段时间以来我一直被束缚的编程挑战之后,我总是在想,"它有效,那就足够了".
在我看来,我认为这不是真正正确的思维模式,我认为我应该始终尝试以最佳性能进行编码.
无论如何,有了这个说,我刚试了一个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) 我用c ++编写了一个程序来输出你告诉它的fibonacci序列中的数字.它一直工作到大约第47个数字,然后它打印出完全不同的数字,甚至是负数,并且它们都没有超过9或10个单独的整数.这是代码
#include <iostream>
int a = 0;
int b = 1;
int c;
int var = 0;
int num;
void fibonacci()
{
using namespace std;
c = a;
a = a + b;
b = c;
var += 1;
}
void loop()
{
using namespace std;
for (int iii=0; iii<num; iii++)
fibonacci();
}
int main()
{
using namespace std;
cout << "What fibonacci number do you want to know? ";
cin >> num;
cin.get();
loop();
cout << "" << …Run Code Online (Sandbox Code Playgroud) 我写了一个计算斐波纳契数的程序.最初,由于资源问题,我无法输入大数字,但现在我重新编写后,它运行速度很快.但是,如果我使用整数,一旦输入大数字,数字就会变为负数.我尝试使用很长时间,但它们也非常快速地包裹着.如果你不知道我的意思,那么这段代码应该解释一下:
`System.out.println("The 536th fibonacci number: "fib(536));`
`*The 536th fibonacci number: -8757250051716203595*`
Run Code Online (Sandbox Code Playgroud)
显然,在这种情况下,负数是没有意义的,所以我想知道我是如何做到的,所以它总能奏效 - 无论如何都不会缠绕.
编辑:问题解决了!
import java.math.BigInteger;
public static BigInteger fib(int n)
{
return fib2h(n,BigInteger.ONE,BigInteger.ONE);
}
public static BigInteger fibh(int n,BigInteger o,BigInteger p)
{
if(n==1) return o;
return fib2h(n-1,p,o.add(p));
}
Run Code Online (Sandbox Code Playgroud) 我有一个关于将斐波那契序列列入清单的问题,我只是陌生人,请有人帮助我.
这是我的代码.我知道这看起来有点不对劲,因为它说的语法无效.我真的不知道该怎么办:(
此代码适用于普通代码而不使用列表!
myArray1 = [0]
myArray2 = [1]
while myArray2 < 700:
myArray1, myArray2 = b[i], myArray1+myArray2[i]
print(myArray2)
Run Code Online (Sandbox Code Playgroud) 以下程序是找到偶数斐波纳契项的总和不超过四百万.该程序中的最后一个'cout'语句根本没有被执行.为什么?请帮忙.
#include <iostream>
using namespace std;
int main()
{
int a, b, c, sum, sum1, sum2;
a = 1;
b = 2;
sum2 = 0;
cout << b << endl;
c = a + b;
sum1 = c;
while (c <= 4000000)
{
a = b;
b = c;
if ((a + b) <= 4000000)
{
c = a + b;
if (c%2 == 0)
{
sum2 = sum2 + c;
cout << c << endl;
}
}
}
cout …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过优化算法和理解big-o等方面做得更好.
我把下面的函数汇总在一起来计算第n个斐波纳契数.这是有效的(对于相当高的输入).我的问题是,我该如何改进这个功能?以这种方式计算Fibonacci序列有什么缺点?
function fibo(n) {
var i;
var resultsArray = [];
for (i = 0; i <= n; i++) {
if (i === 0) {
resultsArray.push(0);
} else if (i === 1) {
resultsArray.push(1);
} else {
resultsArray.push(resultsArray[i - 2] + resultsArray[i - 1]);
}
}
return resultsArray[n];
}
Run Code Online (Sandbox Code Playgroud)
我相信我的大时间是O(n),但由于我创建的数组,我的空间大o是O(n ^ 2).它是否正确?
我已经设法以递归方式编写我的算法:
int fib(int n) {
if(n == 1)
return 3
elseif (n == 2)
return 2
else
return fib(n – 2) + fib(n – 1)
}
Run Code Online (Sandbox Code Playgroud)
目前我正在尝试将其转换为迭代方法而没有成功:
int fib(int n) {
int i = 0, j = 1, k, t;
for (k = 1; k <= n; ++k)
{
if(n == 1) {
j = 3;
}
else if(n == 2) {
j = 2;
}
else {
t = i + j;
i = j;
j = t;
} …Run Code Online (Sandbox Code Playgroud) 如何使用此代码打印给定术语的斐波纳契序列的所有值?现在它只打印最后一个术语
#include <stdio.h>
int fibonacci(int n){
if (n==2)
return 1;
else
return fibonacci(n-1) + fibonacci(n-2);
}
int main()
{
int n;
int answer;
printf("Enter the number of terms you'd like in the sequence\n");
scanf("%d",&n);
answer = fibonacci(n);
printf("The answer is %d\n", answer);
}
Run Code Online (Sandbox Code Playgroud) 我有一个斐波那契问题,我想计算第n个斐波那契,并想要它的最后一位数字(我将在%10时获得的数字)。n将被给出,并且可以高达10 ^ 18。
unsigned long long int nthfib(long long int n) {
double phi = (1 + sqrt(5)) / 2;
return round(pow(phi, n-1) / sqrt(5));
}
Run Code Online (Sandbox Code Playgroud)
上面的代码,对于大n,例如1024,给出了很大的数字,我无法将其存储在变量中并找到其%10。
由于时间是一个问题,我需要O(1)中的解决方案。
我知道下面是一个不理想的实现相比,斐波那契数发生器的这个,但我似乎无法理解为什么它不按预期工作:
def fibonacci_sequence():
fl, fp = 1, 1
while True:
yield (fl + fp)
store = fl + fp
fp = fl
fl = store
for i in range(10):
print(next(fibonacci_sequence()))
Run Code Online (Sandbox Code Playgroud)
它始终保持打印2。
生成器的状态不是yield在每次迭代的关键字下面更新吗?