赞
踩
package test;
public class PrimeTest {
public static void main(String [] args) {
//方法一
System.out.println(primeBreak(84));
//方法二
System.out.println(f(84));
}
public static String primeBreak(int num) {
//System.out.println(isPrime(num));
//is prime
if(isPrime(num)) {
return num+"";
}
//is not prime
else {
for(int a = 2; a < num; a++) {
for(int b = 2; b < num; b++) {
while( a*b == num){
return primeBreak(a) + "*" + primeBreak(b);
}
}
}
}
return "ok";
}
public static boolean isPrime(int index) {
boolean f = true;
for(int j=2; j<index; j++) {
if(index%j == 0) {
f = false;
break;
}
}
if(!f) {
return false;
}
else{
return true;
}
}
//Chengxusan(int n) {
// String s="";
// int n=0;
/* try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入一个数:");
s = in.readLine();
n =Integer.parseInt(s);
System.out.println(""+f(n));
}catch (IOException e){}
}*/
public static String f(int n){
for(int i=2;i<=n;i++){
while(n!=i){
if(n%i==0){
return i+"*"+f(n/i);
}
else
break;
}
}
return ""+n;
}
}
public class Test {
public static void main(String[] args) {
System.out.println(gongyue(10, 12));
}
//迭代n的阶乘
public static int method(int n) {
if (n == 1) {
return 1;
} else {
return n * method(n - 1);
}
}
// 求m和n的最大公约数
public static int gongyue(int m, int n) {
while (m % n != 0) {
int temp = m % n;
m = n;
n = temp;
}
return n;
}
// 求m和n的最小公倍数
public static int gongbei(int m, int n) {
return m * n / gongyue(m, n);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。