直線公式的 Java 實現\n
您可以使用勒讓德公式計算除以階乘 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
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP