0

I`m trying to implement a java code which n,k is integer (can not be long type) which calculate the result of n! %k for any n,k. for ex, for n=18 and k=71 I want to get 56 and not -18.

I`m thinking that this algorithm should works through modules math, since the result is built by n=k*m+r (r is the division). Any ideas how to implement this algorithm with integer type?

Below is my code and is working but not for any n,k:

    int n = scanner.nextInt();
    int k = scanner.nextInt();
    int result = 1;
    //n!
    while (n!=0){
        result = result * n;
        n=n-1;
    }
    System.out.println(result % k);
John D
  • 175
  • 7

0 Answers0