用 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

更新於: 25-Jun-2020

195 瀏覽次數

開啟你的 職業生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.