我想知道怎样才能找到第n个斐波那契序列的n个非常大的n值1000000.使用等级 - 学校递推方程fib(n)=fib(n-1)+fib(n-2),找到第50个学期需要2-3分钟!
谷歌搜索后,我开始了解Binet的公式,但它不适合n> 79的值,因为这里说的
有没有算法这样做就像我们找到素数一样?
在Haskell中,如何基于第n个Fibonacci数等于第(n-2)个Fibonacci数加上第(n-1)个Fibonacci数的属性生成Fibonacci数?
我见过这个:
fibs :: [Integer]
fibs = 1 : 1 : zipWith (+) fibs (tail fibs)
Run Code Online (Sandbox Code Playgroud)
我真的不明白,或者它是如何产生无限列表而不是包含3个元素的列表.
我如何通过计算实际定义来编写haskell代码,而不是通过使用list函数做一些非常奇怪的事情?
我知道使用适当的函数结构编写没有任何问题,但我想知道如何用大多数Pythonic方式找到第n个斐波那契数字和一行.
我写了那段代码,但在我看来并不是最好的方式:
>>> fib=lambda n:reduce(lambda x,y:(x[0]+x[1],x[0]),[(1,1)]*(n-2))[0]
>>> fib(8)
13
Run Code Online (Sandbox Code Playgroud)
怎么会更好更简单?
我正在研究一个Project Euler问题:关于偶数Fibonacci数的总和问题.
我的代码:
def Fibonacci(n):
if n == 0:
return 0
elif n == 1:
return 1
else:
return Fibonacci(n-1) + Fibonacci(n-2)
list1 = [x for x in range(39)]
list2 = [i for i in list1 if Fibonacci(i) % 2 == 0]
Run Code Online (Sandbox Code Playgroud)
通过打印sum(list2)可以很容易地找到问题的解决方案.但是,我猜测它需要花费大量时间来提出list2.有没有办法让这更快?或者这样就可以了......
(问题:通过考虑Fibonacci序列中的值不超过四百万的项,找到偶数项的总和.)
我很难理解为什么
#include <iostream>
using namespace std;
int fib(int x) {
if (x == 1) {
return 1;
} else {
return fib(x-1)+fib(x-2);
}
}
int main() {
cout << fib(5) << endl;
}
Run Code Online (Sandbox Code Playgroud)
导致分段错误.一旦x下降到1不应该最终返回?
我看到很多生成器函数的例子,但我想知道如何为类编写生成器.可以说,我想把斐波纳契系列写成一个类.
class Fib:
def __init__(self):
self.a, self.b = 0, 1
def __next__(self):
yield self.a
self.a, self.b = self.b, self.a+self.b
f = Fib()
for i in range(3):
print(next(f))
Run Code Online (Sandbox Code Playgroud)
输出:
<generator object __next__ at 0x000000000A3E4F68>
<generator object __next__ at 0x000000000A3E4F68>
<generator object __next__ at 0x000000000A3E4F68>
Run Code Online (Sandbox Code Playgroud)
为什么self.a没有印刷价值?另外,我如何unittest为发电机写信?
我被要求写一个简单的Fibonacci算法实现,然后让它更快.
这是我的初步实施
public class Fibonacci {
public static long getFibonacciOf(long n) {
if (n== 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
return getFibonacciOf(n-2) + getFibonacciOf(n-1);
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner (System.in);
while (true) {
System.out.println("Enter n :");
long n = scanner.nextLong();
if (n >= 0) {
long beginTime = System.currentTimeMillis();
long fibo = getFibonacciOf(n);
long endTime = System.currentTimeMillis();
long delta = …Run Code Online (Sandbox Code Playgroud) 我有一个使用BigInteger的Fibonacci序列的简单实现:
internal class FibonacciEnumerator : IEnumerator<BigInteger>
{
private BigInteger _previous = 1;
private BigInteger _current = 0;
public void Dispose(){}
public bool MoveNext() {return true;}
public void Reset()
{
_previous = 1;
_current = 0;
}
public BigInteger Current
{
get
{
var temp = _current;
_current += _previous;
_previous = temp;
return _current;
}
}
object IEnumerator.Current { get { return Current; }
}
}
internal class FibonacciSequence : IEnumerable<BigInteger>
{
private readonly FibonacciEnumerator _f = new FibonacciEnumerator();
public …Run Code Online (Sandbox Code Playgroud) 以尽可能少的字符生成Fibonacci序列.任何语言都可以,除了您使用一个运算符定义的语言f,它会打印斐波那契数字.
起点:25 14个字符的哈斯克尔:
f=0:1:zipWith(+)f(tail f)
f=0:scanl(+)1f
Run Code Online (Sandbox Code Playgroud) 我是clojure的新手,他想看看所有的大惊小怪.找出感受它的最佳方法是编写一些简单的代码,我想我会从Fibonacci函数开始.
我的第一个努力是:
(defn fib [x, n]
(if (< (count x) n)
(fib (conj x (+ (last x) (nth x (- (count x) 2)))) n)
x))
Run Code Online (Sandbox Code Playgroud)
要使用它,我需要在调用函数时使用[0 1]对x进行种子处理.我的问题是,如果不将它包装在一个单独的函数中,是否可以编写一个只返回元素数量的函数?
做一些阅读让我有了一些更好的方法来实现相同的功能:
(defn fib2 [n]
(loop [ x [0 1]]
(if (< (count x) n)
(recur (conj x (+ (last x) (nth x (- (count x) 2)))))
x)))
Run Code Online (Sandbox Code Playgroud)
和
(defn fib3 [n]
(take n
(map first (iterate (fn [[a b]] [b (+ a b)]) [0 1]))))
Run Code Online (Sandbox Code Playgroud)
无论如何,更多的是为了锻炼而不是其他任何东西,任何人都可以帮助我使用纯粹的递归Fibonacci函数的更好版本吗?或者可能分享更好/不同的功能?