fibonacci

Prolog; try to make fibonacci more effective?

烂漫一生 提交于 2019-12-04 02:41:51
This logic programming is really making a lap dance on my imperative programming skills. This is homework, so please just don't drop me the answer. This is what I have: fibo(N,1) :- N < 2, !. fibo(N,R) :- N1 is N-1, N2 is N-2, fibo(N1,R1), fibo(N2,R2), R is R1+R2. I'm suppose to make another function that looks like this; fib(N,Value,LastValue) . N is the n'th number, and value is the return value. I don't understand how I can rewrite this using accumulation. And since it counts backwards I don't see how it can "know" a last value before it calculates anything. :s Any input is appreciated. I

Calculating fibonacci

ぐ巨炮叔叔 提交于 2019-12-03 16:54:02
问题 I was sent this nice non-recursive function for computing a fibonacci sequence. So I coded up a bit of c# and was able to verify all numbers up to 1474 were correct. The problem comes in when attempting to calculate it for 1475 and above. My c# math skills just aren't up to the task of figuring out a different way. So, does someone have a better way of expressing this particular math function in c#? other than the traditional way of doing a recursive function? Incidentally, I started to use

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

谁说我不能喝 提交于 2019-12-03 16:00:24
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? In your code, num starts as the 0 th Fibonacci number, and num1 as the 1 st . So to find the n th , you have to iterate the step n times:

Calculating nth fibonacci number using the formulae in python

半腔热情 提交于 2019-12-03 15:57:19
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): if lfib(i) != efib(i): print i, "lfib:", lfib(i) print " efib:", efib(i) For n > 71 I see that the

Why does (int)55 == 54 in C++?

…衆ロ難τιáo~ 提交于 2019-12-03 15:11:58
问题 So I'm learning C++. I've got my "C++ Programming Language" and "Effective C++" out and I'm running through Project Euler. Problem 1...dunzo. Problem 2...not so much. I'm working in VS2008 on a Win32 Console App. Whats the Sum of all even terms of the Fibonacci Sequence under 4 million? It wasn't working so I cut down to a test case of 100... Here's what I wrote... // Problem2.cpp : Defines the entry point for the console application. // #include "stdafx.h" using namespace std; int _tmain(int

Help with LINQ Expression

偶尔善良 提交于 2019-12-03 13:51:13
问题 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 ? 回答1: 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(

Algorithm function for fibonacci series [closed]

半世苍凉 提交于 2019-12-03 09:51:39
问题 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. Closed 6 years ago . 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

fibonacci series - recursive summation

和自甴很熟 提交于 2019-12-03 08:41:00
Ok, I initially wrote a simple code to return the Fibonacci number from the series based on the user input.. n=5 will produce 3.. static int fibonacci(int n) { if (n == 1) return 0; else if (n == 2) return 1; else return (fibonacci(n - 1) + fibonacci(n - 2)); } I was thinking of modifying the code to return the sum of the series rather than just returning the value from the series and while trying to do the sum I accidentally added 1 to the return statement and to my surprise, it returned the sum correctly. The below code will return 7 for n=5. I am not sure if this is a right way to calculate

Why is a Fibonacci heap called a Fibonacci heap?

心已入冬 提交于 2019-12-03 07:04:23
问题 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? 回答1: The Fibonacci heap is made up of a collection of smaller heap-ordered trees of different "orders" that obey certain structural constraints. The Fibonacci

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

不打扰是莪最后的温柔 提交于 2019-12-03 06:57:48
问题 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