"Program 14"
Title: Decompose a positive integer into factorization. For example: Enter 90 and print out 90=2*3*3*5.
1. Program Analysis: The decomposition of n factorization, should first find a minimum prime number k, and then the following steps to complete:
(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.
2. Program Source code:
1 ImportJava.util.Scanner;2 3 /*this article transferred from blog: www.cnblogs.com/java-1004 "program 14"5 title: Decompose a positive integer into factorization. For example: Enter 90 and print out 90=2*3*3*5. 6 Program Analysis: The decomposition of n factorization, should then follow the following steps to complete:7 (1) first to find a minimum prime number k. 8 (2) If the prime number is exactly equal to N, then the process of decomposing the factorization is finished and printed out. 9 (3) 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 n, repeat the first step. Ten (4) If n cannot be divisible by K, the first step is repeated with k+1 as the value of K. One */ A - Public classJAVA14 { - the Public Static voidMain (string[] args) { -System.out.print ("Please enter a positive integer:"); -Scanner Scanner =NewScanner (system.in); - intNumber =scanner.nextint (); + scanner.close (); - intnum =Number ; +System.out.print (num + "="); A BooleanA =true; at while(a) { -A =false; - for(inti = 2; I < math.sqrt (number); i++) { - BooleanPrime =true; - for(intj = 2; J < I; J + +) { - if(i% j = 0) { inPrime =false; - Break; to } + } - if(prime) { the if(Num >i) { * if(num% i = = 0) { $System.out.print (i + "*"));Panax NotoginsengNum/=i; -i = 2; theA =true; + Break; A } the}Else { + System.out.print (num); - Break; $ } $ } - } - } the } -}
Java rewrite "C Classic 100 Questions"--14