Efficient Prime Number Detection

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In mathematics, a prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. For example, 5 is prime because the only ways of writing it as a product, 1 × 5 or 5 × 1, involve 5 itself. However, 4 is not prime because it is a product (2 × 2) in which both numbers are smaller than 4. In this challenge, you need to write a Python function to check if a given number is prime or not.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/ControlFlowGroup(["`Control Flow`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/BasicConceptsGroup -.-> python/variables_data_types("`Variables and Data Types`") python/BasicConceptsGroup -.-> python/numeric_types("`Numeric Types`") python/BasicConceptsGroup -.-> python/booleans("`Booleans`") python/BasicConceptsGroup -.-> python/type_conversion("`Type Conversion`") python/ControlFlowGroup -.-> python/conditional_statements("`Conditional Statements`") python/ControlFlowGroup -.-> python/for_loops("`For Loops`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/using_packages("`Using Packages`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills python/comments -.-> lab-13672{{"`Efficient Prime Number Detection`"}} python/variables_data_types -.-> lab-13672{{"`Efficient Prime Number Detection`"}} python/numeric_types -.-> lab-13672{{"`Efficient Prime Number Detection`"}} python/booleans -.-> lab-13672{{"`Efficient Prime Number Detection`"}} python/type_conversion -.-> lab-13672{{"`Efficient Prime Number Detection`"}} python/conditional_statements -.-> lab-13672{{"`Efficient Prime Number Detection`"}} python/for_loops -.-> lab-13672{{"`Efficient Prime Number Detection`"}} python/tuples -.-> lab-13672{{"`Efficient Prime Number Detection`"}} python/function_definition -.-> lab-13672{{"`Efficient Prime Number Detection`"}} python/importing_modules -.-> lab-13672{{"`Efficient Prime Number Detection`"}} python/using_packages -.-> lab-13672{{"`Efficient Prime Number Detection`"}} python/standard_libraries -.-> lab-13672{{"`Efficient Prime Number Detection`"}} python/math_random -.-> lab-13672{{"`Efficient Prime Number Detection`"}} python/build_in_functions -.-> lab-13672{{"`Efficient Prime Number Detection`"}} end

Number is Prime

Write a Python function called is_prime(n) that takes an integer n as input and returns True if the number is prime, and False otherwise. To solve this problem, you need to follow these rules:

  • Return False if the number is 0, 1, a negative number or a multiple of 2.
  • Use all() and range() to check numbers from 3 to the square root of the given number.
  • Return True if none divides the given number, False otherwise.
from math import sqrt

def is_prime(n):
  if n <= 1 or (n % 2 == 0 and n > 2):
    return False
  return all(n % i for i in range(3, int(sqrt(n)) + 1, 2))
is_prime(11) ## True

Summary

In this challenge, you learned how to check if a given number is prime or not using Python. You used the all() and range() functions to check numbers from 3 to the square root of the given number. You also learned how to return True if none divides the given number, False otherwise.

Other Python Tutorials you may like