Ian Lundberg
Ian Lundberg

Reputation: 1883

add the sum of all numbers from a given number to a given number?

I created a method that will add the sum of all numbers between and including two given numbers, but for some reason it doesn't work.

public static int sumOfAll(int one, int two)
{
    int bigNumber;
    int smallNumber;
    if(one < two){
        bigNumber = two;
        smallNumber = one;
    }
    else{
        bigNumber = one;
        smallNumber = two;
    }

    return ((smallNumber + bigNumber) / 2) * (bigNumber - smallNumber + 1);
}

That is the method and this is how I declared the method.

System.out.println(MathOp.sumOfAll(100, 1));

For some reason the result that is printed is 5000 when it should be 5050. The algorithm I used is right I tested it on wolframalpha. I don't know why it isn't working here and any help would be awesome!

Upvotes: 1

Views: 245

Answers (5)

Chicodelarose
Chicodelarose

Reputation: 882

First of all, you don't need to add more lines of code, change the returning type of your method or change your algorithm.

Background

When you are dividing numbers, in some cases, the result is integer. For example 2 / 1 = 2 or 33 / 11 = 3. In other cases the result is not an integer like 3 / 2 = 1.5 or 10 / 9 = 1.11...

In JAVA, if you try to make arithmetic operations with numbers that are ALL integers, the result will yield and integer value. For example 2 + 3 / 5 = 2 + 0.6. But since 3 and 5 are both integers, JAVA automatically truncates the decimal part of the result. At the end it is 2 + 0 = 2.

How this problem is solved?

Since your method calculates an "inclusive summation of numbers from a to b", it is known that the result will yield an integer value. But the return statement contain arithmetic operations that could yield double values. In that case you force the result to take double values for the moment by adding a ".0" to the end of the numbers there, in this case 2 and 1:

return ((smallNumber + bigNumber) / 2) * (bigNumber - smallNumber + 1);

And that become:

return ((smallNumber + bigNumber) / 2.0) * (bigNumber - smallNumber + 1.0);

Since the the method must return and integer, value the returned value must be cast back to integer:

return (int) (((smallNumber + bigNumber) / 2.0) * (bigNumber - smallNumber + 1.0));

Result

public static int sumOfAll(int one, int two){
        int bigNumber;
        int smallNumber;
        if(one < two){
            bigNumber = two;
            smallNumber = one;
        }
        else{
            bigNumber = one;
            smallNumber = two;
        }

        return (int) (((smallNumber + bigNumber) / 2.0) * (bigNumber - smallNumber + 1.0));
    }

Some Inputs and Outputs
Input: sumOfAll(100,1) --------------------- Output: 5050
Input: sumOfAll(25,5) ----------------------- Output: 315
Input: sumOfAll(1000,100) ----------------- Output: 495550
Input: sumOfAll(80,3) ----------------------- Output: 3237
Input: sumOfAll(7795,7) -------------------- Output: 30384889

Upvotes: 0

Khalid Farhan
Khalid Farhan

Reputation: 465

You can try by changing int to double

public static double sumOfAll(int one, int two)
    {
        double bigNumber;
        double smallNumber;
        if (one < two)
        {
            bigNumber = two;
            smallNumber = one;
        }
        else
        {
            bigNumber = one;
            smallNumber = two;
        }

        return ((smallNumber + bigNumber) / 2) * (bigNumber - smallNumber + 1);
    }

Upvotes: 0

Hanky Panky
Hanky Panky

Reputation: 46900

public static int sumOfAll(int one, int two)
{
    int bigNumber=Math.max(one,two);
    int smallNumber=Math.min(one,two);
    return ((bigNumber+1-smallNumber)*(bigNumber+smallNumber))/2;
}

Upvotes: 3

magodiez
magodiez

Reputation: 741

Why don't you use the summtion formula?

http://en.wikipedia.org/wiki/Summation

http://upload.wikimedia.org/math/a/7/4/a74f603449dc34d308b50bbb6acaba1a.png

In for this just change your return code to

return (bigNumber*(bigNumber+1)-smallNumber*(smallNumber-1))/2;

Upvotes: 1

sanbhat
sanbhat

Reputation: 17622

can you try

return (int)(((double)(smallNumber + bigNumber) / 2) * (bigNumber - smallNumber + 1));

reason being

as @devnull pointed out, when you do ((smallNumber + bigNumber) / 2), the decimal part gets chucked off since they get typecast to int. So an explicit cast to (double) is required before doing multiplication

Upvotes: 0

Related Questions