fibonacci

Fibonacci One-Liner

纵然是瞬间 提交于 2019-11-28 17:04:56
I'm trying to solve questions from Project Euler in Ruby one-liners, and I'm curious if there's a more elegant solution for question two : Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. Here is my one line solution in Ruby: (1..32).inject([0,1]) {|arr, i| (arr << arr[-1] + arr[-2] if arr[-1] + arr[-2] <= 4000000) || arr}.inject(0) {

Step by Step explanation of this code [closed]

徘徊边缘 提交于 2019-11-28 14:53:38
def mystery(n): a, b = 0, 1 while a < n: print (a) a, b = b, a + b If someone could give me a line by line explanation of this code, as also inform of why it will not run, and what extra code needs to be added. def mystery(n): # define a function named "mystery", that takes one argument called "n" a, b = 0, 1 # make a variable named "a" and set it to 0; make a variable named "b" and set it to 1 while a < n: # as long as "a" is smaller than "n" print (a) # display the value contained in "a" on the screen a, b = b, a + b # set "b" to the sum of "a" and "b"; set "a" to the old value of "b"

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

自作多情 提交于 2019-11-28 14:32:53
This question already has an answer here: Multiple assignment and evaluation order in Python 8 answers 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 main(args): fibonacci(1000) return 0 def fibonacci(n): a, b = 0, 1 while b < n: print b, a, b = b, a+b # if I comment this and

Python - Difference between variable value declaration on Fibonacci function

痞子三分冷 提交于 2019-11-28 14:17:32
I'm kind of beginner in python. I was looking at one the types to make a fibonacci function, def fib(n): a=0 b=1 while a<n: print a a,b=b,a+b and I saw the a,b=b,a+b declaration. So, I thought a=b and b=a+b were the same to a,b=a,b+a, so I changed the function for it to be like this: def fib(n): a=0 b=1 while a<n: print a a=b b=a+b and I thought it would be right, but when I executed the program, I got a different output. Can someone explain to me the difference between those two types of declaration? Thanks, anyway. b, a+b creates a tuple containing those two values. Then a, b = ... unpacks

Understanding the Fibonacci sequence

…衆ロ難τιáo~ 提交于 2019-11-28 12:57:38
问题 I have an algorithm that I found online that calculates the Fibonacci sequence. I feel a bit like a fool but I have no idea how it works! def fib(n) if n == 0 || n == 1 return n end if n >= 2 return fib(n-1) + fib(n-2) end end If I call the method with the argument of 10 why does it not return 18? I assume some recursion is happening here but I have no idea. Can someone help me understand this? 回答1: Let's look at fib(4) , according to your code above: fib(4) #=> 3 It does so by using the

How to fix my Fibonacci stream in Scala

混江龙づ霸主 提交于 2019-11-28 12:11:31
I defined a function to return Fibonacci stream as follows: def fib:Stream[Int] = { Stream.cons(1, Stream.cons(2, (fib zip fib.tail) map {case (x, y) => println("%s + %s".format(x, y)); x + y})) } The functions work ok but it looks inefficient (see the output below) scala> fib take 5 foreach println 1 2 1 + 2 3 1 + 2 2 + 3 5 1 + 2 1 + 2 2 + 3 3 + 5 8 So, it looks like the function calculates the n-th fibonacci number from the very beginning. Is it correct? How would you fix it? oxbow_lakes That is because you have used a def . Try using a val : lazy val fib: Stream[Int] = 1 #:: 2 #:: (fib zip

Fibonacci numbers becoming negative after a certain term

邮差的信 提交于 2019-11-28 11:32:59
问题 I wrote this program in Fortran to display the Fibonacci numbers up to the x-th term: program fibonacci implicit none integer :: x,p,c,i,t !initializes limit, previous, current, iterative, and temp print *, "List the first x fibonacci numbers: " read *, x !reads the limit p=0 !sets previous to zero c=1 !sets current to 1 do i=1,x print *, c !prints the current fibonacci number t = c !sets the temporary variable to the current c = c + p !sets the current to the current plus the previous p = t

Fibonacci numbers with OpenMP tasks

拜拜、爱过 提交于 2019-11-28 11:30:58
问题 Is there any benefit by using OpenMP to parallelize the Fibonacci number calculations? There are several examples online which calculate Fibonacci numbers using the task directive in OpenMP. For example at http://docs.oracle.com/cd/E19205-01/820-7883/girtd/index.html and here http://openmp.org/forum/viewtopic.php?f=3&t=1231 Some of these examples claim the performance is better with OpenMP. I don't understand this as calculating the Fibonacci series is, to my understanding, fundamentally non

Parallel Fibonacci Number Calculator

心已入冬 提交于 2019-11-28 10:56:48
问题 I'm using Task Parallel Library (TPL ) for calculating Fibonacci number. Program is given below: public static int Fib(int n) { if (n <= 1) { return n; } Task<int> task = Task.Factory.StartNew<int>(() => Fib(n - 1)); var p = Fib(n - 2); return task.Result + p; } public static void Main(string[] args) { Stopwatch watch = new Stopwatch(); watch.Start(); Console.WriteLine("Answer: " + Fib(44)); watch.Stop(); Console.WriteLine("Time: " + watch.ElapsedMilliseconds); } } Unfortunately this program

Fibonacci function

爱⌒轻易说出口 提交于 2019-11-28 08:35:55
问题 We have been given a task, which we just can't figure out: Write an R function which will generate a vector containing the first n terms of the Fibonacci sequence. The steps in this are as follows: (a) Create the vector to store the result in. (b) Initialize the first two elements. (c) Run a loop with i running from 3 to n, filling in the i-th element Work so far: vast=function(n){ vast=vector() vast[1]=1 vast[2]=1 for(i in 3){vast[i]=vast[i-1]+vast[i-2]} } All we end up is with the error: