程序来寻找一个整数的最大值,对于这个整数,阶乘可以在机器上计算,假设阶乘是使用基本数据类型(如long int)存储的。
null
这个想法是基于这样一个事实,在大多数机器中,当我们超过整数的极限时,值变为负值。
C
// C program to find maximum value of // an integer for which factorial can // be calculated on your system #include <stdio.h> 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" , 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
<?php // PHP program to find maximum // value of an integer for which // factorial can be calculated // on your system function findMaxValue() { $res = 2; $fact = 2; $pos = -1; while (true) { // when fact crosses its size, // it gives negative value $mystring = $fact ; $pos = strpos ( $mystring , 'E' ); if ( $pos > 0) break ; $res ++; $fact = $fact * $res ; } return $res - 1; } // Driver Code echo "Maximum value of" . " integer " . findMaxValue(); // This code is contributed by Sam007 ?> |
Javascript
<script> // Javascript program to find maximum // value of an integer for which factorial // can be calculated on your system function findMaxValue() { let res = 2; let fact = 2; while ( true ) { // When fact crosses its size, // it gives negative value if (fact < 0 || fact > 9223372036854775807) break ; res++; fact = fact * res; } return res - 1; } // Driver Code document.write( "Maximum value of" + " integer " + findMaxValue()); // This code is contributed by rag2127 </script> |
输出:
Maximum value of integer : 20
本文由 普拉莫德·库马尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END