Sum of the Squares


Collapse Content

What is the sum of the squares of all the numbers from a to b (inclusive)?

Challenge

Add up the squares of all the numbers from a to b and return the sum.

Please sign in or sign up to submit answers.

Alternatively, you can try out Learneroo before signing up.

Comments

  • Does ^ not mean "to the power of" in java? I kept trying i2 but that didn't work, so I did i*i and that did.

  • @jacknerik, ^ does not do "power" in Java. You can see the Java's main built-in math operations here: http://www.learneroo.com/modules/11/nodes/103. Your way was good, though there is also the

    cont...
  • I've entered this code
    for (int i = a; i <= b; i ++)
    {
    return (i * i);
    }

    when I run the program it tells me I am missing a return statement.

  • for(int m=a; m<b; m=m+1){
            if(m>a&&m<b){
                return m*m+b*b+a*a
    

    someone explain why my code is wrong.

  • Hint: Use a variable to keep track of the total sum and return that at the end.

  • @Learneroo I tried creating an int that represents the total. For some reason my int that represents the numbers in the range only picks up the first number in the set. Can you explain what I'm doing wrong?

  • @Bernard, create a variable sum before the loop, and then add the square of each number to sum. Then return sum after the loop.

  • @learneroo when you say create a variable. Do you mean create an int variable? And if so I'm given only the first and last number how can I express the numbers in the range in an int? I was thinking a++.

  • Yes, create an int variable. Then loop through the numbers from a to b. If you need more help see this code and modify it to add the squares instead of the numbers. See also Math with loops for more practice.

  • @leaneroo when i plug in the provided code given in the above comment i get an error. I'm gonna work on it still just wanted to let you know. Thanks

  • @learneroo , Finally got it but I don't quite understand still. In my equation does sum remain zero even as the loop goes on. I recall in past nodes that sum changes as the loop goes on. I may be wrong though. Thanks

  • sum is declared outside the loop and set to 0, the loop modifies its value, so after the loop it has the value of whatever it was at the end of the loop. See also scope.

  • Why does this result in "Execution timed out. Please fix your code to execute faster."?

    static int doStuff(int a, int b){
        int sum = 0;
        for (int i=a;a<=b;i++) {
            int aPow = a*a;
            int bPow = b*b;
            sum += (aPow*bPow);
        }
        return sum;
    }
    
  • Check if the loop will ever terminate. Will there be cases where a will never be less than b, so that the loop goes on forever?

  • Thanks. I just re-read my code after sleeping and can't believe I even wrote that.

  • sumsquare = function(a, b) {
    base = a2
    for (i in (a+1):b) {
    base = base + i2
    }
    print(base)
    }

  • //by dart

    main() {
    var sum = SumoftheSquares(1, 3);
    print(sum);
    }

    SumoftheSquares(a, b) {
    int thesum = 0;
    for (int i = a; i <= b; i++) {
    thesum += i * i;
    }
    return thesum;
    }

All Node Comments
Contact Us
Sign in or email us at [email protected]