Solving the complete number of Java Basic algorithm learning by __ algorithm

Source: Internet
Author: User

This article is about the solution of the complete number, here I want to say is the idea of this code, because to implement this function, it is really not difficult, the total number, that is, all the factors are added equal to this number, then this number is called the complete number, the idea in this code is in a range, each time traversing a number, Then save two times this number, one for the factor, the other for whether the last and 0, feeling if it is the first time I think, should be unexpected.

Code:

Package Com.jk.perfectdemo;

Import Java.util.Arrays;
/**
 * 
 * @author JK, this code is written to complete the number of solutions, the total number is the factor is equal to the number of the number, the code is not much, but I think it can also, so I wrote
 * * */public
class test {

	/**
	 * @param args
	 *
	/public static void main (string[] args) {
		//Find the complete number within 5000
		perfectnum ( 5000);

	}

	private static void Perfectnum (int rangge) {
		long[] p=new long[300];
		Long I,j,sum,num;
		int k,count;
		Iterate through each number for
		(i=0;i<rangge;i++) {
			count=0;
			num=i;
			Sum=num;
			See if each number conforms to the full number of conditions for
			(j=1;j<num;j++) {
				if (num%j==0) {
					p[count++]=j;
					Sum=sum-j
				}
			}
			if (sum==0) {
				System.out.println (num+ "=" +arrays.tostring (p));}}}




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.