C 中的完美数
我需要编写一个 C 程序来查找完美数..
main()
{
int n=1000,sum = 0;
for(int num = 1; num <= n; num++)
{
sum = 0;
for(int i = 1; i < num; i++)
{
if(!(num%i))
{
sum+=i;
}
}
if(sum == num)
printf("\n%d",num);
}
}
if(!(num%i))
- 这是我不明白的 d 行。
如果还有其他简单的方法请推荐我
I need to write a C program to find the Perfect Number..
main()
{
int n=1000,sum = 0;
for(int num = 1; num <= n; num++)
{
sum = 0;
for(int i = 1; i < num; i++)
{
if(!(num%i))
{
sum+=i;
}
}
if(sum == num)
printf("\n%d",num);
}
}
if(!(num%i))
- This is d line I do not understand.
If there is any other simple method do please suggest me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
if(!(num%i))
只是表示if( (num%i) == 0 )
if(!(num%i))
simply meansif( (num%i) == 0 )
如果您正在寻找更有效的方法来查找完美数字,您可能需要阅读有关完美数字的维基百科页面。在其中您会发现没有已知的奇完美数(并且使用您的方法您将找不到任何奇完美数)并且所有偶完美数的形式为:
2^(p - 1)*(2 ^p - 1)
其中2^p - 1
是素数,因此p
是素数。因此,如果您想找到偶数完美数,请检查所有素数p
的2^p - 1
素数,如果是的话2^(p - 1)* (2^p - 1)
非常完美。如果您只想使用简单的循环找到一些小的完美数字,您可以通过注意如果
i
整除num
,那么num 也整除,从而使您的方法更加高效/我
。也就是说,您只需循环直到num
的平方根,并将i
和num / i
对添加到sum< /代码>。请注意,如果
num
是平方,则num
的平方根只需添加一次。请注意,如果您以这种方式计算
sum
,则完美数的值将为2 * num
,而不是num
。If you are looking for a more efficient way to find perfect numbers, you might want to read the Wikipedia page on perfect numbers. In it you will find that there are no known odd perfect numbers (and using your method you are not going to find any) and that all even perfect numbers are of the form:
2^(p - 1)*(2^p - 1)
where2^p - 1
is prime and thereforep
is a prime. Thus if you want to find even perfect numbers check the primality of2^p - 1
for all primesp
, if so2^(p - 1)*(2^p - 1)
is perfect.If you just want to find a few small perfect numbers using a simple loop you can make your approach more efficient by noting that if
i
dividesnum
, so doesnum / i
. That is, you only have to loop up until the square root ofnum
and add pairsi
andnum / i
tosum
. Note that ifnum
is square, the square root ofnum
has to be added only once.Note that if you calculate
sum
in this way, it's value will be2 * num
for perfect numbers, notnum
.num % i
表示“num modulo i”;它返回数字除法的提醒(因此,是0
和i-1
之间的数字)。在 C 中,0 为假,所有其他数字为真,因此
!(num % i)
测试“num modulo i”是否为零,或者用简单的数学语言来说,num 是否可以被整除我。num % i
means "num modulo i"; it returns the reminder of the division of the numbers (hence, a number between0
andi-1
).In C, 0 is false and all other numbers are true, so
!(num % i)
tests if "num modulo i" is zero, or in plain math-speak, if num is evenly divisible by i.以非常简单的方式,
if(!(num%i))
代码检查 num 的值是否除以 i,并且如果余数为 0 则返回...因此模数这里使用运算符%来求余数。这段代码类似于if(num % i==0)
。如果返回 true,则 i 的值应与 sum 相加。最后如果sum的值等于num的值,则该数字是完美的,并且显示该数字!
In very simple way, the
if(!(num%i))
code checks that if the value of num is divided by i and it returns if remainder is 0 or not...Hence the the modulus operator % is used here to find the remainder.. this piece of code is similar toif(num % i==0)
.if it returns true then the value of i should be added with sum. Finally if the value of sum is equal to the value of num, the number is perfect and the number is displayed!