`{"id":"bc5ca7af-b112-4861-9dad-945c366520a8","name":"Wilsons Theorem","description":"you have been given number n and p, you need to find n! % p (n factorial mod p). p is a prime no.","inputFormat":"The first line contains the integer n and p. ","outputFormat":"Output an integer in a line containing n! modulo P.","constraints":"1&lt;= n &lt;= 10^9\r\n1&lt;= p &lt;= 10^9\r\n1&lt;= abs(n - p) &lt;= 1000","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.IOException;\r\nimport java.util.Scanner;\r\n\r\npublic class Main {\r\n\t\r\n\r\n\tpublic static void main(String[] args) throws NumberFormatException, IOException {\r\n\t\tScanner scn = new Scanner(System.in);\r\n\t\tStringBuilder sb = new StringBuilder();\r\n\t\t\r\n\t\t\tint n = scn.nextInt();\r\n\t\t\tint p = scn.nextInt();\r\n\r\n\t\t}\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"5 11","sampleOutput":"10\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"086949fa-3c5e-40c6-8a9c-f9be4b400318","name":"Number Theory For Experts","slug":"number-theory-for-experts","type":0},{"id":"e7dbeb4b-e828-42af-8a39-8a1d3f4f8c29","name":"Wilsons Theorem","slug":"wilsons-theorem","type":1}],"next":{"id":"9d5a9f10-abf3-450e-b16d-bb7e06f3e80f","name":"Modular Fast Exponentiation","type":1,"slug":"modular-fast-exponentiation"},"prev":{"id":"4940a5f2-3a1e-4899-a580-63aa0c1bc2c9","name":"Eulers Totient Function","type":1,"slug":"eulers-totient-function"}}}`

# Wilsons Theorem

you have been given number n and p, you need to find n! % p (n factorial mod p). p is a prime no.

`{"id":"bc5ca7af-b112-4861-9dad-945c366520a8","name":"Wilsons Theorem","description":"you have been given number n and p, you need to find n! % p (n factorial mod p). p is a prime no.","inputFormat":"The first line contains the integer n and p. ","outputFormat":"Output an integer in a line containing n! modulo P.","constraints":"1&lt;= n &lt;= 10^9\r\n1&lt;= p &lt;= 10^9\r\n1&lt;= abs(n - p) &lt;= 1000","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.IOException;\r\nimport java.util.Scanner;\r\n\r\npublic class Main {\r\n\t\r\n\r\n\tpublic static void main(String[] args) throws NumberFormatException, IOException {\r\n\t\tScanner scn = new Scanner(System.in);\r\n\t\tStringBuilder sb = new StringBuilder();\r\n\t\t\r\n\t\t\tint n = scn.nextInt();\r\n\t\t\tint p = scn.nextInt();\r\n\r\n\t\t}\r\n}"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"5 11","sampleOutput":"10\r\n","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"086949fa-3c5e-40c6-8a9c-f9be4b400318","name":"Number Theory For Experts","slug":"number-theory-for-experts","type":0},{"id":"e7dbeb4b-e828-42af-8a39-8a1d3f4f8c29","name":"Wilsons Theorem","slug":"wilsons-theorem","type":1}],"next":{"id":"9d5a9f10-abf3-450e-b16d-bb7e06f3e80f","name":"Modular Fast Exponentiation","type":1,"slug":"modular-fast-exponentiation"},"prev":{"id":"4940a5f2-3a1e-4899-a580-63aa0c1bc2c9","name":"Eulers Totient Function","type":1,"slug":"eulers-totient-function"}}}`

Editor

# Wilsons Theorem

medium

you have been given number n and p, you need to find n! % p (n factorial mod p). p is a prime no.

## Constraints

1<= n <= 10^9 1<= p <= 10^9 1<= abs(n - p) <= 1000

## Format

### Input

The first line contains the integer n and p.

### Output

Output an integer in a line containing n! modulo P.

## Example

Sample Input

`.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}5 11`

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}10 ```

Discussions

Show Discussion

Related Resources