1. 如果一个数等于其所有因子之和,我们就称这个数为"完数",例如 6的因子为1,2,3 6=1+2+3 6就是一个完数.请编程打印出1000以内所有的完数
public class Wanshu {
public static int [] wanshu(int n){
int [] ws = new int[3];
int count = 0;
for(int i = 1;i < n;i++){
int sum = 0;
for(int j = 1;j < i;j++){
if(i%j == 0){
sum += j;
}
}
if(i == sum){
ws[count] = i;
count++;
System.out.print(i+"是完数\t"+i+"=");
for(int j = 1;j < i;j++){
if(i%j == 0){
if(j == 1){
System.out.print(j);
}else{
System.out.print("+"+j);
}
}
}
System.out.println();
}
}
return ws;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int []ws = wanshu(1000);
for(int i = 0;i < ws.length;i++){
System.out.println(ws[i]+" ");
}
}
}
public class Wanshu {
public static int wanshu(int i){
int sum = 0;
for(int j = 1;j < i;j++){
if(i%j == 0){
sum += j;
}
}
return sum;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
for(int i = 1;i <= 1000;i++){
if(wanshu(i) == i){
System.out.println(i+"是完数");
}
}
}
}
2.验证歌德巴赫猜想,输入一个大于6的偶数,请输出这个偶数能被分解为哪两个质数的和
如 10=3+7 12=5+7
质数:除了1和自身,不能被任何数整除的数
注意:以下任务必须用方法实现
提示:判断是否是质数的方案:
注:1.number为传入进来的数
2.Math.sqrt(number)为取此数的平方根
for(int i=2;i<=(int)(Math.sqrt(number));i++){
if (number %i == 0) //不是质数
否则是质数
}
public class Goethe {
/**
* @param number
* @return
* 判断某个数是否是质数
*/
public static boolean geothe(int number){
for(int i = 2;i <= (int)Math.sqrt(number);i++){
if(number%i == 0){
return false;
}
}
return true;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个偶数验证歌德巴赫猜想");
int number = scanner.nextInt();
for(int i = 1;i < number/2;i++){//判断一个数是质数,
//那么另一个则是(原数-这个数),只要是质数即可
if(geothe(i) && geothe(number - i)){
System.out.print(number+" 能验证歌德巴赫猜想 ");
System.out.println(number+" = "+i+" + "+(number-i));
}
}
}
}