找出第n个斐波纳契数字为非常大的“n”

18 Solutions collect form web for “找出第n个斐波纳契数字为非常大的“n””

` `var fib = function(n) { return n < 2 ? n : fib(n - 1) + fib(n - 2); };` `

A.利用下划线库

` `var fib2 = _.memoize(function(n) { return n < 2 ? n : fib2(n - 1) + fib2(n - 2); });` `

B.无库

` `var fib3 = (function(){ var memo = {}; return function(n) { if (memo[n]) {return memo[n];} return memo[n] = (n <= 2) ? 1 : fib3(n-2) + fib3(n-1); }; })();` `

` `F(2n-1) = F(n-1)^2 + F(n)^2 === a' = a^2 + b^2 F(2n) = 2 F(n-1) F(n) + F(n)^2 === b' = 2ab + b^2` `

` `public class algFibonacci { // author Orel Eraki // Fibonacci algorithm // O(log2 n) public static int Fibonacci(int n) { int num = Math.abs(n); if (num == 0) { return 0; } else if (num <= 2) { return 1; } int[][] number = { { 1, 1 }, { 1, 0 } }; int[][] result = { { 1, 1 }, { 1, 0 } }; while (num > 0) { if (num%2 == 1) result = MultiplyMatrix(result, number); number = MultiplyMatrix(number, number); num/= 2; } return result[1][1]*((n < 0) ? -1:1); } public static int[][] MultiplyMatrix(int[][] mat1, int[][] mat2) { return new int[][] { { mat1[0][0]*mat2[0][0] + mat1[0][1]*mat2[1][0], mat1[0][0]*mat2[0][1] + mat1[0][1]*mat2[1][1] }, { mat1[1][0]*mat2[0][0] + mat1[1][1]*mat2[1][0], mat1[1][0]*mat2[0][1] + mat1[1][1]*mat2[1][1] } }; } public static void main(String[] args) { System.out.println(Fibonacci(-8)); } }` `

` `int fib(int n) { if (n < 2) return 1; return fib(n-1) + fib(n-2) }` `

…并坐下来看堆栈溢出。

` `int fib(int n) { if (n < 2) return 1; int n_1 = 1, n_2 = 1; for (int i = 2; i <= n; i += 1) { int n_new = n_1 + n_2; n_1 = n_2; n_2 = n_new; } return n_2; }` `

C源代码： –

` `#include<stdio.h> #include<conio.h> #define max 2000 int arr1[max],arr2[max],arr3[max]; void fun(void); int main() { int num,i,j,tag=0; clrscr(); for(i=0;i<max;i++) arr1[i]=arr2[i]=arr3[i]=0; arr2[max-1]=1; printf("ENTER THE TERM : "); scanf("%d",&num); for(i=0;i<num;i++) { fun(); if(i==num-3) break; for(j=0;j<max;j++) arr1[j]=arr2[j]; for(j=0;j<max;j++) arr2[j]=arr3[j]; } for(i=0;i<max;i++) { if(tag||arr3[i]) { tag=1; printf("%d",arr3[i]); } } getch(); return 1; } void fun(void) { int i,temp; for(i=0;i<max;i++) arr3[i]=arr1[i]+arr2[i]; for(i=max-1;i>0;i--) { if(arr3[i]>9) { temp=arr3[i]; arr3[i]%=10; arr3[i-1]+=(temp/10); } } }` `

5000的斐波那契数是：

``````3878968454388325633701916308325905312082127714646245106160597214895550139044037097010822916462210669479293452858882973813483102008954982940361430156911478938364216563944106910214505634133706558656238254656700712525929903854933813928836378347518908762970712033337052923107693008518093849801803847813996748881765554653788291644268912980384613778969021502293082475666346224923071883324803280375039130352903304505842701147635242270210934637699104006714174883298422891491273104054328753298044273676822977244987749874555691907703880637046832794811358973739993110106219308149018570815397854379195305617510761053075688783766033667355445258844886241619210553457493675897849027988234351023599844663934853256411952221859563060475364645470760330902420806382584929156452876291575759142343809142302917491088984155209854432486594079793571316841692868039545309545388698114665082066862897420639323438488465240988742395873801976993820317174208932265468879364002630797780058759129671389634214252579116872755600360311370547754724604639987588046985178408674382863125
``````

` `#include<stdio.h> #include<string.h> #define LIMIT 5001 #define MAX 1050 char num[LIMIT][MAX]; char result[MAX]; char temp[MAX]; char* sum(char str1[], char str2[]) { int len1 = strlen(str1); int len2 = strlen(str2); int minLen, maxLen; int i, j, k; if (len1 > len2) minLen = len2, maxLen = len1; else minLen = len1, maxLen = len2; int carry = 0; for (k = 0, i = len1 - 1, j = len2 - 1; k<minLen; k++, i--, j--) { int val = (str1[i] - '0') + (str2[j] - '0') + carry; result[k] = (val % 10) + '0'; carry = val / 10; } while (k < len1) { int val = str1[i] - '0' + carry; result[k] = (val % 10) + '0'; carry = val / 10; k++; i--; } while (k < len2) { int val = str2[j] - '0' + carry; result[k] = (val % 10) + '0'; carry = val / 10; k++; j--; } if (carry > 0) { result[maxLen] = carry + '0'; maxLen++; result[maxLen] = '\0'; } else { result[maxLen] = '\0'; } i = 0; while (result[--maxLen]) { temp[i++] = result[maxLen]; } temp[i] = '\0'; return temp; } void generateFibonacci() { int i; num[0][0] = '0'; num[0][1] = '\0'; num[1][0] = '1'; num[1][1] = '\0'; for (i = 2; i <= LIMIT; i++) { strcpy(num[i], sum(num[i - 1], num[i - 2])); } } int main() { //freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout); int N; generateFibonacci(); while (scanf("%d", &N) == 1) { printf("The Fibonacci number for %d is %s\n", N, num[N]); } return 0; }` `

` `def fib(n): if n == 0: return 0 if n == 1: return 1 def matmul(M1, M2): a11 = M1[0][0]*M2[0][0] + M1[0][1]*M2[1][0] a12 = M1[0][0]*M2[0][1] + M1[0][1]*M2[1][1] a21 = M1[1][0]*M2[0][0] + M1[1][1]*M2[1][0] a22 = M1[1][0]*M2[0][1] + M1[1][1]*M2[1][1] return [[a11, a12], [a21, a22]] def matPower(mat, p): if p == 1: return mat m2 = matPower(mat, p//2) if p % 2 == 0: return matmul(m2, m2) else: return matmul(matmul(m2, m2),mat) Q = [[1,1],[1,0]] q_final = matPower(Q, n-1) return q_final[0][0]` `

` `def fibo(n): i=3 l=[0,1,1] if n>2: while i<=n: l[i%3]= l[(i-1) % 3] + l[(i-2) % 3] i+=1 return l[n%3]` `

` `public static IEnumerable<BigInteger> Fibonacci() { BigInteger i = 0; BigInteger j = 1; while (true) { BigInteger fib = i + j; i = j; j = fib; yield return fib; } } public static string BiggerFib() { BigInteger fib = Fibonacci().Skip(1000000).First(); return fib.ToString(); }` `

` `def fib(n): if n == 0: return 0 a, b = 0, 1 for i in range(2, n+1): a, b = b, a+b return b` `
` `#include <bits/stdc++.h> #define MOD 1000000007 using namespace std; const long long int MAX = 10000000; // Create an array for memoization long long int f[MAX] = {0}; // Returns n'th fuibonacci number using table f[] long long int fib(int n) { // Base cases if (n == 0) return 0; if (n == 1 || n == 2) return (f[n] = 1); if (f[n]) return f[n]; long long int k = (n & 1)? (n+1)/2 : n/2; f[n] = (n & 1)? (fib(k)*fib(k) + fib(k-1)*fib(k-1)) %MOD : ((2*fib(k-1) + fib(k))*fib(k))%MOD; return f[n]; } int main() { long long int n = 1000000; printf("%lld ", fib(n)); return 0; }` `

` `public class FabSeries { private static Map<BigInteger, BigInteger> memo = new TreeMap<>(); public static BigInteger fabMemorizingTech(BigInteger n) { BigInteger ret; if (memo.containsKey(n)) return memo.get(n); else { if (n.compareTo(BigInteger.valueOf(2)) <= 0) ret = BigInteger.valueOf(1); else ret = fabMemorizingTech(n.subtract(BigInteger.valueOf(1))).add( fabMemorizingTech(n.subtract(BigInteger.valueOf(2)))); memo.put(n, ret); return ret; } } public static BigInteger fabWithoutMemorizingTech(BigInteger n) { BigInteger ret; if (n.compareTo(BigInteger.valueOf(2)) <= 0) ret = BigInteger.valueOf(1); else ret = fabWithoutMemorizingTech(n.subtract(BigInteger.valueOf(1))).add( fabWithoutMemorizingTech(n.subtract(BigInteger.valueOf(2)))); return ret; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter Number"); BigInteger num = scanner.nextBigInteger(); // Try with memorizing technique Long preTime = new Date().getTime(); System.out.println("Stats with memorizing technique "); System.out.println("Fibonacci Value : " + fabMemorizingTech(num) + " "); System.out.println("Time Taken : " + (new Date().getTime() - preTime)); System.out.println("Memory Map: " + memo); // Try without memorizing technique.. This is not responsive for large // value .. can 50 or so.. preTime = new Date().getTime(); System.out.println("Stats with memorizing technique "); System.out.println("Fibonacci Value : " + fabWithoutMemorizingTech(num) + " "); System.out.println("Time Taken : " + (new Date().getTime() - preTime)); } }` `

input号码

40

` `fib :: Integer -> Integer fib 0 = 1 fib 1 = 1 fib n = fib (n - 1) + fib (n - 2)` `

` `fibs = 0 : 1 : zipWith (+) fibs (tail fibs)` `

` `-- declaring a matrix data Matrix = Matrix ( (Integer, Integer) , (Integer, Integer) ) deriving (Show, Eq) -- creating it an instance of Num -- so that if we implement (*) we get (^) for free instance Num Matrix where (*) = mMult -- don't need these (+) = undefined negate = undefined fromInteger = undefined abs = undefined signum = undefined -- 2-d matrix multiplication mMult :: Matrix -> Matrix -> Matrix mMult (Matrix ((a11, a12), (a21, a22))) (Matrix ((b11, b12), (b21, b22))) = Matrix ( (a11 * b11 + a12 * b21, a11 * b12 + a12 * b22) , (a21 * b11 + a22 * b21, a21 * b12 + a22 * b22) ) -- base matrix for generating fibonacci fibBase :: Matrix fibBase = Matrix ((1,1), (1,0)) -- get the large fibonacci numbers fastFib :: Int -> Integer fastFib n = let getNth (Matrix ((_, a12), _)) = a12 in getNth (fibBase ^ n)` `

` `#include<iostream> using namespace std; void fibseries(long int n) { long double x=0; double y=1; for (long int i=1;i<=n;i++) { if(i%2==1) { if(i==n) { cout<<x<<" "; } x=x+y; } else { if(i==n) { cout<<x<<" "; } y=x+y; } } } main() { long int n=0; cout<<"The number of terms "; cin>>n; fibseries(n); return 0; }` `

` `var nthFibonacci = function(n) { var arr = [0, 1]; for (; n > 1; n--) { arr.push(arr.shift() + arr[0]) } return arr.pop(); }; console.log(nthFibonacci(1200)); // 2.7269884455406272e+250` `
• 为什么这个简单的洗牌algorithm产生有偏见的结果？ 什么是一个简单的原因？
• 计算LookAtmatrix
• 评估以stringforms给出的mathexpression式
• math映射数字
• 简单的面试问题变得更加困难：给定数字1..100，find缺less的数字（s）
• 什么是空序列的算术平均值？
• “mod”和“remaining”之间有什么区别？
• 使用常数整数除法器进行高效的浮点除法
• 围绕一个双重的X有效数字
• 如何在JSP / Java中将整体和小数部分加倍？
• 计算2Dvector的交叉产品