fibonacci

Sum of even fibonacci numbers

妖精的绣舞 提交于 2019-11-28 08:22:14
问题 This is a Project Euler problem. If you don't want to see candidate solutions don't look here. Hello you all! I'm developing an application that will find the sum of all even terms of the fibonacci sequence. The last term of this sequence is 4,000,000 . There is something wrong in my code but I cannot find the problem since it makes sense to me. Can you please help me? using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program {

Calculate the Fibonacci number (recursive approach) in compile time (constexpr) in C++11

允我心安 提交于 2019-11-28 07:47:29
I wrote the program Fibonacci number calculation in compile time (constexpr) problem using the template metaprogramming techniques supported in C++11. The purpose of this is to calculate the difference in the run-time between the template metaprogramming approach and the old conventional approach. // Template Metaprograming Approach template<int N> constexpr int fibonacci() {return fibonacci<N-1>() + fibonacci<N-2>(); } template<> constexpr int fibonacci<1>() { return 1; } template<> constexpr int fibonacci<0>() { return 0; } // Conventional Approach int fibonacci(int N) { if ( N == 0 ) return

Recursive Fibonacci in Bash script

帅比萌擦擦* 提交于 2019-11-28 06:33:50
问题 This is my attempt: #!/bin/bash function fibonacci(){ first=$1 second=$2 if (( first <= second )) then return 1 else return $(fibonacci $((first-1)) ) + $(fibonacci $((second-2)) ) fi } echo $(fibonacci 2 0) I think i'm having trouble with the else statement. I get the error return: +: numeric argument required on line 14. The other problem that i'm having is that the script doesn't display any numbers even if i do echo $(fibonacci 0 2) . I thought it would display a 1 since i'm returning a 1

Fast Fibonacci recursion

蹲街弑〆低调 提交于 2019-11-28 04:34:01
I'm trying to recall an algorithm on Fibonacci recursion. The following: public int fibonacci(int n) { if(n == 0) return 0; else if(n == 1) return 1; else return fibonacci(n - 1) + fibonacci(n - 2); } is not what I'm looking for because it's greedy. This will grow exponentially (just look at Java recursive Fibonacci sequence - the bigger the initial argument the more useless calls will be made). There is probably something like a "cyclic argument shift", where calling previous Fibonacci value will retrieve value instead of calculating it again. maybe like this: int fib(int term, int val = 1,

Recursive Fibonacci

ぃ、小莉子 提交于 2019-11-28 03:56:55
I'm having a hard time understanding why #include <iostream> using namespace std; int fib(int x) { if (x == 1) { return 1; } else { return fib(x-1)+fib(x-2); } } int main() { cout << fib(5) << endl; } results in a segmentation fault. Once x gets down to 1 shouldn't it eventually return? When x==2 you call fib(1) and fib(0) : return fib(2-1)+fib(2-2); Consider what will happen when fib(0) is evaluated... The reason is because Fibonacci sequence starts with two known entities, 0 and 1. Your code only checks for one of them (being one). Change your code to int fib(int x) { if (x == 0) return 0;

Fibonacci numbers, with an one-liner in Python 3?

我只是一个虾纸丫 提交于 2019-11-28 03:48:38
I know there is nothing wrong with writing with proper function structure, but I would like to know how can I find nth fibonacci number with most Pythonic way with a one-line. I wrote that code, but It didn't seem to me best way: >>> fib=lambda n:reduce(lambda x,y:(x[0]+x[1],x[0]),[(1,1)]*(n-2))[0] >>> fib(8) 13 How could it be better and simplier? fib = lambda n:reduce(lambda x,n:[x[1],x[0]+x[1]], range(n),[0,1])[0] (this maintains a tuple mapped from [a,b] to [b,a+b], initialized to [0,1], iterated N times, then takes the first tuple element) >>> fib(1000)

Recursive Fib with Threads, Segmentation Fault?

不想你离开。 提交于 2019-11-28 02:12:19
Any ideas why it works fine for values like 0, 1, 2, 3, 4... and seg faults for values like >15? #include #include #include void *fib(void *fibToFind); main(){ pthread_t mainthread; long fibToFind = 15; long finalFib; pthread_create(&mainthread,NULL,fib,(void*) fibToFind); pthread_join(mainthread,(void*)&finalFib); printf("The number is: %d\n",finalFib); } void *fib(void *fibToFind){ long retval; long newFibToFind = ((long)fibToFind); long returnMinusOne; long returnMinustwo; pthread_t minusone; pthread_t minustwo; if(newFibToFind == 0 || newFibToFind == 1) return newFibToFind; else{ long

Can a Fibonacci function be written to execute in O(1) time?

一世执手 提交于 2019-11-27 21:55:13
So, we see a lot of fibonacci questions. I, personally, hate them. A lot. More than a lot. I thought it'd be neat if maybe we could make it impossible for anyone to ever use it as an interview question again. Let's see how close to O(1) we can get fibonacci. Here's my kick off, pretty much crib'd from Wikipedia, with of course plenty of headroom. Importantly, this solution will detonate for any particularly large fib, and it contains a relatively naive use of the power function, which places it at O(log(n)) at worst, if your libraries aren't good. I suspect we can get rid of the power function

Why is my recursive function so slow in R?

风流意气都作罢 提交于 2019-11-27 12:46:51
The following takes about 30 seconds to run whereas I would expect it to be nearly instant. Is there a problem with my code? x <- fibonacci(35); fibonacci <- function(seq) { if (seq == 1) return(1); if (seq == 2) return(2); return (fibonacci(seq - 1) + fibonacci(seq - 2)); } Patrick Burns gives an example in R Inferno of one way to do memoization in R with local() and <<- . In fact, it's a fibonacci: fibonacci <- local({ memo <- c(1, 1, rep(NA, 100)) f <- function(x) { if(x == 0) return(0) if(x < 0) return(NA) if(x > length(memo)) stop("’x’ too big for implementation") if(!is.na(memo[x]))

Generate a sequence of Fibonacci number in Scala [duplicate]

佐手、 提交于 2019-11-27 11:57:23
This question already has an answer here: What is the fastest way to write Fibonacci function in Scala? 8 answers def fibSeq(n: Int): List[Int] = { var ret = scala.collection.mutable.ListBuffer[Int](1, 2) while (ret(ret.length - 1) < n) { val temp = ret(ret.length - 1) + ret(ret.length - 2) if (temp >= n) { return ret.toList } ret += temp } ret.toList } So the above is my code to generate a Fibonacci sequence using Scala to a value n . I am wondering if there is a more elegant way to do this in Scala? There are many ways to define the Fibonacci sequence, but my favorite is this one: val fibs