# How to satisfy Wilson Primes in Python

## The challenge

Wilson primes satisfy the following condition. Let `P` represent a prime number.

Then `((P-1)! + 1) / (P * P)` should give a whole number.

Your task is to create a function that returns `true` if the given number is a Wilson prime.

## The solution in Python code

Option 1:

```.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.shcb-language{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines){white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```def am_i_wilson(n):
return n in (5, 13, 563)
```Code language: Python (python)```

Option 2:

``````def am_i_wilson(n):
if n < 2 or not all(n % i for i in xrange(2, n)):
return False

import math
return (math.factorial(n - 1) + 1) % (n ** 2) == 0
```Code language: Python (python)```

Option 3:

``````def am_i_wilson(n):
return n == 5 or n == 13 or n == 563
```Code language: Python (python)```

## Test cases to validate our solution

``````test.assert_equals(am_i_wilson(0), False)
test.assert_equals(am_i_wilson(1), False)
test.assert_equals(am_i_wilson(5), True)
test.assert_equals(am_i_wilson(8), False)
test.assert_equals(am_i_wilson(9), False)
```Code language: Python (python)```
Tags:
Subscribe
Notify of