fibonacci

Time Complexity of Fibonacci Series

时光怂恿深爱的人放手 提交于 2019-12-12 03:59:17
问题 long int F(int n){ long int F[n]; if (n<2) return n; else { F[0]=0; F[1]=1; for (int i=2; i<n+1; i++) F[i]=F[i-1]+F[i-2]; return F[n]; } } Hi guys, can anyone know how to compute the time complexity of the function above? I am studying C++ and I am quite suffering about compute time complexity of a random algorithm. Please help me! Thanks in advance. 回答1: The code shown relies on a g++ language extension, variable length arrays. I.e. it's not standard C++. The code also misdirects a little by

How can I get the sum of all odd fibonacci vales in javaScript?

好久不见. 提交于 2019-12-12 03:48:06
问题 I am working through this Free Code Camp exercise. Return the sum of all odd Fibonacci numbers up to and including the passed number if it is a Fibonacci number. The first few numbers of the Fibonacci sequence are 1, 1, 2, 3, 5 and 8, and each subsequent number is the sum of the previous two numbers. And here is what I have so far... Any suggestions? function sumFibs(num) { var arr, isFibVal, isEvenVal, sum, i = 0, fibonacci = function (num){ var a, b, result, fibArr = [1]; a=0; b=1; result=b

Calculating a Fibonacci number with array

僤鯓⒐⒋嵵緔 提交于 2019-12-11 23:18:22
问题 // I am learning about recursion in Java. /** I am trying to calculate the 45th Fibonacci number by using an array to shorten the time used, which does not work out well... error message: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 45 at Auf1.fib2(Auf1.java:25) at Auf1.main(Auf1.java:49) **/ public class Auf1 { public static long[] feld; public static long fib2(long n) { if ((n == 1) || (n == 2)) { return 1; } else { if (feld[(int) n] != -1) { return feld[(int) n]; }

Fibonacci, but with different starting numbers. Code works in most cases, but not all

吃可爱长大的小学妹 提交于 2019-12-11 18:23:41
问题 So I have an exercise of finding a Fibonacci sequence but with different starting number and I can't figure out why my code works most of the time, but not in some cases. My code is based on a formula (link: https://www.geeksforgeeks.org/program-for-nth-fibonacci-number/) If n is even then k = n/2: F(n) = [2*F(k-1) + F(k)]*F(k) If n is odd then k = (n + 1)/2 F(n) = F(k)*F(k) + F(k-1)*F(k-1) - #include <map> #include <iostream> #include <cmath> using namespace std; #define long long long map

Recursive fibonacci Assembly

允我心安 提交于 2019-12-11 17:56:57
问题 Today I wrote a recursive fibonacci in assembly and it doesn't work. I compiled it to object file with NASM and than made it elf with gcc. When I enter 1 or 2 the function works perfectly, but when I enter 3, 4, 5, 6, or more the function doesn't work. I think there is problem where the function calls itself. This the code: SECTION .data ;init data str: db "This equal: %d",10,0 SECTION .text ;asm code extern printf global main main: push ebp mov ebp,esp ;-------------------- push 03 ; the

Generate Fibonacci Number without ANY looping / recursion

两盒软妹~` 提交于 2019-12-11 15:13:16
问题 Using ES5 as pseudocode / example: var gr = 1.61803398875; function fib(v) { // fib without recursion if(v < 2) return v; return Math.round(((v-2) + (v-1)) * gr); } function fibr(v) { // fib with recursion if(v < 2) return v; return fibr(v-2) + fibr(v-1); } console.clear(); console.log(fib(0), fibr(0)); // 0 0 console.log(fib(1), fibr(1)); // 1 1 console.log(fib(2), fibr(2)); // 2 1 console.log(fib(3), fibr(3)); // 5 2 console.log(fib(4), fibr(4)); // 8 3 console.log(fib(5), fibr(5)); // 11 5

Is the Fibonacci lattice the very best way to evenly distribute N points on a sphere? So far it seems that it is the best

狂风中的少年 提交于 2019-12-11 13:48:15
问题 Over in the thread "Evenly distributing n points on a sphere" this topic is touched upon: Evenly distributing n points on a sphere. But what I would like to know is: "Is the Fibonacci lattice the very best way to evenly distribute N points on a sphere? So far it seems that it is the best. Does anyone know of a better method?" I have a Ph.D. in physics and may have an application for some of this research in physics. I came across this wonderful paper: http://arxiv.org/pdf/0912.4540.pdf

Very Large Fibonacci in Java

╄→гoц情女王★ 提交于 2019-12-11 13:28:32
问题 I am trying to rapidly calculate large Fibonacci numbers. Here is my code. It is prohibitively slow for numbers above 1 million, how can it be improved? public static BigInteger fib(BigInteger n) { int k = n.intValue(); BigInteger ans = null; if(k == 0) { ans = new BigInteger("0"); } else if(Math.abs(k) <= 2) { ans = new BigInteger("1"); } else { BigInteger km1 = new BigInteger("1"); BigInteger km2 = new BigInteger("1"); for(int i = 3; i <= Math.abs(k); ++i) { ans = km1.add(km2); km2 = km1;

Fibonacci function not accepting 0 and not displaying the last term only

陌路散爱 提交于 2019-12-11 13:10:07
问题 I wrote a function that displays the Fibonacci sequence up to the nth term. The code runs fine, but I want to make two changes and am not sure how to do so. Here is my code: function [ F ] = get_fib( k ) F(1) = 1; F(2) = 1; i = 3; while k >= i; F(i) = F(i-1) + F(i-2); i = i + 1; end end The first problem is that the code does not accept 0 as an input. I tried changing the function to: function [ F ] = get_fib( k ) F(0) = 0; F(1) = 1; F(2) = 1; i = 3; while k >= i; F(i) = F(i-1) + F(i-2); i =

Handling large integers in Java without using BigInteger

旧街凉风 提交于 2019-12-11 12:23:37
问题 I am currently working on an assignment that involves calculating Fibonacci numbers up to F(300) using a linear time complexity algorithm. I know via a quick search that a Java long will overflow at around F(93), so I am trying to figure out how to store such a large number. However, our assignment states that we are not allowed to use language libraries such as BigInteger to store our numbers. We must write our own large integer data type or object. What exactly am I to do in this case? I