用 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
廣告
資料結構
聯網
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP