java算法----质因数分解

package com.dazhongdianping.interview;

/**
 * 质因数分解,给定一个整数,求出该数的所有质因数,如90=2*3*3*5;
 * @author yangjianzhou
 *
 */
public class PrimeFactorization {
	
	public static void main(String[] args) {
		PrimeFactorization pf = new PrimeFactorization();
		pf.factorization(105);
	}
	
	public void factorization(int n){
		for(int i = 2;i<=n;i++){
			while(n>=i){
				if(n%i==0){
					System.out.println(i);
					n = n/i;
				}else{
					break;
				}
			}
		}
	}

}



运行结果:
3 5 7 

你可能感兴趣的:(java算法)