史密斯数java_Java中的史密斯数字

一个合成数字,其数字总和等于其素因子的数字总和。Ex: 58 = 2 x 29

(5 + 8 = 12) (2+ 2 + 9 = 12)

程序public class SmithNumbers {

public static boolean isPrime(int number) {

int loop;

int prime = 1;

for(loop = 2; loop 

if((number % loop) == 0) {

prime = 0;

}

}

if (prime == 1) {

return true;

} else {

return false;

}

}

public static ArrayList primeFactors(int number) {

int sum = 0;

ArrayList al = new ArrayList();

for(int i = 2; i

while(number % i == 0) {

System.out.println(i+" ");

al.add(i);

number = number/i;

}

}

if(number >2) {

System.out.println(number);

al.add(number);

}

return al;

}

public static void main(String args[]) {

Scanner sc = new Scanner(System.in);

System.out.println("输入数字:");

int num = sc.nextInt();

int sum = 0;

if(isPrime(num)) {

System.out.println("Given number is not smith number ");

} else {

ArrayList arrayList = primeFactors(num);

System.out.println("Prime factors of the given number are"+arrayList);

Iterator it = arrayList.iterator();

while(it.hasNext()) {

int n = (int) it.next();

System.out.println(n);

while(n!=0) {

sum = sum+n%10;

n = n/10;

}

}

int temp = 0;

while(num != 0) {

temp = temp + num%10;

num = num/10;

}

System.out.println("sum of the digits of the numbers of the prime factorization: "+sum);

System.out.println("Sum of digits in the given number: "+temp);

if(temp==sum) {

System.out.println("Given number is a smith number");

} else {

System.out.println("Given number is not smith number ");

}

}

}

}

输出结果输入数字:

648

2

2

2

3

3

3

3

Prime factors of the given number are[2, 2, 2, 3, 3, 3, 3]

2

2

2

3

3

3

3

sum of the digits of the numbers of the prime factorization: 18

Sum of digits in the given number: 18

Given number is a smith number

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值