EmSAT Achieve Exam  >  EmSAT Achieve Notes  >  Python for EmSAT Achieve  >  Code: Check Prime Number

Code: Check Prime Number | Python for EmSAT Achieve PDF Download

Introduction

Prime numbers are an essential concept in mathematics and computer science. They play a significant role in various algorithms and are widely used in programming. In this article, we will explore how to check if a number is prime using Python. We will provide simple code examples and explanations to help beginners grasp the concept easily.

Understanding Prime Numbers

A prime number is a positive integer greater than 1 that has no positive divisors other than 1 and itself. In other words, it cannot be divided evenly by any other number except 1 and itself.

Checking Prime Numbers in Python

Let's dive into some code examples that demonstrate different approaches to check whether a given number is prime or not.

Method 1: Basic Iteration

One of the simplest ways to check if a number is prime is to iterate from 2 to the square root of the number and check if it has any divisors within that range.

import math

def is_prime_basic(num):

    if num < 2:

        return False

    for i in range(2, int(math.sqrt(num)) + 1):

        if num % i == 0:

            return False

    return True

# Testing the function

print(is_prime_basic(17))  # Output: True

print(is_prime_basic(25))  # Output: False

Explanation:

  • We import the 'math' module to access the 'sqrt()' function, which returns the square root of a number.
  • The 'is_prime_basic()' function takes a number as input.
  • We check if the number is less than 2 (as prime numbers start from 2), and return 'False' if it is.
  • We iterate from 2 to the square root of the number using the range() function.
  • Within the loop, we check if the number is divisible by the current iteration value (i). If it is, we return 'False' as it has a divisor other than 1 and itself.
  • If no divisors are found, the function returns 'True', indicating the number is prime.

Method 2: Optimized Iteration

We can optimize the above approach further by considering some mathematical properties of prime numbers. We only need to iterate up to the square root of the number, and we can skip even numbers greater than 2 since they are not prime.

import math

def is_prime_optimized(num):

    if num < 2:

        return False

    if num == 2:

        return True

    if num % 2 == 0:

        return False

    for i in range(3, int(math.sqrt(num)) + 1, 2):

        if num % i == 0:

            return False

    return True

# Testing the function

print(is_prime_optimized(17))  # Output: True

print(is_prime_optimized(25))  # Output: False

Explanation:

  • The 'is_prime_optimized()' function follows a similar logic to 'is_prime_basic()', but with some additional checks.
  • We first handle cases where the number is less than 2, equal to 2 (which is prime), or divisible by 2 (even number), returning 'False' in those cases.
  • The loop starts from 3 and increments by 2, skipping even numbers.
  • This optimized approach reduces unnecessary iterations and improves the efficiency of prime number checks.

Sample Problems

Problems 1: Check if the number 29 is prime.

print(is_prime_optimized(29))  # Output: True

Problems 2: Check if the number 100 is prime.

print(is_prime_optimized(100))  # Output: False

Problems 3: Find the sum of all prime numbers between 1 and 50.

primes_sum = sum(num for num in range(1, 51) if is_prime_optimized(num))

print(primes_sum)  # Output: 328

Conclusion

In this article, we explored how to check for prime numbers in Python using two different methods. We learned that prime numbers are integers greater than 1 with no divisors other than 1 and itself. The basic and optimized iteration methods provided simple and efficient ways to determine if a number is prime. By understanding these concepts and applying the code examples, you can solve problems involving prime numbers and build more complex algorithms in your Python programs.

The document Code: Check Prime Number | Python for EmSAT Achieve is a part of the EmSAT Achieve Course Python for EmSAT Achieve.
All you need of EmSAT Achieve at this link: EmSAT Achieve
49 videos|38 docs|18 tests

Top Courses for EmSAT Achieve

49 videos|38 docs|18 tests
Download as PDF
Explore Courses for EmSAT Achieve exam

Top Courses for EmSAT Achieve

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Semester Notes

,

MCQs

,

Code: Check Prime Number | Python for EmSAT Achieve

,

Code: Check Prime Number | Python for EmSAT Achieve

,

Objective type Questions

,

study material

,

Viva Questions

,

past year papers

,

Code: Check Prime Number | Python for EmSAT Achieve

,

Previous Year Questions with Solutions

,

pdf

,

ppt

,

Summary

,

Important questions

,

practice quizzes

,

mock tests for examination

,

Free

,

video lectures

,

Extra Questions

,

Exam

,

shortcuts and tricks

,

Sample Paper

;