Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Python code for checking if a number is a prime number

for i in range(2, 20):
    for x in range(2, i):
        if i % x == 0:
            break
    else:
        print(i, "is a prime number")
Comment

how to see if a number is prime in python

def is_prime(n: int) -> bool:
    """Primality test using 6k+-1 optimization."""
    if n <= 3:
        return n > 1
    if n % 2 == 0 or n % 3 == 0:
        return False
    i = 5
    while i ** 2 <= n:
        if n % i == 0 or n % (i + 2) == 0:
            return False
        i += 6
    return True
Comment

check for prime in python

def is_prime(n: int) -> bool:
    """Primality test using 6k+-1 optimization."""
    import math
    if n <= 3:
        return n > 1
    if n % 2 == 0 or n % 3 == 0:
        return False
    i = 5
    while i <= math.sqrt(n):
        if n % i == 0 or n % (i + 2) == 0:
            return False
        i += 6
    return True
Comment

check if number is prime python

def check_if_prime():
    number = int(input("Enter number: "))
    
    prime_lists = [1,2,3]
    
    divisible_by = []
    if number in prime_lists:
        return divisible_by
    
    if number==0:
        return None
    
    for i in range(2,number):
        
        if number%i==0:
            divisible_by.append(i)
            
    return divisible_by
        
    
check_if_prime()
Comment

PREVIOUS NEXT
Code Example
Python :: remove from list if not maches in list 
Python :: pass arguments with apply 
Python :: jupyter today date 
Python :: selenium do not open browser window 
Python :: global variable python 
Python :: python discord bot 
Python :: python sort algorithm 
Python :: format in python 
Python :: text from xml doc in python 
Python :: merge two netcdf files using xarray 
Python :: python synonym library 
Python :: create an array with a range of elements 
Python :: how to python 
Python :: python string formatting 
Python :: pickling python example 
Python :: TypeError: Direct assignment to the forward side of a many-to-many set is prohibited. Use .set() instead 
Python :: np.arrange 
Python :: all python functions 
Python :: python dictionary comprehensions 
Python :: take absolute value in python 
Python :: add a tuple to a dictionary python 
Python :: all possible combinations in python 
Python :: re date python 
Python :: how to handle missing values in dataset 
Python :: how to install ffmpeg_streaming in python 
Python :: python merge dict 
Python :: anagrams string python 
Python :: excel write column 
Python :: find highest value in array python 
Python :: sphinx autodoc command 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =