Calculate the number of all completions within 1000

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.