Home > Back-end >  And the number of
And the number of

Time:10-14

Ask, why do I only 1, the output is a logic where is wrong?



The class wanshu {
Public static void main (String args []) {
for(int i=1; i<=1000; I++) {
Int sum=0;
for(int j=1; j<=I; J++)
{
If (I % j==0) {
Sum=sum + j;
}
}
If (sum==I)
System.out.println(i);
}
}
}

CodePudding user response:

refer to the original poster weixin_45836043 response:
I ask, why I only 1, the output is a logic where is wrong?


Because you are the inner loop is equal to the number I added, plus any number greater than themselves, so you all the number is greater than 1 later ruled out,

CodePudding user response:

And inner loop can be optimized, and the number of any factor will not greater than his own half, j<=I/2, so you don't wrong,
  • Related