answersLogoWhite

0


Best Answer

Factorial (n) = n * Factorial (n-1) for all positive values n given Factorial (1) = Factorial (0) = 1.

Pseudo-code:

Function: factorial, f

Argument: positive number, n

IF n<=1 THEN

RETURN 1

ELSE

RETURN n * f(n-1)

END IF

User Avatar

Wiki User

9y ago

Still curious? Ask our experts.

Chat with our AI personalities

DevinDevin
I've poured enough drinks to know that people don't always want advice—they just want to talk.
Chat with Devin
ViviVivi
Your ride-or-die bestie who's seen you through every high and low.
Chat with Vivi
EzraEzra
Faith is not about having all the answers, but learning to ask the right questions.
Chat with Ezra
More answers
User Avatar

Wiki User

15y ago

public static final long factorial(final long n) {

if (n <= 1) {
return 1;

}

long _n = n;
for (int i = (int) (n - 1); i > 1; --i) {
_n *= i;

}

return _n;

}


we want an algorithm not code i mean not programing

User Avatar

User Avatar

Wiki User

14y ago

You get the factorial by multiplying the number with every number before down to 1.

Factorial of 3 would be 3! = 3 * 2 * 1 = 6 or the factorial of 5 would be 5! = 5 * 4 * 3 * 2 * 1 = 120.

User Avatar

User Avatar

Wiki User

14y ago

The classic algorithm to find N Factorial is ...

int nfact (int n) {

if (n 2) {

decimal_initialize (d, 2);

return;

}

while (N > 2) {

decimal_multiply (d, N);

N--;

}

return;

}

/* Example main line */

int main (int argc, char *argv[]) {

int N;

decimal Decimal = {2, NULL};

if (argc < 2) {

printf ("Enter N (or use command line) : ");

scanf_s ("%d", &N);

} else {

N = atoi (argv[1]);

}

printf ("Arbitrary: %u! = ", N);

decimal_NFactIterative (&Decimal, N);

decimal_print_digits (&Decimal, true);

return 0;

}

User Avatar

User Avatar

Wiki User

14y ago

The problem with factorials is they get large quickly, and overflow the underlying hardware's capability of representation. The simple example...

unsigned long nfactorial (unsigned long N) return N 2) {

decimal_initialize (d, 2);

return;

}

while (N > 2) {

decimal_multiply (d, N);

N--;

}

return;

}

/* Example main line */

int main (int argc, char *argv[]) {

int N;

decimal Decimal = {2, NULL};

if (argc < 2) {

printf ("Enter N (or use command line) : ");

scanf_s ("%d", &N);

} else {

N = atoi (argv[1]);

}

printf ("Arbitrary: %u! = ", N);

decimal_NFactIterative (&Decimal, N);

decimal_print_digits (&Decimal, true);

return 0;

}

Now, this works well, though for larger numbers you will need to increase your stack size parameter at link time. It also starts to take time. One way to improve that is to store more than one digit in each node of the linked list decimal. You could even store an entire 32 bit word in each element, but the complexity of doing so for this example exceeds the value of the lesson. You would also need to implement a division operator, because you will need to convert to decimal for display purposes.

User Avatar

User Avatar

Wiki User

9y ago

For any positive integer, n, factorial (n) can be calculated as follows:

- if n<2, return 1.

- otherwise, return n * factorial (n-1).

The algorithm is recursive, where n<2 represents the end-point.

Thus for factorial (5) we find the following recursive steps:

factorial (5) = 5 * factorial (4)

factorial (4) = 4 * factorial (3)

factorial (3) = 3 * factorial (2)

factorial (2) = 2 * factorial (1)

factorial (1) = 1

We've now reached the end-point (1 is less than 2) and the results can now filter back up through the recursions:

factorial (2) = 2 * factorial (1) = 2 * 1 = 2

factorial (3) = 3 * factorial (2) = 3 * 2 = 6

factorial (4) = 4 * factorial (3) = 4 * 6 = 24

factorial (5) = 5 * factorial (4) = 5 * 24 = 120

Thus factorial (5) = 120.

We can also use a non-recursive algorithm. The factorial of both 0 and 1 is 1 thus we know that the return value will always be at least 1. As such, we can initialise the return value with 1. Then we begin iterations; while 1<n, multiply the return value by n and then subtract 1 from n. We can better represent this algorithm using pseudocode:

Function: factorial (n), where n is an integer such that 0<=n. Returns an integer, f.

Let f = 1

Repeat while 1<n

Let f = f * n

Let n = n - 1

End repeat

Return f

User Avatar

User Avatar

Wiki User

15y ago

Factorial of n:

fac = 1

while n > 1

fac = fac * n

n = n - 1

User Avatar

User Avatar

Wiki User

14y ago

Recursive Factorial function:

long int factorial(int n)

{

if (n<=1)

return(1);

else

n=n*factorial(n-1);

return(n);

}

User Avatar

Add your answer:

Earn +20 pts
Q: How do algorithm to find the factorial of a number?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Write the Pseudocode to find the factorial of a number?

Pseudo code+factorial


Write an algorithm to print the factorial of given number and then draw the flowchart?

write an algorithm to print the factorial of a given number and then draw the flowchart. This looks like someones homework, defiantly someone looking for the easy way. When it comes to programming, the more you do the better you get. Experience counts (making your own mistakes and learning from the mistake).


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


Algorithm to find factorial using functions?

factorial using recursion style in c++ is unsigned int fact(unsigned int a) { if (a&lt;=1) return 1; else { f*=fact(a-1); return a; } } when using looping structure factorial is unsigned int fact (unsigned int n) { unsigned int i,f=1; for(i=1;i&lt;=n;i++) f*=i ; return f; }

Related questions

Write the Pseudocode to find the factorial of a number?

Pseudo code+factorial


Write an algorithm to print the factorial of given number and then draw the flowchart?

write an algorithm to print the factorial of a given number and then draw the flowchart. This looks like someones homework, defiantly someone looking for the easy way. When it comes to programming, the more you do the better you get. Experience counts (making your own mistakes and learning from the mistake).


What is the time complexity, in terms of Big O notation, for an algorithm that has a factorial time complexity of O(n!)?

The time complexity of an algorithm with a factorial time complexity of O(n!) is O(n!).


An algorithm that generates all r-permutations of an n-element set?

P(n,r)=(n!)/(r!(n-r)!)This would give you the number of possible permutations.n factorial over r factorial times n minus r factorial


What is the most efficient way to implement a factorial algorithm in a programming language?

The most efficient way to implement a factorial algorithm in a programming language is to use an iterative approach rather than a recursive one. This involves using a loop to multiply the numbers from 1 to the given input number to calculate the factorial. This method is more memory-efficient and faster than using recursion.


What algorithm allows to find the factorial?

Just multiply all the natural numbers from 1 to the number. For example, 7! = 7 x 6 x 5 x 4 x 3 x 2 x 1 = 5040.


How can you figure out combinations in math?

If you have N things and want to find the number of combinations of R things at a time then the formula is [(Factorial N)] / [(Factorial R) x (Factorial {N-R})]


Find flow chart for factorial of given number?

i need a pic of cuson


Find the number of distinguishable permutations of the letters in the word manatee?

Take the total number of letters factorial, then divide by the multiple letters factorial (a and e). 7! / (2!*2!) or 1260.


How do you find factorial of given number?

Factorials are the product of 1 and all the integers up to the given number. Simply put, 5 factorial or 5! = 5*4*3*2*1


How many zeros are possible factorial 10 to the power factorial 10?

To calculate the number of zeros in a factorial number, we need to determine the number of factors of 5 in the factorial. In this case, we are looking at 10 to the power of 10 factorial. The number of factors of 5 in 10! is 2 (from 5 and 10). Therefore, the number of zeros in 10 to the power of 10 factorial would be 2.


By using call by reference find the factorial of a number?

chutia mc,bc bhosdika