Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started