100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached
logo-home
Summary Understanding Prime Numbers and How to Check for Primality in Python $10.49
Add to cart

Summary

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

 0 purchase
  • Course
  • Institution

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 ...

[Show more]

Preview 1 out of 2  pages

  • March 5, 2023
  • 2
  • 2020/2021
  • Summary
avatar-seller
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.

The benefits of buying summaries with Stuvia:

Guaranteed quality through customer reviews

Guaranteed quality through customer reviews

Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.

Quick and easy check-out

Quick and easy check-out

You can quickly pay through credit card or Stuvia-credit for the summaries. There is no membership needed.

Focus on what matters

Focus on what matters

Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!

Frequently asked questions

What do I get when I buy this document?

You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.

Satisfaction guarantee: how does it work?

Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.

Who am I buying these notes from?

Stuvia is a marketplace, so you are not buying this document from us, but from seller user111. Stuvia facilitates payment to the seller.

Will I be stuck with a subscription?

No, you only buy these notes for $10.49. You're not tied to anything after your purchase.

Can Stuvia be trusted?

4.6 stars on Google & Trustpilot (+1000 reviews)

66781 documents were sold in the last 30 days

Founded in 2010, the go-to place to buy study notes for 15 years now

Start selling
$10.49
  • (0)
Add to cart
Added