用Java查找数字的礼貌
可以表示为正连续整数之和的数字称为礼貌数字。
Ex: 5 = 2+3
用正整数之和表示数字的方式的数量就是该数字的礼貌程度。
Ex: 9 = 4+5 || 2+3+4
算法
得到一个数的素数。
获得大于2的素因子的幂。
将它们加1。
将它们相乘,从结果中减去1。
程序
import java.util.Scanner;
public class PolitenessOfANumber {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number");
int num = sc.nextInt();
int count = 0, result = 1;
for(int i = 2; i< num; i++) {
while(num%i == 0) {
System.out.println(i+" ");
num = num/i;
if(i>2) {
count ++;
}
result = result*(count+1);
}
if(num >2) {
System.out.println(num);
}
System.out.println("Politeness of the given number is : "+(result-1));
}
}
}输出结果
Enter a number 216 2 2 2 3 3 3 Politeness of the given number is : 3