Challenge CPU Computing power-Find the perfect number (Perfect Numbers)

Source: Internet
Author: User
Tags int size

There are 1 of the divisors of any natural number and itself, and we call the factor less than itself the true divisor of the natural number.
For example, all true divisors of 6 are 1, 2, 3, and 6=1+2+3. Like this, the sum of all the true divisors of a number is exactly equal to this number, usually the number is called the perfect number.

Before writing this code, did not think the perfect number is so few, 100 within only 6, 282, 100 million within, also only 4.

To calculate the perfect number within 100 million, it is really challenging the computing power of the computer Ah, two hours did not finish, where to borrow a supercomputer to use it.

The code is as follows:

package example; JHTP Exercise 6.24:perfect Numbers//by pandenghuang@163.com (/** Perfect) An integer number are Numbers to being a per Fect number if it factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number, because 6 = 1 + 2 + 3.
Write A method Isperfect This determines whether parameter number is a perfect number. Use the "method" in a application that displays all the perfect numbers between 1 and 1000. Display the factors of each perfect number to confirm this number is indeed perfect. Challenge the computing power of your computer by testing numbers much larger 1000.
Display the results.*/import Java.util.Scanner;

Import Java.util.Date;
		public class Perfectnumber {public static Boolean isperfect (int number) {int sum=0;
		for (int i=1;i<number;i++) {if (number%i==0) sum+=i;
		} if (Sum==number) return true;
	else return false;
	public static void Main (string[] args) {int size=0; int Count=0;
	Long begintime=0;
	Long endtime=0;

	Double duration=0.0;
	
	Scanner input=new Scanner (system.in);
		do {System.out.print ("Enter the upper limit (integer, enter-1 exit)");
		Size=input.nextint ();
		if (size==-1) System.out.print ("exited program");
		else{begintime=new Date (). GetTime ();
				for (int i=1;i<=size;i++) {if (Isperfect (i)) {System.out.printf ("%d\t", I);
				count++;
			if (count%10==0) System.out.println ();
			} endtime=new Date (). GetTime ();
			Duration= (Double) (endtime-begintime)/1000/60; System.out.printf ("The perfect number of \n%d below has%d, which is%.2f minutes in total.
			
	\ n ', size,count,duration);
} while (Size!=-1);
 }
}


Run Result:

Please enter the upper limit (integer, enter-1 exit): 10000
6 28 496 8128
The perfect number under 10000 has a total of 4, 0.00 minutes.

Please enter the upper limit (integer, enter-1 exit): 100000
6 28 496 8128
The perfect number under 100000 has a total of 8, 0.34 minutes.
 

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.