Discover the Magic of Prime Numbers in Python.

Prime numbers use case.


In mathematics a prime number is a natural number that is greater than 1 that is not a product of two smaller natural numbers. In opposite to prime a composite number is a natural number greater than 1 that is a product of two smaller natural numbers. So all the natural numbers greater than 1 are prime or composite. The property of being prime is called primality.

Prime numbers.
Magic of Prime Numbers meme.

Python Knowledge Base: Make coding great again.
- Updated: 2024-11-20 by Andrey BRATUS, Senior Data Analyst.




    Today’s practical use cases of the Prime numbers lay in the fields of cryptography, geometry, biology, and quantum mechanics.

    Checking if number is Prime is one of the most common job interview questions for Python programmers.


  1. Python Function to check if a number is prime:


  2. 
    def prime(number):
         
        if number > 1:
    
           for i in range(2,number):
               if (number % i) == 0:
                   print(number,"is NOT a prime number")
                   print(i,"times",number//i,"is",number)
                   break
           else:
               print(number,"IS a prime number")
    
    
        else:
           print(number,"is NOT a prime number")
    
    # Check
    prime(24)
    print('\n')
    prime(23)
    

  3. Prime number function check result:


  4. OUT:
    24 is NOT a prime number
    2 times 12 is 24

    23 IS a prime number





See also related topics: