I was writing a Java code for Fibonacci numbers using recursion in java.
There might be n number of ways to achieve this.
I was used the below way, where there can be n number of arguments provided to a java method.
To brief about Fibonacci series
0, 1, 1, 2, 3, 5, 8, ...
First and next value in series is 0 & 1, first value in the sequence is 0, second is 1, third value is (first + second value in the series), goes on...
F0 = 0 & F1= 1
Below is the java method which will be recursively called.
private static long calculateFibo(long start, long next, long numberofiter, int... i)
while calling this method it is not necessary that the last parameter in the method signature should be provided.
Below is how it is called, there is no fourth parameter passed.
long result = calculateFibo(start,next,numberofiter);
Also note in the implementation of the method,
the int... i is validated, this is a integer array variable. where the first in the the array is the count used to validate the iteration.
This validation is done here
int leng = i.length;
if(leng > 0 && leng == 1)
{
cnt = i[0]; //This i[0] has the first value of the string.
//When iterating recursively we are passing this value refer the next code snippet
cnt++;
}
Calling the "calculateFibo" method recursive passing the default parameter. Below is the code snippet.
calculateFibo(start, next, numberofiter,cnt);
cnt is the last parameter passed.
As here i need int variable passed is as int ... i. This can be Object also.
Code:
--------------------------------------------------------------------------------------------------------
public class FiboStart {
public static void main(String args[])
{
long start =0;
long next =1;
//Just the number of iteration.
long numberofiter = 37;
long result = calculateFibo(start,next,numberofiter);
System.out.println(numberofiter + " Fibonacci is "+ result);
}
private static long calculateFibo(long start, long next, long numberofiter, int... i) {
int leng = i.length;
int cnt = 0;
if(leng > 0 && leng == 1)
{
cnt = i[0];
cnt++;
}
if( cnt < numberofiter)
{
System.out.println("start := "+start+ " , next "+next +" === cnt "+cnt);
next = start+next;
start =next - start;
return calculateFibo(start, next, numberofiter,cnt);
}
return start;
}
}
Output in this case will be
37 Fibonacci is 24157817
=======================================================================
Below is the code where one can pass more than one int value when calling the method.
Highlighted code is changed for understanding.
Code:
----------------------------------------------------------------------------------------------------------
public class FiboStart {
public static void main(String args[])
{
long start =0;
long next =1;
long numberofiter = 10;
long result = calculateFibo(start,next,numberofiter);
System.out.println(numberofiter + " Fibonacci is "+ result);
}
private static long calculateFibo(long start, long next, long numberofiter, int... i) {
int leng = i.length;
int cnt = 0;
int cnt1 = 0;
if(leng > 0 && leng == 2)
{
cnt = i[0];
cnt1 = i[1];
System.out.println("first default param "+i[0]+" AND "+"second default param "+i[1]);
cnt1 = cnt1+10;
cnt++;
}
if( cnt < numberofiter)
{
//System.out.println("start := "+start+ " , next "+next +" === cnt "+cnt);
next = start+next;
start =next - start;
return calculateFibo(start, next, numberofiter,cnt,cnt1);
}
return start;
}
}
Output:
first default param 0 AND second default param 0
first default param 1 AND second default param 10
first default param 2 AND second default param 20
first default param 3 AND second default param 30
first default param 4 AND second default param 40
first default param 5 AND second default param 50
first default param 6 AND second default param 60
first default param 7 AND second default param 70
first default param 8 AND second default param 80
first default param 9 AND second default param 90
10 Fibonacci is 55
There might be n number of ways to achieve this.
I was used the below way, where there can be n number of arguments provided to a java method.
To brief about Fibonacci series
0, 1, 1, 2, 3, 5, 8, ...
First and next value in series is 0 & 1, first value in the sequence is 0, second is 1, third value is (first + second value in the series), goes on...
F0 = 0 & F1= 1
Below is the java method which will be recursively called.
private static long calculateFibo(long start, long next, long numberofiter, int... i)
while calling this method it is not necessary that the last parameter in the method signature should be provided.
Below is how it is called, there is no fourth parameter passed.
long result = calculateFibo(start,next,numberofiter);
Also note in the implementation of the method,
the int... i is validated, this is a integer array variable. where the first in the the array is the count used to validate the iteration.
This validation is done here
int leng = i.length;
if(leng > 0 && leng == 1)
{
cnt = i[0]; //This i[0] has the first value of the string.
//When iterating recursively we are passing this value refer the next code snippet
cnt++;
}
Calling the "calculateFibo" method recursive passing the default parameter. Below is the code snippet.
calculateFibo(start, next, numberofiter,cnt);
cnt is the last parameter passed.
As here i need int variable passed is as int ... i. This can be Object also.
Code:
--------------------------------------------------------------------------------------------------------
public class FiboStart {
public static void main(String args[])
{
long start =0;
long next =1;
//Just the number of iteration.
long numberofiter = 37;
long result = calculateFibo(start,next,numberofiter);
System.out.println(numberofiter + " Fibonacci is "+ result);
}
private static long calculateFibo(long start, long next, long numberofiter, int... i) {
int leng = i.length;
int cnt = 0;
if(leng > 0 && leng == 1)
{
cnt = i[0];
cnt++;
}
if( cnt < numberofiter)
{
System.out.println("start := "+start+ " , next "+next +" === cnt "+cnt);
next = start+next;
start =next - start;
return calculateFibo(start, next, numberofiter,cnt);
}
return start;
}
}
Output in this case will be
37 Fibonacci is 24157817
=======================================================================
Below is the code where one can pass more than one int value when calling the method.
Highlighted code is changed for understanding.
Code:
----------------------------------------------------------------------------------------------------------
public class FiboStart {
public static void main(String args[])
{
long start =0;
long next =1;
long numberofiter = 10;
long result = calculateFibo(start,next,numberofiter);
System.out.println(numberofiter + " Fibonacci is "+ result);
}
private static long calculateFibo(long start, long next, long numberofiter, int... i) {
int leng = i.length;
int cnt = 0;
int cnt1 = 0;
if(leng > 0 && leng == 2)
{
cnt = i[0];
cnt1 = i[1];
System.out.println("first default param "+i[0]+" AND "+"second default param "+i[1]);
cnt1 = cnt1+10;
cnt++;
}
if( cnt < numberofiter)
{
//System.out.println("start := "+start+ " , next "+next +" === cnt "+cnt);
next = start+next;
start =next - start;
return calculateFibo(start, next, numberofiter,cnt,cnt1);
}
return start;
}
}
Output:
first default param 0 AND second default param 0
first default param 1 AND second default param 10
first default param 2 AND second default param 20
first default param 3 AND second default param 30
first default param 4 AND second default param 40
first default param 5 AND second default param 50
first default param 6 AND second default param 60
first default param 7 AND second default param 70
first default param 8 AND second default param 80
first default param 9 AND second default param 90
10 Fibonacci is 55
No comments:
Post a Comment