在 C 中求 10,000,000 以内的平方
3 个连续数字 11、12 和 13 的平方和为 434(即 121 + 144 + 169 = 434)。数字 434 从两种方式读取都是相同的,称为回文。我需要找出小于 10^7
的数字之和,该数字可以表示为连续平方和并得到一个结果 回文。如果在 2 个不同的序列中,一个数字重复,则求和 他们两次。也就是说,如果 11 出现在 2 个连续的数字序列中,则将其相加两次。
我需要根据上面的场景写一个程序。
我的理解是我们必须找到 10,000,000 以内的平方,然后是所有数字。我应该如何用 C 语言编写程序来执行此操作?
The sum of squares of the 3 consecutive numbers 11, 12 and 13 is 434 (that is 121 + 144 + 169 = 434). The number 434 reads the same from both ways and is called a palindrome. I need to find out the sum of the numbers less than 10^7
that can be expressed as the sum of consecutive squares and results in a
palindrome. If in 2 different sequences, a number repeats, then sum
them twice. That is if 11 occurs in 2 consecutive number sequences, sum it twice.
I need to write a program based on the above scenario.
What I understood is we have to find squares up to 10,000,000 and then all the numbers. How should I approach writing a program to do this in C?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能需要一个 for 循环来递增变量?使用此变量,您可以生成 3 个连续的数字。然后将平方数相加。如果它高于您的最大数字,您将停止循环。如果在下面,你检查它是否是回文?
You probably need a for loop which increments a variable? Using this variable you can generate 3 consecutive numbers.. then sum up the squared numbers.. if it's above your max number you stop the loop. if it's below you check whether it's a palindrom?
使用暴力方式就是一种可能的方式。
从
1 到 10^7 - 2
迭代变量i
,以便获取变量的前三个值(包括 i)的平方和,并确定其是否回文与否。IE。当
i=5
时,在for
循环中你需要确定 i^2 + (i+1)^2 + (i+2)^2 是否是回文。
我不确定,但你宁愿使用
long long
因为你需要计算平方。Using brute force way is one such possible way.
Iterate a variable
i
from1 to 10^7 - 2
so that you are going to take sum of squares of first three value of variable (including i) and find whether its palindrome or not.ie. when
i=5
, in afor
loopyou need to find whether i^2 + (i+1)^2 + (i+2)^2 is palindrome or not.
I am not sure but you rather take
long long
as you need to calculate squares.