gpt4 book ai didi

java - 如何编写超阶乘程序?

转载 作者:行者123 更新时间:2023-12-01 09:16:46 27 4
gpt4 key购买 nike

Write a program to calculate and print the super factorial of a number using a for loop. The factorial of a number is the product of all integers up to and including that number, so the factorial of 4 (written as 4!) is 4*3*2*1= 24.

The super factorial is the product of all factorials up to and including that factorial.

4!!=4!*3!*2!*1!

我使用以下代码找到了“阶乘”:

import java.util.Scanner;
public class superfactorial {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner (System.in);

// number whose factorial to be found
int number;
// prompting input
System.out.print("Enter number: ");
number = input.nextInt();

int factorial = factorial(number);

System.out.printf("the factorial of %d is %d", number , factorial);
}

// method that calculates the factorial
public static int factorial (int n){
int output = 1;
for (int i=1; i <= n; i++) {
output = output * i;
}
return output;
}
}

最佳答案

考虑4! = 4x​​3x2x1,可以看到分解中有4个数字。一般来说n的分解会有n个数! (n(n-1)(n-2)....(n-(n-1))。因此,要获得超阶乘,您需要做的就是获取分解中每个分量的阶乘。

伪代码看起来像这样

sp = 0 
for i = n to 1:
sp = sp * factorial(i)
end for
return sp

关于java - 如何编写超阶乘程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40494052/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com