在Java中查找大数的阶乘
我试图用for循环和double数据类型的典型方式找到一个大数的阶乘,例如87856 但它显示出无限的结果,可能是因为它超过了它的极限 所以请告诉我如何找到一个非常大的数的阶乘 我的代码:在Java中查找大数的阶乘,java,factorial,largenumber,Java,Factorial,Largenumber,我试图用for循环和double数据类型的典型方式找到一个大数的阶乘,例如87856 但它显示出无限的结果,可能是因为它超过了它的极限 所以请告诉我如何找到一个非常大的数的阶乘 我的代码: class abc { public static void main (String[]args) { double fact=1; for(int i=1;i<=8785856;i++) { fact=fact*
class abc
{
public static void main (String[]args)
{
double fact=1;
for(int i=1;i<=8785856;i++)
{
fact=fact*i;
}
System.out.println(fact);
}
}
我是Java新手,但已经学习了一些IO处理和所有方面的概念。使用类
biginger
。(我不确定这是否适用于如此大的整数)提示:使用biginger
类,并准备为JVM提供大量内存。8785856的值代码>是一个非常大的数字 是类中的一个特殊保留值,当您超过adouble
所能容纳的最大值时使用
如果希望代码正常工作,请使用BigDecimal
类,但给定输入编号,不要期望程序很快完成执行。您可能需要重新考虑计算这个巨大的值。表示它肯定不适合在主内存中显示。这通过示例解释了java中的biginteger阶乘。公共静态void main(字符串[]args){
public static void main(String[] args) {
BigInteger fact = BigInteger.valueOf(1);
for (int i = 1; i <= 8785856; i++)
fact = fact.multiply(BigInteger.valueOf(i));
System.out.println(fact);
}
BigInteger事实=BigInteger.valueOf(1);
对于(inti=1;i,此代码应该可以正常工作:-
public class BigMath {
public static String factorial(int n) {
return factorial(n, 300);
}
private static String factorial(int n, int maxSize) {
int res[] = new int[maxSize];
res[0] = 1; // Initialize result
int res_size = 1;
// Apply simple factorial formula n! = 1 * 2 * 3 * 4... * n
for (int x = 2; x <= n; x++) {
res_size = multiply(x, res, res_size);
}
StringBuffer buff = new StringBuffer();
for (int i = res_size - 1; i >= 0; i--) {
buff.append(res[i]);
}
return buff.toString();
}
/**
* This function multiplies x with the number represented by res[]. res_size
* is size of res[] or number of digits in the number represented by res[].
* This function uses simple school mathematics for multiplication.
*
* This function may value of res_size and returns the new value of res_size.
*/
private static int multiply(int x, int res[], int res_size) {
int carry = 0; // Initialize carry.
// One by one multiply n with individual digits of res[].
for (int i = 0; i < res_size; i++) {
int prod = res[i] * x + carry;
res[i] = prod % 10; // Store last digit of 'prod' in res[]
carry = prod / 10; // Put rest in carry
}
// Put carry in res and increase result size.
while (carry != 0) {
res[res_size] = carry % 10;
carry = carry / 10;
res_size++;
}
return res_size;
}
/** Driver method. */
public static void main(String[] args) {
int n = 100;
System.out.printf("Factorial %d = %s%n", n, factorial(n));
}
}
公共类BigMath{
公共静态字符串阶乘(int n){
收益阶乘(n,300);
}
私有静态字符串阶乘(int n,int maxSize){
int res[]=新的int[maxSize];
res[0]=1;//初始化结果
int res_size=1;
//应用简单的阶乘公式n!=1*2*3*4…*n
对于(int x=2;x=0;i--){
buff.append(res[i]);
}
返回buff.toString();
}
/**
*此函数将x乘以res[]表示的数字。res_size
*是res[]的大小或res[]表示的数字中的位数。
*此函数使用简单的学校数学进行乘法运算。
*
*此函数可以修改res_size的值,并返回res_size的新值。
*/
专用静态整数乘法(整数x,整数分辨率[],整数分辨率大小){
int carry=0;//初始化进位。
//将n与res[]的各个数字一一相乘。
对于(int i=0;i
使用BigInteger解决您的问题(8785856!)的上述解决方案将花费数小时的CPU时间,如果不是数天的话。您需要确切的结果还是近似值就足够了
有一种数学方法叫做“
“这可以简单快速地计算,下面是Gosper的改进:
要真正找出这个数字的阶乘,应该使用PYTHON函数,并尝试打开任务管理器,查看编译器占用的内存量。
之后,您将知道JVM将花费多少时间,因为PYTHON是进行数值计算的最佳语言。import java.util.*;
import java.util.*;
import java.math.*;
class main
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
int i;
int n=sc.nextInt();
BigInteger fact = BigInteger.valueOf(1);
for ( i = 1; i <= n; i++)
{
fact = fact.multiply(BigInteger.valueOf(i));
}
System.out.println(fact);
}
}
导入java.math.*;
班长
{
公共静态void main(字符串参数[])
{
扫描仪sc=新的扫描仪(System.in);
int i;
int n=sc.nextInt();
BigInteger事实=BigInteger.valueOf(1);
对于(i=1;i试试这个:
import java.math.BigInteger;
public class LargeFactorial
{
public static void main(String[] args)
{
int n = 50;
}
public static BigInteger factorial(int n)
{
BigInteger result = BigInteger.ONE;
for (int i = 1; i <= n; i++)
result = result.multiply(new BigInteger(i + ""));
return result;
}
}
import java.math.biginger;
公共类大阶乘
{
公共静态void main(字符串[]args)
{
int n=50;
}
公共静态双整数阶乘(int n)
{
BigInteger结果=BigInteger.ONE;
对于(int i=1;iScanner r=新扫描仪(System.in);
系统输出打印(“输入编号:”);
int num=r.nextInt();
int ans=1;
如果(数字0){
System.out.println(num+“x”);
ans*=num--;
}
System.out.println(“\b\b=“+ans”);
公共静态void main(字符串[]args)抛出java.lang.Exception
{
BigInteger事实=BigInteger.1;
整数阶乘号=8785856;
对于(int i=2;我为什么不使用更高的数据类型,如biginger
,这也是家庭作业?如果是,则标记为这样。循环变量i为int,您试图在其中存储一个巨大的数字,尝试将其设为双字节。根据我的计算,表示这个数字大约需要25兆字节的内存。甚至不是嗯,那就不会有人听到BigInteger类的声音了。是的,我也使用过BigInteger,但是编译器无法显示这样一个数字的答案,所以还有其他的解决办法吗??“无法显示答案”是什么意思?你能发布“无法显示答案”的代码吗?是的,我也使用了biginteger,但编译器无法显示这样一个数字的答案….是的,我也使用了biginteger,但编译器无法显示这样一个数字的答案,所以还有其他解决方法吗???尝试增加堆大小。是的,我也使用了biginteger,但编译器无法显示su的答案有没有别的办法呢?@HimanshuAggarwal你可以尝试一个讨厌的方法,比如为整数编写自己的乘法例程。例如,你可以用两个大整数来表示一个大整数。这将是一个复杂而麻烦的过程。例如,如果我的整数大小为4位,那么要表示像18这样的数字,我可以使用2个整数rs:1和8具有适当的权重。(1具有10^1的权重,8具有10^1的权重)。因此,如果您可以
import java.util.Scanner;
public class factorial {
public static void main(String[] args) {
System.out.println("Enter the number : ");
Scanner s=new Scanner(System.in);
int n=s.nextInt();
factorial f=new factorial();
int result=f.fact(n);
System.out.println("factorial of "+n+" is "+result);
}
int fact(int a)
{
if(a==1)
return 1;
else
return a*fact(a-1);
}
}
import java.math.BigInteger;
public class LargeFactorial
{
public static void main(String[] args)
{
int n = 50;
}
public static BigInteger factorial(int n)
{
BigInteger result = BigInteger.ONE;
for (int i = 1; i <= n; i++)
result = result.multiply(new BigInteger(i + ""));
return result;
}
}
Scanner r = new Scanner(System.in);
System.out.print("Input Number : ");
int num = r.nextInt();
int ans = 1;
if (num <= 0) {
ans = 0;
}
while (num > 0) {
System.out.println(num + " x ");
ans *= num--;
}
System.out.println("\b\b=" + ans);
public static void main (String[] args) throws java.lang.Exception
{
BigInteger fact= BigInteger.ONE;
int factorialNo = 8785856 ;
for (int i = 2; i <= factorialNo; i++) {
fact = fact.multiply(new BigInteger(String.valueOf(i)));
}
System.out.println("Factorial of the given number is = " + fact);
}