Sum of the Squares
Collapse Content
Show 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
Jacknerik
Dec 7, 4:44 PMDoes ^ 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.
Learneroo
Dec 8, 10:42 PM@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
Java Math library, which hasn't been covered yet. (You can see it over here: http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html )
Colm Forde
Nov 1, 7:13 PMI'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.
Bernard Mitchell
Jun 14, 6:02 PMsomeone explain why my code is wrong.
Learneroo
Jun 15, 4:29 PMHint: Use a variable to keep track of the total sum and return that at the end.
Bernard Mitchell
Jun 17, 4:49 PM@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?
Learneroo
Jun 18, 8:20 PM@Bernard, create a variable
sum
before the loop, and then add the square of each number tosum
. Then returnsum
after the loop.Bernard Mitchell
Jun 19, 9:36 AM@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++.
Learneroo
Jun 19, 10:25 AMYes, 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.
Bernard Mitchell
Jun 21, 2:24 PM@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
Bernard Mitchell
Jun 28, 3:39 PM@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
Learneroo
Jun 28, 10:45 PMsum
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.Bryan
Jan 8, 8:22 AMWhy does this result in "Execution timed out. Please fix your code to execute faster."?
Learneroo
Jan 8, 11:24 AMCheck 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?
Bryan
Jan 9, 3:17 AMThanks. I just re-read my code after sleeping and can't believe I even wrote that.
avisuisse
Sep 30, 10:31 PMsumsquare = function(a, b) {
base = a2
for (i in (a+1):b) {
base = base + i2
}
print(base)
}
omar
Oct 17, 7:14 AM//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;
}