100% tevredenheidsgarantie Direct beschikbaar na je betaling Lees online óf als PDF Geen vaste maandelijkse kosten 4,6 TrustPilot
logo-home
Samenvatting

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

Beoordeling
-
Verkocht
-
Pagina's
2
Geüpload op
05-03-2023
Geschreven 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.

Meer zien Lees minder
Instelling
Vak








Oeps! We kunnen je document nu niet laden. Probeer het nog eens of neem contact op met support.

Geschreven voor

Vak

Documentinformatie

Geüpload op
5 maart 2023
Aantal pagina's
2
Geschreven in
2020/2021
Type
Samenvatting

Onderwerpen

Voorbeeld van de inhoud

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.99
Krijg toegang tot het volledige document:

100% tevredenheidsgarantie
Direct beschikbaar na je betaling
Lees online óf als PDF
Geen vaste maandelijkse kosten

Maak kennis met de verkoper
Seller avatar
user111

Maak kennis met de verkoper

Seller avatar
user111 University of Arizona College of Engineering
Volgen Je moet ingelogd zijn om studenten of vakken te kunnen volgen
Verkocht
0
Lid sinds
2 jaar
Aantal volgers
0
Documenten
8
Laatst verkocht
-

0.0

0 beoordelingen

5
0
4
0
3
0
2
0
1
0

Waarom studenten kiezen voor Stuvia

Gemaakt door medestudenten, geverifieerd door reviews

Kwaliteit die je kunt vertrouwen: geschreven door studenten die slaagden en beoordeeld door anderen die dit document gebruikten.

Niet tevreden? Kies een ander document

Geen zorgen! Je kunt voor hetzelfde geld direct een ander document kiezen dat beter past bij wat je zoekt.

Betaal zoals je wilt, start meteen met leren

Geen abonnement, geen verplichtingen. Betaal zoals je gewend bent via Bancontact, iDeal of creditcard en download je PDF-document meteen.

Student with book image

“Gekocht, gedownload en geslaagd. Zo eenvoudig kan het zijn.”

Alisha Student

Veelgestelde vragen