Java decomposition prime factor, java prime factor

Source: Internet
Author: User

Java decomposition prime factor, java prime factor
 

1 package test; 2 3 import java. util. detail; 4 5 public class Test19 {6/** 7 * Analysis: For the decomposition of n, we should first find a minimum prime number k 8 9 * Minimum prime number: "2 ". 2 is the smallest prime number, that is, the even number and the prime number, and then follow the steps below: 10 11 * (1) if the prime number is equal to n, it indicates that the process of decomposing the prime factor has ended, print it out. 12 13 * (2) if n> k, but n can be divisible by k, then the value of k should be printed and divided by the quotient of n, as the new positive integer you n, repeat the first step. 14 15 * (3) if n cannot be divisible by k, k + 1 is used as the value of k and the first step is repeated. 16*17 * @ param think 18 */19 public static void main (String [] args) {20 seconds s = new seconds (System. in); 21 System. out. println ("type a positive integer:"); 22 int n = s. nextInt (); 23 int k = 2; // defines a minimum prime factor of 24 System. out. print (n + "="); 25 while (k <= n) {// cyclically judge 26 if (k = n) {27 System. out. print (n); 28 break; 29} else if (n % k = 0) {30 System. out. print (k + "*"); 31 n = n/k; 32} else k ++; 33} 34} 35}

 

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.