Jun-20-2019, 10:18 AM
I'm working on a code challenge in which we have to return the k non-prime +ve numbers.
The code comes with a locked generator code which is printing the all numbers also.
Here's the complete code:
The code comes with a locked generator code which is printing the all numbers also.
Here's the complete code:
from math import sqrt def manipulate_generator(generator, n): def is_prime(num): for i in range(2,int(sqrt(num))+1): if num % i == 0: return False return True def yield_np(n): np_counter = 1 num_to_print = 1 while np_counter <= n: if num_to_print == 1 or is_prime(num_to_print) == False: yield num_to_print np_counter+=1 num_to_print+=1 if n > 0: for i in yield_np(n): yield i else: next(generator) def positive_integers_generator(): n = 1 while True: x = yield n if x is not None: n = x else: n += 1 k = int(input()) g = positive_integers_generator() for _ in range(k): n = next(g) print(n) manipulate_generator(g, n)For example, if I pass: 12 it's output should be:
1 4 6 8 9 10 12 14 15 16 18 20but my output is:
1 2 3 4 5 6 7 8 9 10 11 12