fibonacci

PHP: Can someone explain how this code works? (Fibonacci)

I promise this isn't homework. I'm just a curious novice. How does this: function f($i){return $i<2?$i:f($i-1)+f($i-2);} (written by someone smart) produce the same result as this function fibonacci($n, $arr = array(0,1)){ $arr[] = $arr[(count($arr) - 1)] + $arr[(count($arr) - 2)]; if (count($arr) == $n) return $arr[$n - 1]; els...

Fibonacci Sequence Algorithm

I am attempting to find the first number in the fibbonacci sequence to contain N digits (N being somewhere in the range of 500 and 2000). I attempt to do this with the following code: BigInteger num = BigInteger.valueOf(2); BigInteger num1 = BigInteger.ONE; BigInteger num2 = BigInteger.ONE; int record = 0; BigInteger TEN = BigInteger.va...

Help with LINQ Expression

How to write a LINQ Expression (method call syntax preferred) that gives a list of fibonacci numbers lying within a certain range, say 1 to 1000 ? ...

Calculating Fibonacci Numbers Recursively in C

I'm trying to learn C by writing a simple program to output Fibonacci numbers. It isn't working. fibonacci.h unsigned int fibonacci_recursive(unsigned int n); fibonacci.c #include <stdio.h> #include "fibonacci.h" main() { unsigned int i; for (i = 0; i < 10; i++) { printf("%d\t%n", fibonacci_recursive(i)); } ...

How I do fibonaci sequence under 1000?

#include <iostream> using namespace std; void main() { int i = 0; while (i < 1000) { int TEMP = i * 2; cout << i << endl; TEMP = i; i = i +1; // ??? } return; } I'm so confused?? :( ...

Why is .NET faster than C++ in this case?

Make sure you run outside of the IDE. That is key. -edit- I LOVE SLaks comment. "The amount of misinformation in these answers is staggering." :D Calm down guys. Pretty much all of you were wrong. I DID make optimizations. It turns out whatever optimizations I made wasn't good enough. I ran the code in GCC using gettimeofday (I'll past...

Code Chess: Fibonacci Sequence

Building upon the proven success of Code Golf, I would like to introduce Code Chess. Unlike Code Golf, which strives for concision, Code Chess will strive for cleverness. Can you create a clever or unexpected Fibonacci generator? Your code must print or return either the nth Fibonacci number or the first n Fibonacci numbers. ...

Solving Recursion in fibonacci numbers

I'm unaware of the mathematics in this algorithm and could use some help. Algorithm: if n<2 then return n else return fibonacci(n-1) + fibonacci(n-2) Statement n < 2 is O(1) Time n >=2 is O(1) Return n is O(1) Time n>=2 is - Return fib(n-1) + fib(n-2) is - and time n>=2 is T(n-1) + T(n-2) +O(1) Total: ...

How did this get 8?

Here's the code: class qual { public static int fibonacci(int n) { if (n == 0 || n == 1) { return 1; } else { return fibonacci(n-1) + fibonacci(n-2); } } public static void main(String[] arg) { System.out.println(fibonacci(5)...

Test if a number is fibonacci

I know how to make the list of the Fibonacci numbers, but i don't know how can i test if a given number belongs to the fibonacci list - one way that comes in mind is generate the list of fib. numbers up to that number and see if it belongs to the array, but there's got to be another, simpler and faster method. Any ideas ? ...

How many additional function calls does fib(n) require if "LINE 3" is removed?

I just got this question on an interview and had no idea how to calculate the answer. How many additional function calls does fib(n) require if "LINE 3" is removed? The answer should be in terms on n. int fib(int n) { if(n == 0) return 0; if(n == 1) return 1; if(n == 2) return 1; //LINE 3 HERE <--- return fib(n - 1) + fib(n - 2...

Properties of bad fibonacci algorithm

I was looking at the canonical bad fibonacci algorithm the other day: public static int fib(int n) { // Base Case if (n < 2) return 1; else return fib(n-1) + fib(n-2); } I made the interesting observation. When you call fib(n), then for k between 1 and n fib(k) is called precisely fib(n-k+1) times (or ...

Sum of even fibonacci numbers

This is a Project Euler problem. If you don't want to see candidate solutions don't look here. Hello you all! im developping an application that will find the sum of all even terms of the fibonacci sequence. The last term of this sequence is 4,000,000 . There is something wrong in my code but I cannot find the problem since it makes se...

Fibonacci using SBN in OISC in Machine Language

Hello, I want to generate fibonacci series using SBN in an OISC architecture. My initial approach is to implement it in assembly language first and then convert it to machine language. The first steps involve storing 0 and 1 in 2 registers and then subtract 1 from 0 and repeatedly subtract 1 in the consequent steps. Everytime it will ge...

Sum of Fibonacci numbers

Hi, I'm rather new to Haskell. The problem is to find the sum of all even Fibonacci numbers not greater than 4 million. I can't use lists. If I understand correctly, the below solution is wrong, because it uses lists: my_sum = sum $ filter (odd) $ takeWhile (< 4000000) fibs Where fibs is the list of all Fibonacci numbers. Somehow,...

Generalizing Fibonacci sequence with SICStus Prolog

I'm trying to find a solution for a query on a generalized Fibonacci sequence (GFS). The query is: are there any GFS that have 885 as their 12th number? The initial 2 numbers may be restricted between 1 and 10. I already found the solution to find the Nth number in a sequence that starts at (1, 1) in which I explicitly define the initia...

Generating Fibonacci series in F#

I'm just starting to learn F# using VS2010 and below is my first attempt at generating the Fibonacci series. What I'm trying to do is to build a list of all numbers less than 400. let fabList = let l = [1;2;] let mutable a = 1 let mutable b = 2 while l.Tail < 400 do let c = a + b l.Add(c) let a...

Fibonacci in C works great with 1 - 18 but 19 does nothing at all...

Yeah right... we are forced to programm some good old C at our university... ;) So here's my problem: We got the assignment to program a little program that show a fibonacci sequence from 1 to n 1 to 18 works great. But from 19 the program does nothing at all and just exit as it's done. I can not find the error... so please give me a hi...

Whats the point of lazy-seq in clojure?

I am looking through some example Fibonacci sequence clojure code: (def fibs (lazy-cat [1 2] (map + fibs (rest fibs)))) I generally understand what is going on, but don't get the point of lazy-cat. I know that lazy-cat is a macro that is translating to something like this: (def fibs (concat (lazy-seq [1 2]) (lazy-seq (map + fibs ...

Unsigned Long Long Won't Go Beyond The 93th Fibonacci Number?

Here's the code I wrote for finding the n-th Fibonacci number: unsigned long long fib(int n) { unsigned long long u = 1, v = 1, t; for(int i=2; i<=n; i++) { t = u + v; u = v; v = t; } return v; } While the algorithm runs pretty quickly, the output starts to freak out when n>93. I think/kno...