English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

6 answers

Y = 6!

2007-09-10 09:50:01 · answer #1 · answered by Steve C 7 · 1 1

You have two methods to computer factorial, a recursive and an iterative one, the recursive one is simply as what we do on paper n! = n * n-1 * n-2 * ... * 3 * 2 * 1, and the iterative one is simply by inverting the formula above; i.e. n! = 1 * 2 * 3 ... * n-1 * n

Here are code snippets in java:

10 public static double factorial(int n) { //recursive function
20     if (n <= 1) return 1;
30     return n * factorial(n -1);
40 }

and the iterative one is:
10 public static double factorial(int n) {
20     double i = n;
30     for (; --n > 0; i *= n);
40     return i;
50 }

It's important to note that the iterative factorial method is 80% faster than the recursive one.

Thanks.

2007-09-10 12:27:21 · answer #2 · answered by Fox 3 · 0 0

there are a number of the thank you to get a factorial in programming the recursive version which takes the intger num in as a parameter, tests if is decrease than or equivalent to a minimum of one if no longer it calls its self whilst multiplying the consequence by ability of one decrease than num, it is repeated until num equals 0. int factorial( int num ) { if ( num <= a million ) return a million; else return num * factorial( n-a million ); } Or the interative way that may use any sort of loop you go with the only I did grow to be with a whilst loop and it behaves in a smilar vogue to the recursive function yet would not call it particularly is self, so it repeats the operation and returns the fee - it makes use of slightly extra reminiscence than the recursive function and is extremely slower, although as this is utilising purely integers it particularly in no way is going previous 8! int factorial( int n ) { int actuality = a million; whilst ( n > a million) { actuality = actuality * n; n = n - a million; } return actuality; }

2016-12-13 05:27:06 · answer #3 · answered by Anonymous · 0 0

Here's the SImple & easy method for computing Factorials in C / C++ / Java

Iterative or Looping method

Let N = Upper Limit of Range ( 1 ... N )

int computeFactorial(int limit){
 int result = 1;
 int i = 1;
 for (i = 1; i <= limit; i++){
  result = ( result * i ) // Iterative Formula
  }
 return (result);
}

Hope that is what u r looking for.

2007-09-10 10:06:20 · answer #4 · answered by Andy 3 · 0 0

You can use recursion to compute factorial.

So
Factorial(n)
{
if(n <= 1)
{
return 1;
} else {
return Factorian (n -1)
}
}

2007-09-10 09:51:57 · answer #5 · answered by asdf098 4 · 0 0

Must specify in which programming language!!!

2007-09-10 09:49:08 · answer #6 · answered by shadow 5 · 0 1

fedest.com, questions and answers