So I recreated the sieve of eratosthenes but the result (prime numbers) is not output as i expected.
The first function is the sieve which returns a dictionary with all integers up to a given range as keys and True or False as their values (prime or not). The generator afterwards is supposed to get all the keys with a value of True (prime numbers). I think the problem is that I haven't used the generator correctly (I just learnt about generators), but I can't spot the mistake.
from math import sqrt
def sieve(x):
values = {}
for l in range(2, x + 1):
values[l] = True
for k in range(2, int(sqrt(x)) + 1):
if values[k] == True:
for j in range(k**2, x + 1, k):
values[j] = False
return values
def primes(dict1):
for l in dict1:
if l == True:
yield list(dict1.keys())[l]
for k in primes(sieve(int(input()))):
print(k, end = " ")
I expect all prime numbers (keys in the dictionary with a value of True) to be printed. But there is no output whatsoever.