一个合成数字,其数字总和等于其素因子的数字总和。
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 < number; loop++) { if((number % loop) == 0) { prime = 0; } } if (prime == 1) { return true; } else { return false; } } public static ArrayList<Integer> primeFactors(int number) { int sum = 0; ArrayList al = new ArrayList(); for(int i = 2; i< number; 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<Integer> 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