fibonacci

Scala unit type, Fibonacci recusive depth function

为君一笑 提交于 2021-01-27 18:30:47
问题 So I want to write a Fibonacci function in scala that outputs a tree like so: fib(3) | fib(2) | | fib(1) | | = 1 | | fib(0) | | = 0 | = 1 | fib(1) | = 1 = 2 and my current code is as follows: var depth: Int = 0 def depthFibonacci(n:Int, depth: Int): Int={ def fibonnaciTailRec(t: Int,i: Int, j: Int): Int = { println(("| " * depth) + "fib(" + t + ")") if (t==0) { println(("| " * depth) + "=" + j) return j } else if (t==1) { println (("| " * depth) + "=" + i) return i } else { depthFibonacci(t-1

Why this dynamic version of Fibonacci program is incredibly faster then this other? Prolog solutions

*爱你&永不变心* 提交于 2021-01-27 16:11:48
问题 I am learning Prolog using SWI Prolog and I have a doubt about the followings two solutions of the Fibonacci number calculation program: The first one is this: fib(1,1). fib(2,1). fib(N,F) :- N > 2, N1 is N-1, fib(N1,F1), N2 is N-2, fib(N2,F2), F is F1+F2. It is pretty clear for me hw it work, it is very simple. Then I have this second version that, reading the code, seems to work as the previous one but after that it have calculate the Fibonacci number of N save it in the Prolog database by

Why this dynamic version of Fibonacci program is incredibly faster then this other? Prolog solutions

北城余情 提交于 2021-01-27 16:11:34
问题 I am learning Prolog using SWI Prolog and I have a doubt about the followings two solutions of the Fibonacci number calculation program: The first one is this: fib(1,1). fib(2,1). fib(N,F) :- N > 2, N1 is N-1, fib(N1,F1), N2 is N-2, fib(N2,F2), F is F1+F2. It is pretty clear for me hw it work, it is very simple. Then I have this second version that, reading the code, seems to work as the previous one but after that it have calculate the Fibonacci number of N save it in the Prolog database by

Why this dynamic version of Fibonacci program is incredibly faster then this other? Prolog solutions

你离开我真会死。 提交于 2021-01-27 15:13:39
问题 I am learning Prolog using SWI Prolog and I have a doubt about the followings two solutions of the Fibonacci number calculation program: The first one is this: fib(1,1). fib(2,1). fib(N,F) :- N > 2, N1 is N-1, fib(N1,F1), N2 is N-2, fib(N2,F2), F is F1+F2. It is pretty clear for me hw it work, it is very simple. Then I have this second version that, reading the code, seems to work as the previous one but after that it have calculate the Fibonacci number of N save it in the Prolog database by

Unexpected Negative Numbers in Java

谁都会走 提交于 2021-01-27 12:11:48
问题 import java.util.*; public class Prac9FibonacciNumbers { public static void main(String[] args) { int[] x = new int[100]; x[0] = 1; x[1] = 1; for (int a = 2; a < 100; a++) { x[a] = x[a - 1] + x[a - 2]; } for (int a = 0; a < 100; a++) { if(a < 99){ System.out.print(x[a] + ","); } else{ System.out.print(x[a]); } } } } This program is meant to create a list of Fibonacci numbers. However, for some reason, it is giving me negative numbers right in the middle of my output. I could use Math.abs()

Hughes' Fibonacci stream

戏子无情 提交于 2021-01-26 08:23:12
问题 I am trying to understand the "Streams as arrows" section in John Hughes' famous "Generalising Arrows to Monads". To be more precise, I am interested in writing down the Fibonacci stream. I tweaked Hughes' definition a bit: data StreamProcessor a b = Get (a -> StreamProcessor a b) | Put b (StreamProcessor a b) | Halt put = Put get = Get First of all, I treat stream processors as lists which may block (waiting for input). That is: Put :: b -> StreamProcessor a b -> StreamProcessor a b matches

Understanding recursion with the Fibonacci Series

别等时光非礼了梦想. 提交于 2020-12-27 08:59:06
问题 I am trying to better understand recursion and how return statements work. As such, I'm looking at a piece of code meant to identify the fibonacci number associated with a given term -- in this case, 4. I'm have difficulty understanding the else statement. def f(n): if n == 0: return 0 if n == 1: return 1 else: return f(n-1) + f(n-2) f(4) I have tried using Visualize Python to examine what happens at each step, but I get lost when it hits the else statement. It looks like it is taking the

What are overlapping subproblems in Dynamic Programming (DP)?

爷,独闯天下 提交于 2020-12-13 07:04:47
问题 There are two key attributes that a problem must have in order for dynamic programming to be applicable: optimal substructure and overlapping subproblems [1]. For this question, we going to focus on the latter property only. There are various definitions for overlapping subproblems , two of which are: A problem is said to have overlapping subproblems if the problem can be broken down into subproblems which are reused several times OR a recursive algorithm for the problem solves the same

Last Digit of the Sum of Fibonacci Numbers

我怕爱的太早我们不能终老 提交于 2020-12-04 14:33:28
问题 I am trying to find the last digit of sum of Fibonacci Series. I calculate the sum as F(n+2) - 1 . The below code is working fine but it is slow for large numbers (e.g 99999 ). How can I optimize this? n = int(input()) def last_digit(n): a, b = 0, 1 for i in range(n+2): a, b = b, a + b return (a-1) % 10 print(last_digit(n)) 回答1: Look at this table: http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibtable.html notice that fib(60) last digit is 0 and fib(61) last digit is 1 , that

Last Digit of the Sum of Fibonacci Numbers

别等时光非礼了梦想. 提交于 2020-12-04 14:26:15
问题 I am trying to find the last digit of sum of Fibonacci Series. I calculate the sum as F(n+2) - 1 . The below code is working fine but it is slow for large numbers (e.g 99999 ). How can I optimize this? n = int(input()) def last_digit(n): a, b = 0, 1 for i in range(n+2): a, b = b, a + b return (a-1) % 10 print(last_digit(n)) 回答1: Look at this table: http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibtable.html notice that fib(60) last digit is 0 and fib(61) last digit is 1 , that