Java中的Legendre公式
您可以计算除以阶乘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
热门推荐
10 2026祝福语简短创意牛
11 结婚婚庆祝福语简短
12 生日祝福语古语简短创意
13 祝福语对联文案简短大气
14 圣诞简短祝福语给父母
15 甄嬛传祝福语简短
16 韩语贺卡祝福语简短版
17 侄儿生日祝福语简短独特
18 汤姆猫祝福语简短霸气