7
\$\begingroup\$

Your method should accept a real number as a parameter representing a limit, and should add and print terms of the sequence until the sum of terms meets or exceeds that limit. For example, if your method is passed 2.0, print terms until the sum of those terms is at >= 2.0. You should round your answer to 3 digits past the decimal point.

The following is the output from the call sequenceSum(2.0);
1 + 1/2 + 1/3 + 1/4 = 2.083

If your method is passed a value less than 1.0, no output should be produced. You must match the output format shown exactly; note the spaces and pluses separating neighboring terms.

I'm wondering if there's anything I can do for the edge case of n = 1? It kinda just seems like it's hanging out at the end, and it looks a bit strange to me. Also, looking at the types for each value, I'm wondering if I need to think more about the data types I chose to use, in terms of should I use a double for this, or an int?.

I'd use methods to break out the functionality of my code, but the prompt only allows me to submit one method for this type of problem. Any feedback is appreciated!

public static void sequenceSum(double n){
    if(n > 1.0){
        int denominator = 1;
        double accumulator = 0;
        System.out.print("1");
        while(accumulator < n) {
            accumulator += ( 1.0 / denominator);
            if(denominator > 1.0){
                System.out.print(" + " + 1 + "/" + denominator);
            }
        denominator++;        
        }
    System.out.printf(" = %1.3f", accumulator);
    } else if(n == 1.0){
        System.out.print("1 = 1.000");
    }
}
\$\endgroup\$
  • 1
    \$\begingroup\$ It's offtopic, but it is related to the mathematics behind the series - see here. The growth rate is so slow (logarithmic) that for a relatively small n, the program will virtually run forever. \$\endgroup\$ – Alexei Dec 13 '15 at 21:12
  • 2
    \$\begingroup\$ @Alexei and you might meet some funny approximation issues (i.e. if you start summing the smallest terms, you will get a better result than if you start with the "big" terms) because of how the floats are stored! \$\endgroup\$ – oliverpool Dec 13 '15 at 21:31
7
\$\begingroup\$

I'm wondering if there's anything I can do for the edge case of n = 1?

Yes, I think you are considering it to much (see below: you can simply remove some branching)

Also, looking at the types for each value, I'm wondering if I need to think more about the data types I chose to use, in terms of should I use a double for this, or an int?.

I think your current choice is fine: int for the denominator and double for the accumulator.


Following your program (my comments adress the previous block of code):

public static void sequenceSum(double n){
    if(n > 1.0){

I think you can simply remove this first condition (already well managed in the while loop)

        int denominator = 1;
        double accumulator = 0;
        System.out.print("1");
        while(accumulator < n) {
            accumulator += ( 1.0 / denominator);
            if(denominator > 1.0){

Your denominator is an int: you should compare it with 1 (and not 1.0)

                System.out.print(" + " + 1 + "/" + denominator);

You should integrate the 1 into the string: " + 1/".

            }
        denominator++;        
        }
    System.out.printf(" = %1.3f", accumulator);
    } else if(n == 1.0){

This branch can simply be replaced with a condition at the very beginning to check if n < 1.0 and immediately return if it is the case.

        System.out.print("1 = 1.000");
    }
}

Miscellaneous

Instead of printing each time, you could append to a StringBuffer

You have good variable names, except for n. Maybe sumLimit is more appropriate? (or accumulatorLimit).

Simplifying > 1 conditions

You could start your denominator at 2 and your accumulator at 1.0, to get rid of the if condition inside your while loop (if n is 1.0, then the while loop will simply not be executed at all).

\$\endgroup\$
  • \$\begingroup\$ I was able to eliminate the edge case by taking your suggestions to integrate the 1 into the string: " + 1/". But I'm not able to successfully eliminate the if condition inside the while loop. Maybe it's because of the starting positions I chose? At any rate, I'm keeping the 1.0 / denominator for now, because it's what allows the program to successfully sum the fractions (unless it's better for me to explicitly cast a double. But now that the case of n = 1 is solved, what about if n < 1? I can't produce output then, but currently it will, unless I put another if statement in. \$\endgroup\$ – cody.codes Dec 13 '15 at 21:19
  • 1
    \$\begingroup\$ But I'm not able to successfully eliminate the if condition inside the while loop. what happends when you simply remove this condition (with the new "starting" values that I proposed)? For the case n < 1.0, I think you will need an extra condition at the very beginning. \$\endgroup\$ – oliverpool Dec 13 '15 at 21:22
  • 1
    \$\begingroup\$ And doing something like if(n < 1.0){return} at the very beginning is easier to understand than a if(n > 1.0]{...}else if(n == 1.0){...}(and it's a direct implementation of the question "If your method is passed a value less than 1.0, no output should be produced. ") \$\endgroup\$ – oliverpool Dec 13 '15 at 21:27
  • 1
    \$\begingroup\$ Actually a StringBuffer is sufficient (here is an example) \$\endgroup\$ – oliverpool Dec 14 '15 at 8:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.