fibonacci

Generating Fibonacci series in F#

梦想的初衷 提交于 2019-11-30 08:30:51
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 = b let b = c My first problem is that on the last statement, I'm getting an error message "Incomplete structured construct at or before this point in expression" on the last line. I don't understand what I'm doing wrong here. While this seems to be an obvious way to build the list in a fairly efficient

Print a string of fibonacci recursively in C#

你。 提交于 2019-11-30 07:33:06
Can that be done with no while loops? static void Main(string[] args) { Console.WriteLine("Please enter a number"); int number = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(" #" + Fibonacci(number)); } public static int Fibonacci(int number) { if (number <= 1) { return 1; } else { return Fibonacci(number - 2) + Fibonacci(number - 1); } } I can't even add a Console.WriteLine in the body of base case since it gets executed [number] number of times; Not sure how to do this without loops... static void Main(string[] args) { Console.WriteLine("Please enter a number"); int number =

What is the difference between `a, b = b, a+b` and `a = b; b = a+b` for fibonacci [duplicate]

不打扰是莪最后的温柔 提交于 2019-11-30 06:02:54
问题 This question already has answers here : Multiple assignment and evaluation order in Python (8 answers) Closed 3 years ago . I'm new to python so I want to ask you a question.. Previously while I was writing a fibonacci function I tryed to replace a, b = b, a+b with a = b b = a + b Believing that it was the same thing but I noted that the output is different (and wrong) Shouldn't these two codes do the same thing? Here it is the full code: #!/usr/bin/env python # -*- coding: utf-8 -*- def

Nth Fibonacci number for n as big as 10^19?

橙三吉。 提交于 2019-11-30 05:29:23
I am trying to make a program to find the nth Fibonacci number for 1 < n < 10^19. Here is my code using dynamic programming. memo = {} def fib(n): if n in memo: return memo[n] if n <= 2: f = 1 else: f = fib(n-1) + fib(n-2) memo[n]=f return f print fib(input()) % 1000000007 My code does not seem to work for large numbers. I get invalid response error. Any suggestions? Getting the Nth fibonacci number when N is 10^19 is not goign to work if you do it the naive way (at least i would guess it won't work). There's a much better way to do it. And this technique works with lots of series' like this.

Performance of Fibonacci

南楼画角 提交于 2019-11-30 05:20:08
问题 f[0] = 0; f[1] = 1; f[x_] := f[x-1] + f[x-2] This function is running slow in Mathematica and I need to increase the speed. I have to use functional programming and recursion. I'm not sure why this is running so slow, and even the slightest idea how to improve this would be helpful. 回答1: A good way to write a faster recursive function is to have it memorize previous values. This does come at the cost of memory, of course, but it can help in cases like this. In order to calculate f[x] , you

Tail Recursion Fibonacci

感情迁移 提交于 2019-11-30 05:04:05
How do I implement a recursive Fibonacci function with no loops running in O(n)? Typically I'd be against posting an answer to a homework question like this, but everything posted so far seems to be overcomplicating things. As said in the comments above, you should just use recursion to solve the problem like you would do iteratively. Here's the iterative solution: def fib(n): a, b = 0, 1 while n > 0: a, b = b, a+b n -= 1 return a Here's an equivalent recursive solution: def fib(n): def fib_help(a, b, n): return fib_help(b, a+b, n-1) if n > 0 else a return fib_help(0, 1, n) Note that in both

Recursive Fibonacci in Assembly

北慕城南 提交于 2019-11-30 04:47:21
问题 I'm attempting to implement a recursive Fibonacci program in Assembly. However, my program crashes, with an unhandled exception, and I can't seem to pick out the problem. I don't doubt that it involves my improper use of the stack, but I can't seem to point out where... .386 .model Flat public Fibonacci include iosmacros.inc ;includes macros for outputting to the screen .code Fibonacci proc MOV EAX, [EBP+8] CMP EAX, 1 JA Recurse MOV ECX, 1 JMP exit Recurse: DEC EAX MOV EDX, EAX PUSH EAX CALL

How does the recursion here work?

拜拜、爱过 提交于 2019-11-30 03:01:28
问题 Code 1: public static int fibonacci (int n){ if (n == 0 || n == 1) { return 1; } else { return fibonacci (n-1) + fibonacci (n-2); } } How can you use fibonacci if you haven't gotten done explaining what it is yet? I've been able to understand using recursion in other cases like this: Code 2: class two { public static void two (int n) { if (n>0) { System.out.println (n) ; two (n-1) ; } else { return ; } } public static void main (String[] arg) { two (12) ; } } In the case of code 2, though, n

Calculating Fibonacci Number accurately in C++?

喜你入骨 提交于 2019-11-29 22:28:06
问题 I am really confused. I am trying to calculate Fibonacci numbers, but as they get larger and large the numbers start to become wrong. and I do not know why. How do you calculate accurate Fibonacci numbers using Binet's Formula, it is my understanding that this should always return a integer? Here is what I have been trying to work with. http://ideone.com/e6t6h See as the number goes up. it gets all weird? here I print it out with a cout.precision(15); http://ideone.com/XREh2 here I print it

Fibonacci Code Golf

孤街浪徒 提交于 2019-11-29 21:25:36
Generate the Fibonacci sequence in the fewest amount of characters possible. Any language is OK, except for one that you define with one operator, f , which prints the Fibonacci numbers. Starting point: 25 14 characters in Haskell : f=0:1:zipWith(+)f(tail f) f=0:scanl(+)1f Callum Rogers RePeNt, 9 , 8 chars 1↓[2?+1] Or 10 chars with printing: 1↓[2?+↓£1] Run using: RePeNt "1↓[2?+1]" RePeNt is a stack based toy language I wrote (and am still improving) in which all operators/functions/blocks/loops use Reverse Polish Notation (RPN). Command Explanation Stack ------- ----------- ----- 1 Push a 1