A:
Assuming a number n to be factorized. following steps:
1. from 1 to n, find the smallest prime number a by which n is divided;
2. if a equal to n, then exit;
3. if n is divided by a, then performs the division continuously, and the result is assigned to n;
4. if n is not divided by a, try the next number a = a + 1, and repeat 2 to 4;
public class IntegerFactoring {
- private static int getFactor(int num, int startpoint){
- if(num <= 1) return 1;
for(int i=startpoint+1; i<=num; i++){
- if((num % i) == 0) return i;
return 1;
public static void printFactoring(int n){
- int f = 1;
f=getFactor(n, f);
System.out.print(n + "=" + f);
n = n/f;
f--;
while((f=getFactor(n, f)) !=1){
- while(n%f ==0){
- n = n/f;
System.out.print("*" + f);
- n = n/f;
- while(n%f ==0){
public static void main(String[] args) {
- IntegerFactoring.printFactoring(90);
- if(num <= 1) return 1;
result:
90=2*3*3*5
No comments:
Post a Comment