Home > Python >Python Exercise: Prime Number List

Prime Number List

Implement a function named getprimes(end). This function will return a list of prime numbers that is less than or equal to the end number passed in as a parameter. Try to make your function be efficient.

You need login to comment

Solution submitted by Dzeng at March 7, 2020, 11:02 p.m.
num = int(input("Enter the number you would like the list to go up to.")) data_list = [i for i in range(2, num +1)] primes = [] while len(data_list) > 0: primes.append(data_list[0]) for l in data_list: if l % primes[len(primes)-1] == 0: data_list.remove(l) print(primes)
Solution submitted by Andrew at May 26, 2020, 12:42 a.m.
def isprime(input): for i in range(2, input): if input % i == 0: return(False) return(True) def getprimes(number): for i in range(1, number): if isprime(i): print(i) getprimes(1000000)

You need login to submit your solution