📜  可以在计算机上计算其阶乘的整数的最大值

📅  最后修改于: 2021-04-29 03:42:43             🧑  作者: Mango

程序查找整数,可以在计算机上为其计算阶乘,并假设该阶乘使用诸如long long int之类的基本数据类型进行存储。

这个想法是基于这样的事实,在大多数机器中,当我们越过整数的极限时,值将变为负数。

C
// C program to find maximum value of 
// an integer for which factorial can 
// be calculated on your system
#include 
  
int findMaxValue()
{
    int res = 2;
    long long int fact = 2;
    while (1)
    {
        // when fact crosses its size, 
        // it gives negative value
        if (fact < 0)
            break;
        res++;
        fact = fact * res;
    }
    return res - 1;
}
  
// Driver Code
int main()
{
    printf ("Maximum value of integer : %d\n",
                                 findMaxValue());
    return 0;
}


Java
// Java program to find maximum value of 
// an integer for which factorial can be
// calculated on your system
import java.io.*;
import java.util.*;
  
class GFG
{
    public static int findMaxValue()
    {
        int res = 2;
        long fact = 2;
        while (true)
        {
            // when fact crosses its size, 
            // it gives negative value
            if (fact < 0)
                break;
            res++;
            fact = fact * res;
        }
        return res - 1;
    }
  
    // Driver Code
    public static void main(String[] args)
    {
        System.out.println("Maximum value of"+
                                 " integer " + 
                              findMaxValue());
    }
}


Python3
# Python3 program to find maximum value of 
# an integer for which factorial can be
# calculated on your system
import sys
def findMaxValue():
  
    res = 2;
    fact = 2;
    while (True):
          
        # when fact crosses its size
        # it gives negative value
        if (fact < 0 or fact > sys.maxsize):
            break;
        res += 1;
        fact = fact * res;
    return res - 1;
  
# Driver Code
if __name__ == '__main__':
      
    print("Maximum value of integer:", 
                      findMaxValue());
      
# This code is contributed by 29AjayKumar


C#
// C# program to find maximum value of 
// an integer for which factorial can 
// be calculated on your system
using System;
  
class GFG
{
    public static int findMaxValue()
    {
        int res = 2;
        long fact = 2;
        while (true)
        {
            // when fact crosses its size, 
            // it gives negative value
            if (fact < 0)
                break;
            res++;
            fact = fact * res;
        }
        return res - 1;
    }
  
    // Driver Code
    public static void Main()
    {
        Console.Write("Maximum value of"+
                            " integer " + 
                         findMaxValue());
    }
}
  
// This code is contributed by nitin mittal


PHP
 0)
            break;
        $res++;
        $fact = $fact * $res;
    }
    return $res - 1;
}
  
// Driver Code
echo "Maximum value of". 
           " integer " . 
         findMaxValue();
      
// This code is contributed by Sam007
?>


输出 :

Maximum value of integer : 20