Not Getting Expected Output. What is wrong with my logic/syntax? Thanks in advance:)
I'm supposed to create a method to find the nth harmonic number. Say the use asks for the third harmonic number, I want it to return the sum of 1, 1/2, and 1/3. I am required to solve this recursively and I have difficulty thinking that way. I am nonplussed as to why this code does not work. The System.out.prints I do indicate that n is decrementing as intended but the sum of harmonic numbers is not accumulating. I've included the mini test program so if you want to run this you can go ahead. Thanks! This is my first post here btw.
Code Java:
public class HarmonicTest {
public static void main(String[] args)
{
System.out.println(Harmonic.getHarmonic(3)); //getHarmonic(3));
}
}
import java.util.*;
public class Harmonic
{
public static double getHarmonic(int n)
{
if(n>0)
{
return(getHarm(n, 0));
}
else
throw new InputMismatchException("The input for n must be greater than 0.");
}
public static double getHarm(int n, double harmonic)
{
harmonic = harmonic + (1/n);
System.out.println("Int is: " + n);
System.out.println("Harmonic is: " + harmonic);
if(n == 1)
{
return harmonic;
}
else
return(getHarm((n-1), harmonic));
}
}
Re: Not Getting Expected Output. What is wrong with my logic/syntax? Thanks in advanc
Whenever I hear about people new to programming doing mathematics that involve division, I always look for the same thing, and lo and behold I see it again.
There is this little thing in programming that is called: Integer Division (division : Java Glossary)
Integer Division is when you divide one integer from another integer. Everything you have ever learned in math will tell you that 1/2 = 0.5, but that is not the case in programming. In programming, 1/2 = 0. But, "Why?" you ask. The reason is that when you do any math with all integers, your result is an integer. So it will perform the 1 divided by 2, but it will throw away all of the decimals. So, in programming, 10/3 = 3, while in real math 10/3 = 3.33 (and a bunch more 3s). How can we solve this problem? Well if even ONE of the numbers in our math is a double instead of an int, all of our problems are solved. So, 1.0/2 = 0.5 since 1.0 is a double and not an int. Here are a few examples:
1/2 = 0
1.0/2 = 0.5
1/2.0 = 0.5
1.0/2.0 = 0.5
5/4 = 1
5.0/4 = 1.25
5/4.0 = 1.25
Now, why does this help you? Because of this line:
Code java:
harmonic = harmonic + (1/n);
Notice the integer division: 1/n
To solve this, you just need the 1 to be a double instead of an int, so this will solve it:
Code java:
harmonic = harmonic + (1.0/n);
Tell me if all of that makes sense.
Re: Not Getting Expected Output. What is wrong with my logic/syntax? Thanks in advanc
Also, a tidbit about recursion.
You can actually make your code much more appropriate for recursion. Here is a step-by-step of what I would do:
Usually when doing recursion, the first thing you want to do is check the base-case.
Code java:
public static double getHarm(int n)
{
if(n==1)
return 1;
}
Next we determine what we will be doing. Since we are adding, we can add the result of our recursion to our current value. We also want to remember to alter the parameters in some way when we call the recursive method.
Code java:
public static double getHarm(int n)
{
if(n==1)
return 1;
return getHarm(n-1) + (1.0/n);
}
And that is actually all it takes. The purpose of recursion is to create a simple algorithm (that is near impossible to understand just by looking at it). When you do recursion, you are sacrificing some efficiency, but sometimes it is the best way of doing something.
Re: Not Getting Expected Output. What is wrong with my logic/syntax? Thanks in advanc
This is great! I know exactly what you are talking about. I learned that back in my first comp sci class and of course if you don't use it, you lose it. I'll remember it forever now because it's been an issue. That's how these things work. :) Thanks a lot!