Skip to content

Instantly share code, notes, and snippets.

@chinmaythosar
Created May 24, 2020 13:22
Show Gist options
  • Save chinmaythosar/9c2a08b025ecf17848ea16efe95074f3 to your computer and use it in GitHub Desktop.
Save chinmaythosar/9c2a08b025ecf17848ea16efe95074f3 to your computer and use it in GitHub Desktop.
//Calculating Fibonacci without using any loops in O(n)
//No goal in particular just asked for a task.
#include<stdio.h>
int *arr;
int fib(int n);
int main(void) {
puts("Hello World!");
int n=15;
arr=malloc(sizeof(int)*2);
arr[0]=1;
arr[1]=1;
arr[0]=
printf("%d",fib(n-2));
free(arr);
return 0;
}
int fib(int n){
if(n==0)
return arr[1];
else
{
int temp = arr[1]+arr[0];
arr[0]=arr[1];
arr[1]=temp;
return fib(n-1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment