fibonacci

OpenMP benchmark: Am I doing it right?

Deadly 提交于 2019-12-14 03:15:46
问题 I made a program which calculates the fibonacci sequence. I executed it with different numbers of threads (eg. 1, 2, 10) but the execution time remained almost the same (about 0.500 seconds). I'm using CodeBlocks on Ubuntu and the GNU GCC compiler. In CodeBlocks I linked the library gomp and defined the flag -fopenmp for the compiler. #include <omp.h> #include <stdio.h> #include <stdlib.h> int main() { int i, n=1000, a[n]; omp_set_num_threads(4); for(i=0; i<n; i++) { a[i] = 1 + (rand() % ( 50

How to generate Fibonacci series in C

二次信任 提交于 2019-12-14 00:10:48
问题 I want to generate Fibonacci series in C. My code is giving a compilation error. Here is the code, Actually I am a beginner in programming. main() { int 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 { next = first + second; first = second; second = next; } printf("%d\n",next); } } 回答1: This works well. #include<stdio.h> int main() {

bottom up fibonacci in python using O(1) space

痞子三分冷 提交于 2019-12-13 16:17:36
问题 I want to write a bottom up fibonacci using O(1) space. My problem is python's recursion stack is limiting me from testing large numbers. Could someone provide an alternate or optimization to what I have? This is my code: def fib_in_place(n): def fibo(f2, f1, i): if i < 1: return f2 else: return fibo(f1, f2+f1, i -1) return fibo(0, 1, n) 回答1: You can memoize the Fibonacci function for efficiency, but if you require a recursive function, it's still going to take at least O(n): def mem_fib(n,

Fibonacci Series in C - The series of numbers up to a given number [closed]

浪子不回头ぞ 提交于 2019-12-13 11:27:14
问题 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 . Could anyone possibly provide feedback on my code given below? I have done the Fibonacci series multiple times in other languages, but for some odd reason, it won't print the correct series when I code it in C. I

Fibonacci direct calculation formula

假如想象 提交于 2019-12-13 11:25:16
问题 I have seen Fibonacci has direct formula with this (Phi^n)/√5 while I am getting results in same time but accurate result not approximate with something I managed to write: for r = 0 to 2 Sum [(n-r)!/((n-2r)!r!)] ( ! is the symbol for factorial ): def fr(n, p): # (n-r)!/((n-2r)!r!) r = int(n / p) n_f = 0 for j in range(1, r + 1): t_f = 1 r_f = factorial(j) i = (n - j) while i > (n - (2 * j)): t_f = t_f * i i = i - 1 n_f = n_f + t_f / r_f return n_f + 1 def factorial(n): if n == 0: return 1

How I do fibonaci sequence under 1000? [closed]

Deadly 提交于 2019-12-13 10:11:46
问题 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 7 years ago . #include <iostream> using namespace std; void main() { int i = 0; while (i < 1000) { int TEMP = i * 2; cout << i << endl; TEMP = i; i = i +1; // ??? } return; } I'm so confused?? :( 回答1: First you should check

Calculate the function of f(i) [closed]

感情迁移 提交于 2019-12-13 09:55:57
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago . When i is a positive integer, there is a function f(i) that satisfy the following. f(0) = 0, f(1) = 1 f(i) = f(i-1) + f(i-2) So, based on the above, I want to write a program to determine f(i). And write a program to determine f(1000). 回答1: The following Python 3.0 script will

Fibonacci sequence while loop

旧巷老猫 提交于 2019-12-13 08:47:02
问题 I have to write code that displays the Fibonacci sequence to the user desired number of terms and must also use a while loop. I'm not sure why this code isn't working. #include <stdio.h> #include <stdlib.h> int main (void) { int max; printf("Enter the max term of the Fibonacci Sequence:\n"); scanf("%i", &max); int a=0; int b=0; a=2; while(a<max) { if((a==0||a==1)) { printf("%i\n", &a); ++a; } else if(a>1) { a=(a-1)+(a-2); printf("%i\n", &a); ++a; } } return 0; } 回答1: You can try this.

nth term for Fibonacci series for enormous data input(Without Recursion or loop) [duplicate]

半城伤御伤魂 提交于 2019-12-13 08:08:12
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: nth fibonacci number in sublinear time I was creating a program which is related to the stair problem, i.e you have n stairs and the player can climb on the stairs using them one by one or skipping one ... Now to solve this problem I needed n th ( n +1)th term for the Fibonacci for n number of stairs, but the problem is my input range is 1 ≤ n ≤ 1000000. And for that much greater value of n if I use the loop

Determine Fibonacci Number from User Input using Recursion

一个人想着一个人 提交于 2019-12-13 07:08:08
问题 From my homework, I need to have the user enter a number in numeric form, and convert it to the simultaneous fibonacci number from the sequence, while using recursion. My question is how can I make the sequence through an array but not store it, so the array can be the size of the number the user entered... Here's some starting code I have: import java.util.Scanner; public class ReverseUserInput1 { //a recursive method to reverse the order of user input public static void main(String[] args)