fibonacci

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

半腔热情 提交于 2019-12-20 12:25:49
问题 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

Fibonacci calculation in Java Longs shows up negative

帅比萌擦擦* 提交于 2019-12-20 07:47:35
问题 My Fibonacci calculator works fine, but when going up to higher numbers the result comes up negative, as it would if it was an Integer over its max value. It is working with a cache java.util.Map<Integer, Long> . Everything that goes into the Map is exactly what is expected, but when printing it out I get e.g. for 291: -784134397488903422 According to http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibCalcX.html, it should be:

Why is Haskell so slow compared to C for Fibonacci sequence?

本秂侑毒 提交于 2019-12-20 07:46:40
问题 I am just beginner in Haskell. And I writing a code to display the N numbers in the Fibonacci sequence. Here is my code in Haskell, fib_seq 1 = 1:[] fib_seq 2 = 1:1:[] fib_seq n = sum(take 2 (fib_seq (n-1))):fib_seq (n-1) When I run this code for higher numbers like fib_seq 40 in GHCI, it takes a long time to evaluate it and my computer hangs and I have to interrupt. However, when I write the same exact logic in C, (I just print instead of saving it in the list), #include<stdio.h> int fib_seq

How to return an array from a function and loop through it?

*爱你&永不变心* 提交于 2019-12-20 03:51:11
问题 #include <iostream> int* fib(int); int main() { int count; std::cout<<"enter number up to which fibonacci series is to be printed"<<std::endl; std::cin>>count; int *p=new int[count]; p=fib(count); int i; for(i<0;i<=count;i++) std::cout<<p[i]<<std::endl; return 0; } int* fib(int d) { int *ar=new int[d]; int p=-1,q=1,r; int j; for(j=0;j<=d;j++) { r=p+q; ar[j]=r; p=q; q=r; } return ar; delete ar; } Why am I not able to print the whole array of Fibonacci series in this way? 回答1: Several issues

Matrix Exponentiation Algorithm for large values of N

落爺英雄遲暮 提交于 2019-12-20 02:14:22
问题 I want to calculate the Fibonacci of very large value of N ie. 10^6 with a complexity of O(logN). Here is my code but it gives the result for 10^6 in 30 seconds which is very time consuming.Help me point out the mistake.I have to give the output in modulo 10^9+7. static BigInteger mod=new BigInteger("1000000007"); BigInteger fibo(long n){ BigInteger F[][] = {{BigInteger.ONE,BigInteger.ONE},{BigInteger.ONE,BigInteger.ZERO}}; if(n == 0) return BigInteger.ZERO; power(F, n-1); return F[0][0].mod

Memoization in OCaml?

情到浓时终转凉″ 提交于 2019-12-18 17:13:35
问题 It is possible to improve "raw" Fibonacci recursive procedure Fib[n_] := If[n < 2, n, Fib[n - 1] + Fib[n - 2]] with Fib[n_] := Fib[n] = If[n < 2, n, Fib[n - 1] + Fib[n - 2]] in Wolfram Mathematica. First version will suffer from exponential explosion while second one will not since Mathematica will see repeating function calls in expression and memoize (reuse) them. Is it possible to do the same in OCaml? How to improve let rec fib n = if n<2 then n else fib (n-1) + fib (n-2);; in the same

Prevent backtracking after first solution to Fibonacci pair

天大地大妈咪最大 提交于 2019-12-18 13:36:31
问题 The term fib(N,F) is true when F is the N th Fibonacci number. The following Prolog code is generally working for me: :-use_module(library(clpfd)). fib(0,0). fib(1,1). fib(N,F) :- N #> 1, N #=< F + 1, F #>= N - 1, F #> 0, N1 #= N - 1, N2 #= N - 2, F1 #=< F, F2 #=< F, F #= F1 + F2, fib(N1,F1), fib(N2,F2). When executing this query (in SICStus Prolog), the first (and correct) match is found for N (rather instantly): | ?- fib(X,377). X = 14 ? When proceeding (by entering ";") to see if there are

Why does Java regex engine throw StringIndexOutOfBoundsException on a + repetition?

南楼画角 提交于 2019-12-18 12:54:24
问题 I've written a regex pattern to find Fibonacci numbers (it doesn't matter why, I just did). It works wonderfully as expected (see on ideone.com): String FIBONACCI = "(?x) .{0,2} | (?: (?=(\\2?)) (?=(\\2\\3|^.)) (?=(\\1)) \\2)++ . "; for (int n = 0; n < 1000; n++) { String s = new String(new char[n]); if (s.matches(FIBONACCI)) { System.out.print(n + " "); } } // 0 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 A possessive repetition (i.e. ++ on the main "loop") is crucial, because you don't

Is there a better way (performance) calculate fibonacci than this one?

不问归期 提交于 2019-12-18 12:37:31
问题 I made this code.. And I need to get the best of it.. I really need the best performance of calculating fibonacci numbers.. please help be.. I've read some code of this type of calculation and I think I got the best of them.. Avaliate this for me.. plz.. ps: And I really need the BigInteger.. I'll calculate Fibonacci of enormous numbers ps2: I have calculated some big numbers with this algorithm and I got a great response time.. but I need to know whether it could be better ps3: to run this

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

孤者浪人 提交于 2019-12-18 12:02:37
问题 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? 回答1: 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