Java: public static long fibby(long n){ if (n==0){ return 1; } return fibby(n/4)
ID: 3843389 • Letter: J
Question
Java:
public static long fibby(long n){
if (n==0){
return 1;
}
return fibby(n/4)+fibby(3*n/4);
public static void tablegen(int start,int end){
long answer = fibby(start);
System.out.println(start+" "+answer);
if(start !=end) {
tablegen(start+1,end);
}
For sparsetablegen(13, 20), expected:
13 15
15 18
16 21
20 24
got:
13 15
14 15
15 18
16 21
17 21
18 21
19 21
20 24
I need helping changing ONLY tablegen to display the results in "got: " and keep it as only (int start, int end) and can not using any for or while loops.
Explanation / Answer
This is the most easy possible way to solve your issue by changing the function signature
public static void tablegen(int start,int end, long prevAnswer){
long answer = fibby(start);
if (prevAnswer != answer)
System.out.println(start+" "+answer);
if(start !=end) {
tablegen(start+1,end, answer);
}
}
other way is:
public static void tablegen(int start,int end, long prevAnswer){
long answer = fibby(start);
if (prevAnswer != answer)
System.out.println(start+" "+answer);
while(start !=end) {
start++;
if (answer != fibby(start){
tablegen(start,end);
break;
}
}
}
I hope you like the solution. Let me know if you have some different need :) Shall help you for sure!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.