fly_fish_2005
发贴: 4
积分: 0
|
于 2006-01-06 13:48
public class ChangeIntToPrime { /* * function:判断temp是否为素数 * * */ public boolean checkPrime(int temp){ boolean flag=true; int i=(int)Math.sqrt(temp); for(int j=2;j<=i;j++ ){ if(temp%j!=0){ continue; }else{ flag=false; j=i+1; } } return flag; } /* * function:将temp分解为两个素数之和并打印 * * */ public void ChangeInt(int temp){ int i=(int)(temp/2); for(int j=2;j<=i;j++){ if(checkPrime(j)){ int k=temp-j; if(checkPrime(k)){ System.out.println("整数temp"+temp+"被分解为素数"+j+"和素数"+k+"之和"); break; } }else{ continue; } } } public static void main(String []args){ ChangeIntToPrime ch=new ChangeIntToPrime(); for(int i=6;i<=100;i=i+2){ ch.ChangeInt(i); } } }
|