Skip to content

Fibon code #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions N-th_Fibonacci/Fibonacci.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@


import java.util.Scanner;

public class Fibonacci
{
double f=0;
double mem[]=new double[1001];
public static void main(String args[])
{
Scanner scanner=new Scanner(System.in);
int n =scanner.nextInt();
System.out.println(n+"-th Fibonacci number is: "+new Fibonacci().fibo2(n));
scanner.close();
}
public double fibo2(int n)
{
for(int k=1;k<=n;k++)
{
if(k<=2)
f=1;
else
f=mem[k-1]+mem[k-2];
mem[k]=f;
}
return mem[n];

}
}













/*
* public double fibo(int n)
{
if(n<=2)
f=1;
else
f=fibo(n-1)+fibo(n-2);
return f;

}

public double fibo1(int n)
{
if(mem[n]!=0)
f=mem[n];
else if(n<=2)
f=1;
else
f=fibo1(n-1)+fibo1(n-2);
mem[n]=f;
return f;

}
*/
Comment on lines +30 to +66
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/*
* public double fibo(int n)
{
if(n<=2)
f=1;
else
f=fibo(n-1)+fibo(n-2);
return f;
}
public double fibo1(int n)
{
if(mem[n]!=0)
f=mem[n];
else if(n<=2)
f=1;
else
f=fibo1(n-1)+fibo1(n-2);
mem[n]=f;
return f;
}
*/

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could remove the commented-out sections to make your code clean :)

Copy link
Collaborator

@Mr-Skully Mr-Skully Oct 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly in the other branch too..

Copy link
Collaborator

@Mr-Skully Mr-Skully Oct 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway, I've added the Hacktoberfest-accepted labels to your PRs, so the Hacktoberfest website count them as valid PRs.
Also, since fibonacci numbers are whole numbers, do you need to use the double datatype?

Copy link
Author

@Priyasumesh123 Priyasumesh123 Oct 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is taken as double to have larger range than int.We can use long as well.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, long would be the more appropriate choice.

2 changes: 2 additions & 0 deletions N-th_Fibonacci/Input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
20
20-th Fibonacci number is: 6765.0
7 changes: 7 additions & 0 deletions N-th_Fibonacci/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Readme

***n-th Fibonacci using memoization***

Finding the n-th fibonacci number using normal recursive algorithm has a time complexity of O(2^n).
Here we use a Dynamic Programming approach called Memoization. The answers to subproblems are stored in an array and thereby reducing the number of computations drastically. This Algorithm runs in the order of O(n).