GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.practicepython.org — 11 Check Primality Functions Solutions
Practice PythonBeginner Python exercises16 April 2014Check Primality Functions SolutionsExercise 11Ask the user for a number and determine whether the number is prime or not. (For those who have forgotten, a prime number is a number that has no divisors.). You can (and should!) use your answer to Exercise 4 to help you. Sample solutionThere are many ways of solving this problem, so here are a sample solutions: This one is a different breakdown of functions to solve the problem. The strings between three And here is a solution without using functions. It is also a correct solution that accomplishes the given task, just without the use of functions. This solution doesn’t use functions, but does use list comprehensions, which are always fun. Thanks to Carlos for this solution. The interesting thing here is the observation that when you want to check if a number is prime, all you need to do is check the numbers from 2 to the square root of the number. This is because the pair of numbers that are both the largest factors of the number are Another solution is a clean, short solution that uses list comprehensions. Enjoying Practice Python?comments powered by Disqus Copyright Michele Pratusevich, 2014-2022.
|
| You are visitor 01402809 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |