Legendre 公式在 Java 中\n


可以使用 Legendre 公式來計算最大素數階數的指數,它除以階乘 n!。

程式

import java.util.Scanner;

public class LegendresFormula {
   static int Largestpower(int n, int p) {
      int ans = 0;
      while (n > 0) {
         n /= p;
         ans += n;
      }
      return ans;
   }
   public static void main (String[] args) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter the n value :");
      int n = sc.nextInt();
      System.out.println("Enter the p value :");
     
      int p = sc.nextInt();
      int temp = n;
      int result = 0;
     
      while (temp > 0) {
         temp = temp / p;
         result = result + temp;
      }
      System.out.println("Largest power of "+p +" that divides "+ n+"! is :"+result);
   }
}

輸出

Enter the n value :
20
Enter the p value :
6
Largest power of 6 that divides 20! is :3

更新日期:2020 年 6 月 25 日

197 次觀看

開啟你的職業生涯生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.