英文:
I believe there is a logical math problem with my code, but I cannot put my finger where is it and why
问题
这是您的代码,当我尝试测试输入为100时,结果为24501794,而不是25164150,以下是我的代码:
class DifferenceOfSquaresCalculator {
int computeSquareOfSumTo(int input) {
int sumOfNatural = 0;
int sumOfNaturalSquared = 0;
for (int i = 1; i < input; i++){
sumOfNatural += i;
}
sumOfNaturalSquared = (int) Math.pow(sumOfNatural, 2);
return sumOfNaturalSquared;
}
int computeSumOfSquaresTo(int input) {
int sumOfSquaredNaturals = 0;
for (int i = 1; i < input; i++){
i = (int) Math.pow(i, 2);
sumOfSquaredNaturals += i;
}
return sumOfSquaredNaturals;
}
int computeDifferenceOfSquares(int input) {
int difference = computeSquareOfSumTo(input) - computeSumOfSquaresTo(input);
return difference;
}
}
英文:
This my code and when i try to test the difference for input of 100 i get 24501794 instead of 25164150, here is my code:
class DifferenceOfSquaresCalculator {
int computeSquareOfSumTo(int input) {
int sumOfNatural = 0;
int sumOfNaturalSquared = 0;
for (int i = 1; i < input; i++){
sumOfNatural+=i;
}
sumOfNaturalSquared = (int) Math.pow(sumOfNatural,2);
return sumOfNaturalSquared;
}
int computeSumOfSquaresTo(int input) {
int sumOfSquaredNaturals = 0;
for (int i = 1; i < input; i++){
i = (int) Math.pow(i, 2);
sumOfSquaredNaturals+=i;
}
return sumOfSquaredNaturals;
}
int computeDifferenceOfSquares(int input) {
int difference = computeSquareOfSumTo(input) - computeSumOfSquaresTo(input);
return difference;
}
}
</details>
# 答案1
**得分**: 1
你的代码存在两个问题。
首先,你在计算序列时不包括最后一个数字:
```for (int i = 1; i < input; i++)```
应该改成
```for (int i = 1; i <= input; i++)```
其次,在你的 `computeSumOfSquaresTo` 中,你在循环内部改变了循环变量 `i`。可以尝试以下修改:
int computeSumOfSquaresTo(int input) {
int sumOfSquaredNaturals = 0;
for (int i = 1; i <= input; i++) {
sumOfSquaredNaturals += i * i;
}
return sumOfSquaredNaturals;
}
你还可以使用更简单的方法,使用 StreamAPI:
int computeSquareOfSum(int input) {
int sum = IntStream.rangeClosed(1, input).sum();
return sum * sum;
}
int computeSumOfSquares(int input) {
return IntStream.rangeClosed(1, input).map(i -> i*i).sum();
}
或者,你可以使用一个函数来实现相同的结果:
int computeDifferenceOfSquares(int input) {
int sum = IntStream.rangeClosed(1, input).sum();
int sumOfSquares = IntStream.rangeClosed(1, input).map(i -> i * i).sum();
return sum * sum - sumOfSquares;
}
<details>
<summary>英文:</summary>
You have two problems with this code.
At first, you count the series excluding the last number:
```for (int i = 1; i < input; i++)```
should be
```for (int i = 1; i <= input; i++)```
At second, in your `computeSumOfSquaresTo` you are changing the loop variable `i` inside the loop.
Try this instead:
int computeSumOfSquaresTo(int input) {
int sumOfSquaredNaturals = 0;
for (int i = 1; i <= input; i++) {
sumOfSquaredNaturals += i * i;
}
return sumOfSquaredNaturals;
}
You can also use a simpler approach using StreamAPI:
int computeSquareOfSum(int input) {
int sum = IntStream.rangeClosed(1, input).sum();
return sum * sum;
}
int computeSumOfSquares(int input) {
return IntStream.rangeClosed(1, input).map(i -> i*i).sum();
}
or, with same result, you may use just one function:
int computeDifferenceOfSquares(int input) {
int sum = IntStream.rangeClosed(1, input).sum();
int sumOfSquares = IntStream.rangeClosed(1, input).map(i -> i * i).sum();
return sum * sum - sumOfSquares;
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论