Java decomposition factorization

Source: Internet
Author: User


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


Analysis: To decompose n factorization, you should first find a minimum prime number k, and then complete the following steps:

(1) If the prime number is exactly equal to N, then the process of decomposing the factorization is finished and printed out.

(2) if n>k, but n can be divisible by K, the value of k should be printed, and n divided by the quotient of K, as a new positive integer you n, repeat the first step.

(3) If n cannot be divisible by K, the first step is repeated with k+1 as the value of K.


public class Resolveprime {public static void main (string[] args) {System.out.println (Resolveprime (90)); System.out.println (Resolveprime (134)); System.out.println (Resolveprime (81));} /** * Decomposition factorization * @param num to be decomposed number * @return decomposed numeric result */public static string resolveprime (int num) {//define result string cache object, used to save result character St Ringbuffer sb = new StringBuffer (num + "=");//define the minimum prime number int i = 2;//to divide the method while (I <= num) {//If num can divide I, then I is a result of num number if (num% i = = 0) {//Save I to SB and follow *sb.append (i + "*");//Assign num divided by the value of I to Numnum = num/i;//Reset I to 2i = 2;} else {// If it is not divisible, then I self-increment i++;}} Removes the last * of the string cache object, returning the result to return sb.tostring (). substring (0, sb.tostring (). Length ()-1);}}

Output results

90=2*3*3*5134=2*6781=3*3*3*3


Java decomposition factorization

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.