Java programming implementation of qualitative data and line-based decomposition code sharing, prime data line-based Decomposition

Source: Internet
Author: User

Java programming implementation of qualitative data and line-based decomposition code sharing, prime data line-based Decomposition

1. Solving prime numbers

1.1 description

First, let's understand the concept of prime number? Prime Number: if a single number can only be divided by one, such a number is called a prime number, and corresponding to it is called a number. Based on this concept, we can quickly think of a method, that is, starting from 1, constantly testing to see whether numbers can be divided from 1 to itself.

In this case, the quantity of quality is very simple. Is there a more convenient way for us? Here we will introduce a famous method of Eratosthenes for determining the quality.

1.2 Solution

First, we know that this problem can be solved by using a loop. Dividing a specified number by all the smaller ones is not a prime number if the number can be divisible. But how can we reduce the number of rounds of check? How can we find all prime numbers smaller than N?

Assume that the number to be checked is N. In fact, you only need to check the root number of N. The principle is very simple. Suppose A * B = N. If A is greater than N, in fact, the check before A is smaller than A can first check that the number of B can be divisible by N. However, when using the root number in a program, it will be accurate. Therefore, you can use I * I <= N to perform the check and run it faster.

Let's assume there is a sieve to store 1 ~ N, for example:

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 19 20 21 ...... N

First, filter the multiples of 2:

2 3 5 7 9 11 13 15 17 19 21 ...... N

Then sieve the multiples of 3:

2 3 5 7 11 13 17 19 ...... N

Then we will screen the multiples of 5, then the prime number of 7, and then the multiples of 11 ........, in this way, the number left at the end is a prime number, which is the Eratosthenes filtering method (EratosthenesSieveMethod ).

The number of checks can be reduced. In fact, you only need to check 6n + 1 and 6n + 5, that is, you can directly skip the multiples of 2 and 3, this reduces the if check action in the program.

Code 1.3

Import java. util. *; public class Prime {public static int [] findPrimes (final int max) {int [] prime = new int [max + 1]; ArrayList list = new ArrayList (); for (int I = 2; I <= max; I ++) prime [I] = 1; for (int I = 2; I * I <= max; I ++) {// here we can improve if (prime [I] = 1) {for (int j = 2 * I; j <= max; j ++) {if (j % I = 0) prime [j] = 0 ;}}for (int I = 2; I <max; I ++) {if (prime [I] = 1) {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;} public static void main (String [] args) {int [] prime = Prime. findPrimes (1000); for (int I = 0; I <prime. length; I ++) {System. out. print (prime [I] + "");} System. out. println ();}}

2. factorization

2.1 description

As shown above, let's take a look at what is factorization? Converting a number into the product of another number is called an factorization. After learning about this concept, we should be able to understand the solution prime numbers mentioned above. In fact, here we are solving one and several factors.

The formula decomposition basically uses a value smaller than the input number as the divisor, which is divided by the input value. If the Division can be completed, it is regarded as a factor. A quick solution is to find all the prime numbers smaller than the number, and try to see if it can be divided.

Code 2.2

import java.util.ArrayList;  public class Factor {   public static int[] factor(int num) {     int[] pNum = Prime.findPrimes(num);          ArrayList list = new ArrayList();          for(int i = 0; pNum[i] * pNum[i] <= num;) {        if(num % pNum[i] == 0) {          list.add(new Integer(pNum[i]));         num /= pNum[i];        }        else          i++;      }       list.add(new Integer(num));          int[] f = new int[list.size()];     Object[] objs = list.toArray();     for(int i = 0; i < f.length; i++) {       f[i] = ((Integer) objs[i]).intValue();     }          return f;   }      public static void main(String[] args) {     int[] f = Factor.factor(100);     for(int i = 0; i < f.length; i++) {       System.out.print(f[i] + " ");     }     System.out.println();   } } 

3. Summary

Solving prime numbers and factorization are the basic skills for learning programs and algorithms. You should be familiar with them. The code here only has a small amount of comments, which may be a little hard for beginners, but this is the first step into the Hall of program algorithms. You can copy the code to your machine and add comments to the machine to make the program process clearer.

The above is all the content of this article on Java programming implementation quality and line-based decomposition code, I hope to help 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.