Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Implementation of fibonacci in Java:
public class Fibonacci {
private int fibonacci(int n){
return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
}
private void test(){
System.out.println(fibonacci(4));
}
public static void main(String[] args) {
Fibonacci test = new Fibonacci();
test.test();
}
}
Using Oracle Java 8 on Ubuntu, it prints out 3 in Eclipse. But I was expecting 5:
fibonacci(4)
fibonacci(3) fibonacci(2)
fibonacci(2) fibonacci(1) fibonacci(1) fibonacci(0)
fibonacci(1) fibonacci(0) 1 1 1
1 1
So then what's wrong with my Java implementation?
Your fibonacci(int n) in Java will return 0 for n=0. You want to always return 1 for n<2. Replace:
return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
with
return n < 2 ? 1 : fibonacci(n - 1) + fibonacci(n - 2);
As it currently stands, the following is evaluated: (f is short for fibonacci)
f(4)
f(3)+f(2)
(f(1)+f(2))+(f(0)+f(1))
(1+(f(0)+f(1)))+(0+1)
(1+0+1)+(0+1)
2+1
3
The following should occur:
f(4)
f(3)+f(2)
(f(1)+f(2))+(f(0)+f(1))
(1+(f(1)+f(1)))+(1+1)
(1+1+1)+(1+1)
3+2
5
This has nothing to do with the language.
The first returns the sequence 0 1 1 2 3 5 8 13 ...
The second returns the sequence 1 1 2 3 5 8 13 ...
It's up to you to decide which sequence you want.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
Given base and n that are both 1 or more, compute recursively (no
loops) the value of base to the n power, so powerN(3, 2) is 9 (3
squared).
The answer is
public int powerN(int base, int n) {
if(n == 1)
return base;
return base * powerN(base, n - 1);
}
I am confused because when I look at this because is this not saying multiply the base against the returned number of powerN()?
powerN(3,3);
= 3
= 3*powerN(base, n-1) = 6
= 3*powerN(base, n-1) = 9
Which would multiply 9*6*3?
I do not see why we would have to multiply the base by the function?
Should the method not just return the answer as the base never changes and once n==1 the base case executes
Let's calculate powerN(3,3) as you did.
powerN(3,3) = 3 * powerN(3,2)
3 * powerN(3,2) = 3 * 3 * powerN(3,1)
3 * 3 * powerN(3,1) = 3 * 3 * 3 = 27
So it was just wrong the way you calculated.
Let's say we have powerN(2,3)
powerN(2,3):
call#1> return 2*powerN(2,3-1)
call#2> return 2*powerN(2,2-1)
call#3> (n==1)return 2*1
So,
call#3 returns 2 to call#2 [call#2 is now returning 2*2=4]
call#2 returns 4 to call#1 [call#1 is now returning 2*4=8]
That's it.
This question already has answers here:
Understanding how recursive functions work
(18 answers)
Closed 7 years ago.
I have the following program
public static int doSomething(int num){
if(Math.abs(num) > 9){
return (1 + doSomething( num / 10));
}
else{
return 1;
}
}
public static void main(String[] args){
System.out.println(doSomething(333));
}
This is how I understand it. If the number is 333.
333 / 10 gives me 33. Since 33 > 9 it runs the recursive loop again giving me 3.
After 3 it enters the else condition.
I don't understand why it prints 3 as answer.
I am new to java so still trying to understand the basics.
I don't believe the question is a duplicate. My question is much simpler being a beginner to java. Also the question I believe is using javascript not java.
Look, what will return doSomething() if Math.abs(num) < 9? 1? yes you are right. Ok lets start.
doSomething(333) => 1 + doSomething(33) // 33 not less than 9
=> 1 + (1 + doSomething(3)) // now 3 is less than 9
=> 1 + (1 + {1})
=> 3
The recursion is not on 1 + 33, but on 33 only, so it's:
doSomething(333)
= 1 + doSomething(33)
= 1 + 1 + doSomething(3)
= 1 + 1 + 1
= 3
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
What output is produced by the following program?
The answer says 7 but i have trouble working it out.
public class practice {
public static void main(String[] args){
int i = 5;
int b = g(i);
System.out.println(b+i);
}
public static int f(int i) {
int n = 0;
while (n * n <= i) {n++;}
return n-1;
}
public static int g(int a) {
int b = 0;
int j = f(a);
b = b + j;
return b;
}
}
I assume main is getting called. Here is a list of steps that happen
g gets called with 5 as its parameter.
then in function g, f gets called with g's parameter, which is 5
In function f n is set to zero, a while loop is called and every time n*n is less than or equal to its parameter, which is 5, n is incremented. below outlines the while loop.
0*0 is less than 5, increment n from 0 to 1 and continue.
1*1 is less than 5, increment n from 1 to 2 and continue.
2*2 is less than 5, increment n from 2 to 3 and continue.
3*3 is not less than 5, break out of the loop.
n-1, which is 3-1=2, gets returned back to where it was called, in variable j in function g.
b gets assigned to b+j which is 0+2.
b gets returned back to variable b in function main.
b+i, which is 5+2, which is 7, gets printed as the answer.
Your using a number (5) and basically checking what is the first whole number squared that equals more than than 5. Then you negate 1 from the answer and add 5.
So here is the maths:
1. Find the first whole number squared that is larger than 5
0*0 = 0 //+1
1*1 = 1 //+1
2*2 = 4 //+1
3*3 = 9 //3*3 is the first value over 5; loop breaks
2. Negate 1 from 3 (the first whole number when squared that is larger than
3 - 1 = 2; //using 3, negate 1 (also could have used n--)
3. Using the final value 2 add 5
2 + 5 = 7 //using 2 add 5
Answer is 7.
There is some really unnecessary stuff in this code, such as the two extra methods.
This question already has answers here:
Java recursive Fibonacci sequence
(37 answers)
Closed 8 years ago.
I am learning Java and I have this code from the internet and running it in Eclipse:
public class Fibonacci {
public static void main (String [] args) {
for (int counter = 0; counter <= 3; counter++){
System.out.printf("Fibonacci of %d is: %d\n", counter, fibonacci(counter));
}
public static long fibonacci(long number) {
if ((number == 0) || (number == 1))
return number;
else
return fibonacci(number - 1) + fibonacci(number - 2);
}
}
I've tried to understand it but cannot get it. So I run through the code and counter gets passed in through the fibonacci method. As counter starts at 0 and this is what gets passed first, then 1 and I understand the method passes back 0 and then 1.
When it reaches 2: it will return 2-1 + 2-2 = 2 and it does return this.
When it reaches 3: it will return 3-1 + 3-2 = 3 but it does not return 3 it returns 2.
Please can someone explain to me why as I cannot figure this out?
Thanks
First, I have to tell you that this recursive version has a dramatic exponential cost. Once you understand how it works, my advice for you would be to learn about tail recursivity, write a tail-recursive solution, an iterative solution, and compare them to your current method for high values of "number".
Then, your function basically uses the mathematical definition of the Fibonacci sequence :
f0 = 1, f1 = 1, fn = fn-1 + fn-2 for all n >= 2
For example if we call fibonacci(3), this will return fibonacci(2) + fibonacci(1). fibonacci(2) will be executed first and will return fibonacci(1) + fibonnacci(0). Then fibonacci(1) will return immediately 1 since it is a terminal case. It happens the same thing with fibonnacci(0), so now we have computed fibonnacci(2) = 1 + 0 = 1. Let's go back to fibonacci(3) which has been partially evaluated at this point : 1 + fibonnacci(1). We just have to compute fibonnacci(1) and we can finally return 1 + 1 = 2.
Even in this little example, you can see that we evaluated twice fibonacci(1), that is why this version is so slow, it computes many times the same values of the sequence, and it gets worth when "number" is high.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
1.7 (Approximate p) p can be computed using the following formula:
Write a program that displays the result of 4 * (1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11)
and 4 * (1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13)
Use 1.0 instead of 1 in your 3 5 7 9 11 13
program.
My Code:
/**
* This program will print two different results in
* trying to reach a approximation of pi.
*
* Author: X Date: 11/11/2013
*/
public class one_seven
{
public static void main(String[] args)
{
System.out.print("This will print 1st approximation: ");
System.out.println(4*(1.0-(1.0/3)+(1.0/5)-(1.0/7)-(1.0/11)));
System.out.print("This will print 2nd approximation: ");
System.out.println(4*(1.0-(1.0/3)+(1.0/5)-(1.0/7)-(1.0/11)+(1.0/13)));
}
}
My output for the code is:
This will print 1st approximation: 2.531601731601732
This will print 2nd approximation: 2.83929403929404
You're missing the 1.0/9 term.
System.out.print("This will print 1st approximation: ");
System.out.println(4*(1.0-(1.0/3)+(1.0/5)-(1.0/7)+(1.0/9)-(1.0/11)));
System.out.print("This will print 2nd approximation: ");
System.out.println(4*(1.0-(1.0/3)+(1.0/5)-(1.0/7)+(1.0/9)-(1.0/11)+(1.0/13)));
In both approximations you left out + (1.0/9.0)