Given N numbers, calculate sum of their factorial modulo 107. (Note it is not 10^7).
what is the meaning of factorial modulo 107 in this problem.
Given N numbers, calculate sum of their factorial modulo 107. (Note it is not 10^7)
It means that you need to calculate the sum of the factorials and final result should be the( sum of factorials) %107
e.g, For input as
5
1
2
3
4
5
Their respective factorials are 1, 2, 6, 24, 120
Now the final ans should be like (1+2+6+24+120)%107=46.
Since, modulo 107 is taken so the final ans would definitely lie in between 0 to 106
thank you for the reply
import java.util.Scanner;
public class FactorialModulo {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int n = sc.nextInt();
int sum = TakingInput(n);
int ans = sum % 107;
System.out.println(ans);
}
public static int Factorial(int num) {
int fact = 1;
for (int i = 1; i <= num; i++) {
fact = fact * i;
}
return fact;
}
public static int TakingInput(int n) {
int count = 0;
int fact = 0;
while (count < n) {
int nm = sc.nextInt();
fact += Factorial(nm);
count++;
}
return fact;
}
}