Sunday, April 14, 2024
HomeSoftware EngineeringEasy methods to fulfill Wilson Primes in Python

Easy methods to fulfill Wilson Primes in Python

The problem

Wilson primes fulfill the next situation. Let P symbolize a primary quantity.

Then ((P-1)! + 1) / (P * P) ought to give a complete quantity.

Your activity is to create a perform that returns true if the given quantity is a Wilson prime.

The answer in Python code

Possibility 1:

def am_i_wilson(n):
    return n in (5, 13, 563)

Possibility 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

Possibility 3:

def am_i_wilson(n):
    return n == 5 or n == 13 or n == 563

Take a look at instances to validate our resolution

check.assert_equals(am_i_wilson(0), False)
check.assert_equals(am_i_wilson(1), False)
check.assert_equals(am_i_wilson(5), True)
check.assert_equals(am_i_wilson(8), False)
check.assert_equals(am_i_wilson(9), False)


Please enter your comment!
Please enter your name here

Most Popular

Recent Comments