Question: Write a program (primality test.py) that prompts the user for an integer, n, where n is greater than or equal to 2. Define a predicate

 Write a program (primality test.py) that prompts the user for an

Write a program (primality test.py) that prompts the user for an integer, n, where n is greater than or equal to 2. Define a predicate function, is_prime (n), that accepts an integer argument n and returns True if n is prime, False otherwise. A natural number (i.e. 1, 2, 3, 4,5, 6, etc.) is called a prime number (or a prime) if it has exactly two positive divisors, 1 and the number itself. Natural numbers greater than 1 that are not prime are called composite. To test if a number is prime or not, implement the trial division approach below (adapted from Wikipedia): Given an input number n, check whether any prime integer m from 2 to n evenly divides n (the division leaves no remainder). If n is divisible by any m then n is composite, otherwise it is prime For example, we can do a trial division to test the primality of 100. Let's look at all the divisors of 100: 2, 4, 5, 10, 20, 25, 50 Here we see that the largest factor is 100/2 = 50. This is true for all n: all divisors are less than or equal to n / 2. Note: If we take a closer look at the divisors, we will see that some of them are redundant. If we write the list differently: 100 = 2 50 = 4 25 = 5 x 20 = 1010=20 x 5 = 25 x 4 = 50 x 2 the redundancy becomes obvious. Once we reach 10, which is v100, the divisors just flip around and repeat. Therefore, we can further eliminate testing divisors greater than vn. Use functions where appropriate! Example outputs Please enter an integer>2: 100 100 is not prime! Please enter an integer >_ 2: 17 17 is prime! Please enter an integer >= 2: 97 97 is prime

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!