top of page
learn_data_science.jpg

Data Scientist Program

 

Free Online Data Science Training for Complete Beginners.
 


No prior coding knowledge required!

Checking if a Number is Prime or Not with Python

In this blog, we will use the Python programming language to write a simple application that checks if the number is prime or not.


Prime numbers are special numbers, greater than 1, that have exactly two factors, themselves and 1.

19 is a prime number. It can only be divided by 1 and 19.

9 is not a prime number. It can be divided by 3 as well as 1 and 9.


num = int(input('Enter the number: ')) 


Above code is the way to get input from the user, making it int data type and assigning it to a variable called num.


# If given number is greater than 1
if num > 1:
 
    # Iterate from 2 to n / 2
    for i in range(2, int(num/2)+1):
 
        # If num is divisible by any number between
        # 2 and n / 2, it is not prime
        if (num % i) == 0:
            print(num, "is not a prime number")
            break
    else:
        print(num, "is a prime number")
 
else:
    print(num, "is not a prime number")

Since the integer 1 is neither prime nor composite we first check the num to be greater than 1. If the num is greater than 1 then we write a for loop.


The loop starts from 2 and goes till num/2. Because the largest proper divisor of a number N can never be greater than N/2. Recall that range method does not include the second argument.


The next condition checks if the number is divisible by i, if yes, it prints out the message that the number is not prime, else, it says the number is prime. And the last else condition is a way of considering the numbers that are not greater than 1 (not prime).


Let us run the code and see an example:

Enter the number:11

When we enter 11 as an input, the output says:


11 is a prime number

Because 11 is only divisible by itself and 1, the output is correct.


Let us see another one:

Enter the number: 86

When we enter 86 as an input,

86 is not a prime number

is the result. Because 86 can be divided by 1, 2, 43, 86, it is a composite number not a prime number.

You can find the GitHub repo for the jupyter notebook and python code at this link

 
 
 

Comentários


COURSES, PROGRAMS & CERTIFICATIONS

 

Advanced Business Analytics Specialization

Applied Data Science with Python (University of Michigan)

Data Analyst Professional Certificate (IBM)

Data Science Professional Certificate (IBM)

Data Science Specialization (John Hopkins University)

Data Science with Python Certification Training 

Data Scientist Career Path

Data Scientist Nano Degree Program

Data Scientist Program

Deep Learning Specialization

Machine Learning Course (Andrew Ng @ Stanford)

Machine Learning, Data Science and Deep Learning

Machine Learning Specialization (University of Washington)

Master Python for Data Science

Mathematics for Machine Learning (Imperial College London)

Programming with Python

Python for Everybody Specialization (University of Michigan)

Python Machine Learning Certification Training

Reinforcement Learning Specialization (University of Alberta)

Join our mailing list

Data Insight participates in affiliate programs and may sometimes get a commission through purchases made through our links without any additional cost to our visitors.

bottom of page