answersLogoWhite

0

The factorial of 0 is indeed 1, but that's not really a problem. The problem is that the factorial of 20 is 2,432,902,008,176,640,000 and that's the largest factorial that will fit in a 64-bit integer. With only 21 factorials (including 0) to play with, a simple lookup table would be a lot quicker than calculating each of them separately.

unsigned __int64 factorial(unsigned num)

{

switch (num)

{

case (0):

case (1): return 1;

case (2): return 2;

case (3): return 6;

case (4): return 24;

case (5): return 120;

//....

case (20): return 2432902008176640000;

}

return 0; // indicates error!

}

To calculate them individually, use the following function:

unsigned __int64 factorial (unsigned num)

{

if (20<num) return 0; // indicates error!

unsigned __int64 result= 1;

while (1<num) result *= num--;

return result;

}

To accommodate factorials greater than 20 you could use a 64-bit double-precision float, however the results will be an approximation rather than precise and the risk of overflowing still exists. The best solution is to employ a user-defined, dynamically-sized type specifically designed to cater for large integrals (although they'd no longer be integral, of course). There are many libraries available to cater for this but, personally, I use the GMP library as it's one of the fastest available.

User Avatar

Wiki User

11y ago

What else can I help you with?

Continue Learning about Engineering

How do you write a program that calculate factorial in javascript?

function factorial(n) { var x=1; while(n&gt;1) x*=(n--); return x; }


Write a program in java for factorial?

// Iterative solution public static final long iterativeFactorial(final long n) { long factorial = 1; for (long i = 1; i &lt;= n; i++) { factorial *= i; } return factorial; } // Recursive solution public static final long recursiveFactorial(final long n) { if (n &lt;= 1) { return n; } return n * recursiveFactorial(n - 1); } // Arbitrary length solution - may take a while, but works on any positive number. public static final BigInteger factorial(final BigInteger n) { BigInteger factorial = BigInteger.ONE; for (BigInteger i = BigInteger.ONE; i.compareTo(n) &lt;= 0; i = i.add(BigInteger.ONE)) { factorial = factorial.multiply(i); } return factorial; }


Write a recursive procedure to compute the factorial of a number?

#include &lt;iostream&gt; using namespace std; int main() { int i, number=0, factorial=1; // User input must be an integer number between 1 and 10 while(number&lt;1 number&gt;10) { cout &lt;&lt; "Enter integer number (1-10) = "; cin &gt;&gt; number; } // Calculate the factorial with a FOR loop for(i=1; i&lt;=number; i++) { factorial = factorial*i; } // Output result cout &lt;&lt; "Factorial = " &lt;&lt; factorial &lt;&lt; endl;


Programming to calculate a factorial number?

double factorial(double N){double total = 1;while (N > 1){total *= N;N--;}return total; // We are returning the value in variable title total//return factorial;}int main(){double myNumber = 0;cout > myNumber;cout


Program to find factorial of a number using inheritance?

/*71.PROGRAM TO FIND FACTORIAL OF A NUMBER USING RECURSION*/ #include&lt;stdio.h&gt; #include&lt;conio.h&gt; int fact(int); void main() { int n,f; clrscr(); printf("Enter number whose factorial is to be calculated: "); scanf("%d",&amp;n); if(n&gt;0) { f=fact(n); printf("factorial of %d is %d",n,f); } else printf("Factorial of numbers less than 1 does not exist"); getch(); } int fact(int n) { int facto=1; if(n&gt;1) facto=n*fact(n-1); else return 1; return(facto); }

Related Questions

What is the flowchart and pseudocode for a program that accept and displays the factorial of a number?

A flowchart for a program that accepts and displays the factorial of a number would include the following steps: Start, Input the number, Initialize a variable for the factorial, Use a loop to calculate the factorial by multiplying the variable by each integer up to the number, Output the result, and End. Pseudocode for the same program would look like this: START INPUT number factorial = 1 FOR i FROM 1 TO number DO factorial = factorial * i END FOR OUTPUT factorial END


Write a java program to find the factorial of a given number?

Here's a simple Java program to find the factorial of a given number using a recursive method: import java.util.Scanner; public class Factorial { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print(&quot;Enter a number: &quot;); int number = scanner.nextInt(); System.out.println(&quot;Factorial of &quot; + number + &quot; is &quot; + factorial(number)); } static int factorial(int n) { return (n == 0) ? 1 : n * factorial(n - 1); } } This program prompts the user for a number and calculates its factorial recursively.


A program for simple factorial in prolog?

In Prolog, a simple factorial program can be defined using recursion. Here's a basic implementation: factorial(0, 1). % Base case: factorial of 0 is 1 factorial(N, Result) :- N &gt; 0, N1 is N - 1, factorial(N1, Result1), Result is N * Result1. % Recursive case You can query the factorial of a number by calling factorial(N, Result). where N is the number you want to compute the factorial for.


What is the factorial of any number?

a factorial number is a number multiplied by all the positive integers i.e. 4!=1x2x3x4=24 pi!=0.14x1.14x2.14x3.14 0!=1


output of a c program using function to find factorial of a number?

In a C program that calculates the factorial of a number using a function, the program typically prompts the user for an integer input. The function then recursively or iteratively computes the factorial by multiplying the number by the factorial of the number minus one until it reaches one. For example, if the user inputs 5, the program outputs 120, as 5! = 5 × 4 × 3 × 2 × 1. The final result is displayed on the screen.


How do you do a factorial?

You first look at the number that is before the !(factorial sign). Then you times all positive integers (which means it doesn't include 0), including the number itself. The answer is the factorial of the original number beside the ! sign. EX.:4!=1x2x3x4=24


Program for finding the factorial of the two given number using constructor?

kjhk


What is the factorial of 40?

The factorial of a number n, denoted as n!, is the product of all positive integers up to n. In this case, the factorial of 40 is calculated as 40 × 39 × 38 × ... × 2 × 1. This results in an extremely large number, specifically 815915283247897734345611269596115894272000000000.


How do you write a program to factorial in PHP?

To write a program that calculates the factorial of a number in PHP, you can use a recursive function or an iterative approach. Here’s a simple example using a loop: function factorial($n) { $result = 1; for ($i = 2; $i &lt;= $n; $i++) { $result *= $i; } return $result; } echo factorial(5); // Outputs: 120 This code defines a function that multiplies numbers from 2 up to the given number $n to compute the factorial.


What is 5 as a factorial?

The factorial of a number, denoted as ( n! ), is the product of all positive integers up to that number. For 5, this is calculated as ( 5! = 5 \times 4 \times 3 \times 2 \times 1 = 120 ). Thus, 5 as a factorial is 120.


Which number is a factor of all positive numbers?

1 is a factor of all positive numbers.


What is a factorial of a number?

A factorial of a positive integer n, is the product of all positive integers less than or equal to n. For example the factorial of 5 is: 5! = 5 x 4 x 3 x 2 x 1 = 120 0! is a special case that is explicitly defined to be 1. A factorial is denoted by n! (5! for this example)