Static void Main (string [] args) {// calculate the number of all completions in 1-. // The complete number, also known as the perfect number or the complete number, is a special natural number: the sum of all its true factors (except its own divisor) is exactly equal to itself. For example, 6: 6 = 1 + 2 + 3 // true factor, which is the number of all divisible numbers, but does not include the number itself // traverse 1 to 1000 for (int I = 1; I <= 1000; I ++) {int sum = 0; // define a variable to store all the factors of a number and // traverse the true factor of a number. // The true factor of an integer is less than or equal to half of the number, all just need to traverse to I/2 for (int j = 1; j <= I/2; j ++) {if (I % j = 0) // determine whether it can be divisible. It can be the true factor {sum + = j; // sum of all the true factors} if (I = sum) // determine whether the integer is equal to the sum of all its real factors {Console. writeLine (I + "is a complete number");} Console. readKey ();}
Result: 6, 28, 496.
This article is from the "journey to starship" blog, please be sure to keep this source http://stwzlx.blog.51cto.com/6117039/1279537