Java-language perfect data code analysis, java-language solution code

Source: Internet
Author: User

Java-language perfect data code analysis, java-language solution code

1. Concepts

First, let's understand what is perfect number?

Problem description: if a natural number is used, the sum of all its true factors (that is, the approximate number except itself) exactly equals to itself. This number is called the complete number. Summary"

For example,

6 = 1 + 2 + 3
28 = 1 + 2 + 4 + 7 + 14
496 = 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248
8128 = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 127 + 254 + 508 + 1016 + 2032 + 4064

According to the definition of the number, it is not too difficult to use a program to solve the number. First, find all the real factors of the number and then add them to determine whether it is equal to itself. However, when the number is small, there is no problem. Once the number exceeds a certain value, the problem will arise and the execution efficiency of the program will become low.

When optimizing the algorithm logic of a program, we often consider a problem. How can we efficiently utilize the computer's features? Is there a large number of repeated useless algorithms defined by it? We will soon find another solution to this problem.

2. Description

2.1 Analysis

Here, will it be easy to think of the decomposition formula we mentioned earlier? Yes, we will use the factorization formula when solving the perfect number. Generally, there are three steps to solve the perfect number:

1. Find a certain number of prime numbers table

2. Calculate the factorization of a specified number using the prime number table

3. calculate all the true factors and check whether the number is perfect by means of factorization.

2.2 difficulties

At first glance, there are no problems with Step 1 and Step 2. We have discussed this in the previous two articles. If you are not clear about it, you can check it.

The focus is on the third step. How can we find the correct factors? The method is very simple. You should first know all the true factors (those who do not know the concept of the true factor) and add the number itself, it will be twice the number (some people do not know, should they know now ?), For example:

2 * 28 = 1 + 2 + 4 + 7 + 14 + 28 

In fact, this equation can be converted to: (the code is incorrect. I have used it)

Found? 2 and 7 are obtained by means of decomposition. Is there a simplified process?

2.3 conclusion

As long as we can find the factorization, we can use a loop to obtain the value behind the equation. Dividing the value by 2 is the true factor and the first eye of the equation, we may think of using the proportional series formula to solve the problem, however, the power operation can be used to calculate the values after the equation while reading the form decomposition array.

3. Code

Import java. util. arrayList; // calculates the number of perfect public class PerfectNumber {// input a value and calculates at least the number of perfect public static int [] lessThan (int number) {int [] primes = Prime. findPrimes (number); ArrayList list = new ArrayList (); for (int I = 1; I <= number; I ++) {int [] factors = factor (primes, i); if (I = fsum (factors) list. add (new Integer (I);} int [] p = new int [list. size ()]; Object [] objs = list. toArray (); for (int I = 0; I <p. length; I ++) {p [I] = (Integer) objs [I]). intValue () ;}return p ;}// break down the formula private static int [] factor (int [] primes, int number) {int [] frecord = new int [number]; int k = 0; for (int I = 0; Math. pow (primes [I], 2) <= number;) {if (number % primes [I] = 0) {frecord [k] = primes [I]; k ++; number/= primes [I];} else I ++;} frecord [k] = number; return frecord ;} // sum the formula private static int fsum (int [] farr) {int I, r, s, q; I = 0; r = 1; s = 1; q = 1; while (I <farr. length) {do {r * = farr [I]; q + = r; I ++;} while (I <farr. length-1 & farr [I-1] = farr [I]); s * = q; r = 1; q = 1;} return s/2 ;} public static void main (String [] args) {int [] pn = PerfectNumber. lessThan (1000); for (int I = 0; I <pn. length; I ++) {System. out. print (pn [I] + "");} System. out. println ();}}

Summary

The above is all about the analysis of perfect number code in Java language. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message!

Related Article

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.