100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Summary

Summary Understanding Prime Numbers and How to Check for Primality in Python

Rating
-
Sold
-
Pages
2
Uploaded on
05-03-2023
Written in
2020/2021

The document is a brief discussion about prime numbers and how to determine whether a given number is prime or not. It defines what prime numbers are, and provides a simple algorithm for testing whether a number is prime. The algorithm involves checking if the number is less than 2, equal to 2, or even. If it passes those tests, the algorithm then loops through all odd numbers less than or equal to the square root of the given number and checks for divisors. If the number is divisible by any of these odd numbers, it is not prime. If it makes it through the loop without finding a divisor, it is prime. The document also provides an implementation of the algorithm in Python and mentions more efficient algorithms for testing primality.

Show more Read less
Institution
Course








Whoops! We can’t load your doc right now. Try again or contact support.

Written for

Course

Document information

Uploaded on
March 5, 2023
Number of pages
2
Written in
2020/2021
Type
Summary

Subjects

Content preview

Mathematical Algorithms | Prime Number
Checker
Data Structures and Algorithms
In this lecture, we will discuss prime numbers and how to determine whether a given number is prime
or not. A prime number is a positive integer greater than 1 that has no positive integer divisors other
than 1 and itself. For example, 13 is a prime number because it is only divisible by 1 and 13.

To check whether a number is prime or not, we can use a simple algorithm. We start by checking if the
number is less than 2. If so, we know it is not prime. Next, we check if the number is equal to 2, in which
case it is prime. If the number is even, we can also immediately determine that it is not prime since it is
divisible by 2. After that, we loop through all odd numbers less than or equal to the square root of the
given number. If the number is divisible by any of these odd numbers, then it is not prime. Otherwise, it
is prime.

Here is an implementation of this algorithm in Python:

import math

def is_prime(n):
if n < 2:
return False
elif n == 2:
return True
elif n % 2 == 0:
return False
else:
limit = int(math.sqrt(n))
for i in range(3, limit+1, 2):
if n % i == 0:
return False
return True


In this implementation, we first check if the number is less than 2 and return False if so. Then, we check
if the number is 2 and return True if so. If the number is even, we return False. Finally, we loop through
all odd numbers less than or equal to the square root of the given number, checking if the number is
divisible by any of them. If so, we return False. If we make it through the loop without finding a divisor,
we return True.

It is worth noting that there are more efficient algorithms for testing primality, such as the Miller-Rabin
test or the AKS test. However, the algorithm described here is simple and works well for most practical
purposes.
$10.49
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
user111

Get to know the seller

Seller avatar
user111 University of Arizona College of Engineering
Follow You need to be logged in order to follow users or courses
Sold
0
Member since
2 year
Number of followers
0
Documents
8
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Frequently asked questions