您可以计算除以阶乘n的PrimeNumber的最大幂的指数!使用Legendre的公式。
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("输入n值:"); int n = sc.nextInt(); System.out.println("输入p值:"); int p = sc.nextInt(); int temp = n; int result = 0; while (temp > 0) { temp = temp / p; result = result + temp; } System.out.println("最大幂 "+p +" that divides "+ n+"! is :"+result); } }
输出结果
输入n值: 20 输入p值: 6 最大幂 6 that divides 20! is :3