fibonacci

Sum of Fibonacci numbers

試著忘記壹切 提交于 2019-12-05 15:55:12
I'm rather new to Haskell. The problem is to find the sum of all even Fibonacci numbers not greater than 4 million. I can't use lists. If I understand correctly, the below solution is wrong, because it uses lists: my_sum = sum $ filter (odd) $ takeWhile (< 4000000) fibs Where fibs is the list of all Fibonacci numbers. Somehow, I find it difficult not to think in Haskell in terms of lists. Could anyone guide me to a solution to this problem? Regards EDIT: If anyone is interested, I've solved this problem. Here is the code (very clumsy-looking, but works nevertheless): findsum threshold =

Python variable assignment question

折月煮酒 提交于 2019-12-05 13:32:46
a,b = 0,1 while b < 50: print(b) a = b b = a+b outputs: 1 2 4 8 16 32 wheras: a,b = 0,1 while b < 50: print(b) a,b = b, a+b outputs (correct fibonacci sequence): 1 1 2 3 5 8 13 21 34 Aren't they the same? I mean a,b = b, a+b is essentially the same as a = b and b = a+b written separately -- no? No, they are not the same. When you write a,b = b, a+b , the assignments are done "simultaneously". a,b = b, a+b is same as (a, b) = (b, a+b) . So, after a, b = 5, 8 a=5 and b=8. When Python sees this (a, b) = (b, a+b) it first calculates the right side (b, a+b) which is (8,13) and then assigns (this

Index of a really big Fibonacci Number

给你一囗甜甜゛ 提交于 2019-12-05 13:05:05
I need to calculate the index of a Fibonacci number with JavaScript, within the Fibonacci sequence. I need to do this without using recursion, or a loop. I found the following formula in the Math forum : n=⌊logφ(F⋅5√+12)⌋ and coded it in JavaScript: function fibIndex(fib) { fib = BigNumber(fib); return logBasePhi(fib.times(Math.sqrt(5)).plus((1/2))); } function phi() { return (1 + Math.sqrt(5))/ 2; } function getBaseLog(x, y) { return Math.log(y) / Math.log(x); } function logBasePhi(x) { return getBaseLog(phi(), x); } Notice the .times() and .plus() functions that are part of this BigNumber

Fibonacci Sequence (JS) - Sum of Even Numbers

早过忘川 提交于 2019-12-05 07:36:15
I started Project Euler. I am on problem 2 and came up with this code to come up with the sum of even fibonacci numbers up to 4 million. The code seems to do pretty much what I want it to. I do see the correct sum listed when the code is ran. The only part I am really confused about is the very last number displayed in the results. This is what it shows: JS CODE: var previous = 0; var current = 1; var sum = 0; var next; for(i = 1; i < 100; i++){ next = current + previous; previous = current; current = next; if(current % 2 === 0 && current < 4000000) { sum += current; console.log(sum); } }

C print first million Fibonacci numbers

拟墨画扇 提交于 2019-12-05 04:31:21
I am trying to write C code which will print the first 1million Fibonacci numbers. The actual problem is I want to get the lats 10 digits of F(1,000,000) I understand how the sequence works and how to write the code to achieve that however as F(1,000,000) is very large I am struggling to find a way to represent it. This is code I am using: #include<stdio.h> int main() { unsigned long long n, first = 0, second = 1, next, c; printf("Enter the number of terms\n"); scanf("%d",&n); printf("First %d terms of Fibonacci series are :-\n",n); for ( c = 0 ; c < n ; c++ ) { if ( c <= 1 ) next = c; else {

In java, how would I find the nth Fibonacci number?

∥☆過路亽.° 提交于 2019-12-05 01:27:50
问题 Determining the Fibonacci sequence is easy enough to figure out: int num = 0; int num2 = 1; int loop; int fibonacci; System.out.print(num2); for (loop = 1; loop <= 10; loop ++) { fibonacci = num + num2; num = num2; num2 = fibonacci; System.out.print(" " + fibonacci); } My problem lies with trying to pinpoint the value for a specified N. As in, If I want to find the 6th element in the sequence, which is 8, how would I find that number, and only that number? 回答1: In your code, num starts as the

Calculating nth fibonacci number using the formulae in python

你。 提交于 2019-12-05 00:11:57
问题 I am calculating the n-th fibonacci number using (a) a linear approach, and (b) this expression Python code: 'Different implementations for computing the n-th fibonacci number' def lfib(n): 'Find the n-th fibonacci number iteratively' a, b = 0, 1 for i in range(n): a, b = b, a + b return a def efib(n): 'Compute the n-th fibonacci number using the formulae' from math import sqrt, floor x = (1 + sqrt(5))/2 return long(floor((x**n)/sqrt(5) + 0.5)) if __name__ == '__main__': for i in range(60,80)

fibonacci works in python but fails in Java

人走茶凉 提交于 2019-12-04 22:31:06
I have this code for calculating fibonacci number in python . It works and gives the expected result. but when I translated the same to Java , it fails. Any idea of what is going wrong here? In python : def fib3(n): a,b=0,1 while n>0: a,b=b,a+b n-=1 return a fib3(12) --> 144 In Java : public static int fib2(int n){ int a = 0; int b =1; while(n-- >0){ a=b; b=a+b; } return a; } fib2(12) --> 2048 In this section: a=b; b=a+b; you're assigning b to a+b , but a is already b . So really you're doubling b Easiest solution is a temp variable: public static int fib2(int n){ int a = 0; int b =1; while(n-

Improve C++ Fibonacci series

江枫思渺然 提交于 2019-12-04 19:54:16
I know that: int fib(int n) { if (n == 0 || n == 1) return 1; return fib(n − 1)+ fib(n − 2); } when n=5,fib(5) evaluates as: fib(5) fib(4) + fib(3) (fib(3) + fib(2)) + (fib(2) + fib(1)) ((fib(2) + fib(1)) + (fib(1) + fib(0))) + ((fib(1) + fib(0)) + fib(1)) (((fib(1) + fib(0)) + fib(1)) + (fib(1) + fib(0))) + ((fib(1) + fib(0)) + fib(1)) Notice that each base element being used for several times, is there a way to use map to store the previous value and simply do fib(n − 1) + fib(n − 2)? In C++, the two solutions at your disposal that would save you time are the dynamic programming approach and

Iterative Version of Modified Fibonacci Sequence

旧城冷巷雨未停 提交于 2019-12-04 18:44:42
I was just going through the iterative version of fibonacci series algorithm. I found this following code int Fibonacci(int n) { int f1 = 0; int f2 = 1; int fn; for ( int i = 2; i < n; i++ ) { fn = f1 + f2; f1 = f2; f2 = fn; } } A silly question just raised in my mind. The function above adds two previous numbers and returns the third one and then get variables ready for the next iteration. What if it would be something like this. "Return a number of series which is the sum of previous three numbers" how we can change the above code to find such a number.u As a hint, notice that the above