fibonacci

Making Fibonacci faster [duplicate]

两盒软妹~` 提交于 2019-12-03 05:35:51
问题 This question already has answers here : nth fibonacci number in sublinear time (14 answers) Closed 4 years ago . I was required to write a simple implementation of Fibonacci's algorithm and then to make it faster . Here is my initial implementation public class Fibonacci { public static long getFibonacciOf(long n) { if (n== 0) { return 0; } else if (n == 1) { return 1; } else { return getFibonacciOf(n-2) + getFibonacciOf(n-1); } } public static void main(String[] args) { Scanner scanner =

Help with LINQ Expression

和自甴很熟 提交于 2019-12-03 03:48:10
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 ? OK; for a more "FP" answer: using System; using System.Collections.Generic; using System.Linq; static class Program { static void Main() { Func<long, long, long, IEnumerable<long>> fib = null; fib = (n, m, cap) => n + m > cap ? Enumerable.Empty<long>() : Enumerable.Repeat(n + m, 1).Concat(fib(m, n + m, cap)); var list = fib(0, 1, 1000).ToList(); } } Note that in theory this can be written as a single lambda, but that is very hard . Marc Gravell

Looping through a formula that describes a spiral to generate XY coordinates

前提是你 提交于 2019-12-03 02:46:44
I'm trying to generate a spiral galaxy in the form of xy (2D) coordinates -- but math is not my strong suit. I've gleaned the following from an excellent source on spirals: The radius r(t) and the angle t are proportional for the simpliest spiral, the spiral of Archimedes. Therefore the equation is: (3) Polar equation: r(t) = at [a is constant]. From this follows (2) Parameter form: x(t) = at cos(t), y(t) = at sin(t), (1) Central equation: x²+y² = a²[arc tan (y/x)]². This question sort of touched upon galaxy generation, but the responses were scattered and still overly complex for what I need

a recursive Fibonacci function in Clojure

北城以北 提交于 2019-12-03 02:42:36
问题 I'm a newcomer to clojure who wanted to see what all the fuss is about. Figuring the best way to get a feel for it is to write some simple code, I thought I'd start with a Fibonacci function. My first effort was: (defn fib [x, n] (if (< (count x) n) (fib (conj x (+ (last x) (nth x (- (count x) 2)))) n) x)) To use this I need to seed x with [0 1] when calling the function. My question is, without wrapping it in a separate function, is it possible to write a single function that only takes the

Determining the individual letters of Fibonacci strings?

假装没事ソ 提交于 2019-12-03 00:23:07
The Fibonacci strings are defined as follows: The first Fibonacci string is "a" The second Fibonacci string is "bc" The (n + 2)nd Fibonacci string is the concatenation of the two previous Fibonacci strings. For example, the first few Fibonacci strings are a bc abc bcabc abcbcabc The goal is, given a row and an offset, to determine what character is at that offset. More formally: Input: Two integers separated by a space - K and P(0 < K ≤ 10 9 ), ( < P ≤ 10 9 ), where K is the line number of the Fibonacci string and P is the position number in a row. Output: The desired character for the

Algorithm function for fibonacci series [closed]

烂漫一生 提交于 2019-12-03 00:15:52
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center . I'm not looking necessarily for an answer, but I am looking for what this question is asking of. Found this question studying for an interview but not sure what they're asking? Write function that runs through the Fibonacci sequence and returns the index that is passed in as a parameter. amin k firstly,you can update your base math

Why am i not able to print 47th fibonacci number correctly?

不想你离开。 提交于 2019-12-02 23:52:06
问题 I am using 64 bit operating system ,then also i am not able to print 46th fibonacci number correctly which is less than 4 billion. #include<cs50.h> #include<stdio.h> int main(void) { unsigned int n=50; int array[n]; array[0]=0; array[1]=1; printf("%i\n",array[0]); printf("%i\n",array[1]); for(int i=2;i<n;i++) { array[i]=array[i-1]+array[i-2]; printf("%i\n",array[i]); } 回答1: You have to use long long as your data type of the array. because You are going to store out-range numbers of the

Why is a Fibonacci heap called a Fibonacci heap?

跟風遠走 提交于 2019-12-02 20:41:03
The Fibonacci heap data structure has the word "Fibonacci" in its name, but nothing in the data structure seems to use Fibonacci numbers. According to the Wikipedia article: The name of Fibonacci heap comes from Fibonacci numbers which are used in the running time analysis. How do these Fibonacci numbers arise in the Fibonacci heap? The Fibonacci heap is made up of a collection of smaller heap-ordered trees of different "orders" that obey certain structural constraints. The Fibonacci sequence arises because these trees are constructed in a way such that a tree of order n has at least F n+2

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

浪子不回头ぞ 提交于 2019-12-02 19:32:36
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 paste code below) and used g++ -O2 file.cpp and got slightly faster results then C#. Maybe MS didn't create the optimizations needed in this specific case but after downloading and installing mingw I was tested and found the speed to be near identical. Justicle Seems

How many ways are there to describe the Fibonacci sequence in Perl 6?

馋奶兔 提交于 2019-12-02 18:13:20
I've been looking at the various ways of constructing lazy lists in Perl 6 and I would like to collect all of the concise ways of describing the Fibonacci sequence. I will start this off with the three from masak 's journal: my @fibs := (0, 1, -> $a, $b { $a + $b } ... *); my @fibs := (0, 1, { $^a + $^b } ... *); my @fibs := (0, 1, *+* ... *); I was thinking something like this would also work, but I think I have the syntax wrong: my @fibs := (0, 1, (@fibs Z+ @fibs[1..*])); Something there is eager (the slice?) and causes Rakudo to enter an infinite loop. It's a translation of the Haskell