Computer Science, asked by ishikarana249, 6 days ago

Wap of factorial using modular approach in C language.​

Answers

Answered by globalproprty
0

Answer:

Given a large number n and a prime p, how to efficiently compute n! % p?

Examples : 

 

Input: n = 5, p = 13 Output: 3 5! = 120 and 120 % 13 = 3 Input: n = 6, p = 11 Output: 5 6! = 720 and 720 % 11 = 5

A Naive Solution is to first compute n!, then compute n! % p. This solution works fine when the value of n! is small. The value of n! % p is generally needed for large values of n when n! cannot fit in a variable, and causes overflow. So computing n! and then using modular operator is not a good idea as there will be overflow even for slightly larger values of n and r. 

Similar questions