Primality Test in Python

AlgorithmAlgorithmBeginner
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. In other words, a prime number is a positive integer that has no positive integer divisors other than 1 and itself. In this Python challenge, we will write a program to check if a given number is prime or not.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL algorithm(("`Algorithm`")) -.-> algorithm/BasicAlgorithmsGroup(["`Basic Algorithms`"]) 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/ObjectOrientedProgrammingGroup(["`Object-Oriented Programming`"]) python(("`Python`")) -.-> python/ErrorandExceptionHandlingGroup(["`Error and Exception Handling`"]) python(("`Python`")) -.-> python/PythonStandardLibraryGroup(["`Python Standard Library`"]) algorithm/BasicAlgorithmsGroup -.-> algorithm/math_probability("`Math Probability`") 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/standard_libraries("`Common Standard Libraries`") python/ObjectOrientedProgrammingGroup -.-> python/classes_objects("`Classes and Objects`") python/ObjectOrientedProgrammingGroup -.-> python/encapsulation("`Encapsulation`") python/ErrorandExceptionHandlingGroup -.-> python/raising_exceptions("`Raising Exceptions`") python/PythonStandardLibraryGroup -.-> python/math_random("`Math and Random`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") subgraph Lab Skills algorithm/math_probability -.-> lab-268849{{"`Primality Test in Python`"}} python/variables_data_types -.-> lab-268849{{"`Primality Test in Python`"}} python/numeric_types -.-> lab-268849{{"`Primality Test in Python`"}} python/booleans -.-> lab-268849{{"`Primality Test in Python`"}} python/type_conversion -.-> lab-268849{{"`Primality Test in Python`"}} python/conditional_statements -.-> lab-268849{{"`Primality Test in Python`"}} python/for_loops -.-> lab-268849{{"`Primality Test in Python`"}} python/tuples -.-> lab-268849{{"`Primality Test in Python`"}} python/function_definition -.-> lab-268849{{"`Primality Test in Python`"}} python/importing_modules -.-> lab-268849{{"`Primality Test in Python`"}} python/standard_libraries -.-> lab-268849{{"`Primality Test in Python`"}} python/classes_objects -.-> lab-268849{{"`Primality Test in Python`"}} python/encapsulation -.-> lab-268849{{"`Primality Test in Python`"}} python/raising_exceptions -.-> lab-268849{{"`Primality Test in Python`"}} python/math_random -.-> lab-268849{{"`Primality Test in Python`"}} python/build_in_functions -.-> lab-268849{{"`Primality Test in Python`"}} end

Check Prime

Problem

Write a Python function that takes an integer as input and returns True if the number is prime, and False otherwise. If the input is not an integer or less than 2, the function should raise an exception.

A number is considered prime if it is only divisible by 1 and itself. For example, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, and 97 are the first 25 prime numbers.

Requirements

The program should meet the following requirements:

  • The function should take an integer as input.
  • If the input is not an integer or less than 2, the function should raise an exception.
  • The function should return True if the input is a prime number, and False otherwise.
  • The program should not consider 1 as a prime number.

Example Usage

  • check_prime(None) -> Exception
  • check_prime('hello') -> Exception
  • check_prime(1) -> False
  • check_prime(7) -> True

Summary

In this Python challenge, we have learned how to check if a given number is prime or not. We have written a Python function that takes an integer as input and returns True if the number is prime, and False otherwise. We have also discussed the requirements of the program and provided some example usages.

Other Algorithm Tutorials you may like