Java Decomposition Factorization Foundation enhancements

Source: Internet
Author: User

Title: Decompose a positive integer into factorization. For example: Enter 90 and print out 90=2*3*3*5.

In order to be familiar with strengthening the basic exercises, engage in the classic small demo. Not much to say, directly paste the code, look at the comments. Package www.test; import Java.util.Scanner;

public class Resovle {public static void Main (string[] args) {/** 
Simple analysis: * If this prime number is exactly equal to N, then the process of decomposing the factorization is over and printing can be done. If n can be divisible by K, the value of k should be printed, and the quotient of n divided by K, as the new positive integer you n, repeat the first step. If n cannot be divisible by K, the first step is repeated with k+1 as the value of K.  * */
Scanner sc = new Scanner (system.in); Created the keyboard entry object System.out.println ("Enter the integer to decompose:"); int num = Sc.nextint ();      System.out.print (num+ "="); In order to output the format: 90=2*3*3*5.              Resolve (num); Sc.close (); Remember to close the stream}
/**
* Decomposition Method
*
*/
public static void Resolve (Int. num) {for (int i = 2; i < num; i+ +) {if (num% i = = 0) {System.out.print (i+ "*");//Call method, determine whether num/i is prime (prime number), is the prime number of direct output if (num/i >0 && isprime (num/i)) { System.out.print (num/i + "");
//Is not a prime number, can also be decomposed, recursive call (call yourself);}else{resolve (num/i);}    Break;}}} Determines whether the prime number is private static Boolean isprime (int num) {for (int i = 2; i < num; i++) {if (num% i = = 0) {return false;}} return true;}}

  

Java Decomposition Factorization Foundation enhancements

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.