Last active
June 20, 2018 16:42
-
-
Save iswarup/e35a9e60567198de56ec5a6dc9a3f19b to your computer and use it in GitHub Desktop.
Prime number. Using Functions. Using LIST Comprehension.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
####################### | |
# Using Functions: | |
####################### | |
def get_number(prompt): | |
return int(input(prompt)) | |
def is_prime(number): | |
if number == 1: | |
prime = False | |
elif number == 2: | |
prime = True | |
else: | |
prime = True | |
for i in range(2,number): | |
if number % i == 0: | |
prime = False | |
break | |
return prime | |
def print_prime(number): | |
prime = is_prime(number) | |
if prime: | |
desc = "" | |
else: | |
desc = "not" | |
print(number,"is",desc,"a prime number!") | |
while 1==1: | |
print_prime(get_number("Enter a number: ")) | |
########################################### | |
# Using List Comprehension: | |
########################################### | |
''' This solution doesn’t use functions, but does use list comprehensions, which are always fun. | |
Thanks to Carlos for this solution. | |
The interesting thing here is the observation that when you want to check if a number is prime, | |
all you need to do is check the numbers from 2 to the square root of the number. | |
This is because the pair of numbers that are both the largest factors of the number are square root of x and square root of x. | |
Otherwise, the number you are checking for can be found by finding the corresponding factor and checking it. | |
''' | |
######## NOT a bit can I understand here. ########## | |
# Assumes that "a" contains an integer > 2 whose primality needs to be verified | |
# The algorithm builds a list of factors including the number 2 and all odd numbers | |
# up to the square root of "a", and then checks if any of those numbers divides "a" | |
# without a remainder - if so then "a" is not prime, else it is | |
if sum([ True if a%factor == 0 else False for factor in ( [2] + list(range(3,int(math.sqrt(a)),2) )) ]): | |
print("Number is composite") | |
else: | |
print("Number is prime") | |
################################################# | |
# Using List Comprehension (simpler one) | |
################################################# | |
num = int(input("Enter a number: ")) | |
a =[x for x in range(2,num) if num % x == 0] | |
def is_prime(n): | |
if num > 1: | |
if len(a) == 0: | |
print("Prime") | |
else: | |
print("Not Prime.") | |
else: | |
print("Not prime.") | |
is_prime(num) | |
a =int(input("Enter first:")) | |
b =int(input("Enter second:")) | |
for number in range(a,b+1): | |
for num in range(2,a): | |
if number % num == 0: | |
break | |
else: | |
print(number, end=" ") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment