diff --git a/Prime/code.py b/Prime/code.py index eb6771d..4cc5638 100644 --- a/Prime/code.py +++ b/Prime/code.py @@ -1,46 +1,40 @@ def SieveOfEratosthenes(range_to): - # creating a boolean array first - prime = [True for i in range(range_to + 1)] - p = 2 - while (p * p <= range_to): - - # If prime[p] is not changed, then it is a prime - if (prime[p] == True): - - # Update all multiples of p - for i in range(p * 2, range_to + 1, p): - prime[i] = False - p += 1 - print([p for p in range(2, range_to) if prime[p]]) + # creating a boolean array first + prime = [True for i in range(range_to + 1)] + p = 2 + while (p * p <= range_to): -def prime(range_from, range_to): - - allList = [x for x in range(2, range_to + 1)] + # If prime[p] is not changed, then it is a prime + if (prime[p] == True): + + # Update all multiples of p + for i in range(p * 2, range_to + 1, p): + prime[i] = False + + p += 1 - ptr = 0 - while True: - i = 2 - while i * allList[ptr] <= range_to: - if i*allList[ptr] in allList: - allList.remove(i*allList[ptr]) - i += 1 - ptr += 1 - if allList[ptr] ** 2 > range_to: - break + print([p for p in range(2, range_to) if prime[p]]) - primeList = [x for x in allList if x > range_from] - print("Prime List Between ", range_from, " and ", range_to, " is") - print(primeList) +def is_prime(num): + for i in range(2, int(num / 2) + 1): + if num % i == 0: + return False + return True + +def prime(range_from, range_to): + allList = [num for num in range(range_from, range_to + 1) if is_prime(num)] + + print("Prime List Between", range_from, "and", range_to, "is") + print(allList) if __name__ == "__main__": - print("Enter the range of number") - range_from = int(input("From: ")) - range_to = int(input("To: ")) - - prime(range_from, range_to) - SieveOfEratosthenes(range_to) + print("Enter the range of number") + range_from = int(input("From: ")) + range_to = int(input("To: ")) + prime(range_from, range_to) + SieveOfEratosthenes(range_to)