Java uses data structure to solve the mathematical problem of realization problem

Source: Internet
Author: User

Interesting integers:

Number: If a number is exactly equal to the sum of his factors, it is called the completion of the number, the demand is to find out within 10000 of all the finished number.

Solution: 1. Use N to divide all the integers between 1-n, and save the dividend that can be divisible into an array as a factor of N.

2. Subtract the factor by the number n to facilitate the calculation of the sum of the factors equal to N.

3. Repeat steps 1 and 2 to find all the finished numbers.

4. Finally determine whether the sum of the factors is equal to the number n if equal, then the number of n is the end number, output the number of factors.

Package finish; public class Perfectnum {public static void main (string[] args) {int p[] = new Int[30];int count = 0;int s = 0; int c = 0;int i = 0;for (int num = 2;num <=10000;num++) {s = num;for (i =1;i<num;i++) {if (num% i = = 0) {p[count++ ] = i;s-=i;}} if (s = = 0) {System.out.printf ("%d is a complete number, factor is", num); System.out.printf ("%d =%d", num,p[0]); for (i = 1;i<count;i++) {System.out.printf ("+%d", P[i]);} System.out.println (); C + +;}}}}

Java uses data structure to solve the mathematical problem of realization problem

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.