'Python print non-prime numbers

I have a hackkerank coding challenge to print first n non prime numbers, i have the working code but the problem is that they have a locked code which prints numbers from 1 to n along with the output, in order to pass the test i need to print only the non prime numbers not 1...n numbers along with it. I cant comment the printing part of 1...n as it is blocked. please let me know the idea to print only 1st n non prime numbers:

here is my solution:

def manipulate_generator(generator, n):
    if n>1:
    ls=[1]
    for elm in generator:
        if elm>3 and len(ls)<n:
            for k in range(2,elm):
                if elm%k==0 and elm not in ls:
                    ls.append(elm)
                    print(elm)
                    if len(ls)==n:
                        return ls

That's the code I added but here is the code that's locked on which I have to write the code above to make it print the number one at a time

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)

the point is the for _ in range(k): already prints out number which includes numbers I don't want printed out: This is the desired kind of output I want:for n=10 I want it to print out: output:

1
4
6
8
9
10
12
14
15
16

I can't change this code but the one above is what I wrote and can be changed... Pleae help me out... Thanks in anticipation



Solution 1:[1]

Why not to throw away the numbers which we don't need? Look at this solution which I implemented...

def is_prime(n):
    for i in range(2, n):
        if n%i == 0:
            return False
    return True

def manipulate_generator(generator, n):
    if is_prime(n+1):
        next(generator)
        manipulate_generator(generator, n+1)

Note: I understand that the logic can be improved to make it more efficient. But, its the idea of skipping unnecessary number printing which is important here !

Solution 2:[2]

You can print all the numbers from 1 up to the first prime number and then from that first number to the next one until you reach n.

Solution 3:[3]

I'm not sure of your hackerrank situation, but printing the first N non-prime numbers efficiently can be done this way.

def non_prime_numbers_till_n(n):
    primes = set()
    for num in range(2,number + 1):
        if num > 1:
           for i in range(2, math.sqrt(num)):
               if (num % i) == 0:
                  break
               else:
                  primes.add(num)
     result = []
     for i in range(1, n):
         if i not in primes:
            result.append(i)
     return result

Depending on what your online editor expects you can either print them, or store them in a list and return the list.

Also bear in mind, you can only check upto the sqrt of the number to determine if its a prime or not.

Solution 4:[4]

I eventually came up with this answer which I believe should solve it but id there;s a better way to solve it please add your answers:

def manipulate_generator(generator, n):
    for num in range(3,100000):
        for q in range(2,num):
            if num%q==0 and num>n:
                generator.send(num-1)
                return

this link python generator helped me to understand python generator

Solution 5:[5]

I just solved that right now. Like Swapnil Godse said you need to deal with all special cases to optimize computations. This link might be helpful: click here.

Here is the solution:

from math import sqrt

def is_prime(n):
    if (n <= 1):
        return False
    if (n == 2):
        return True
    if (n % 2 == 0):
        return False

    i = 3
    while i <= sqrt(n):
        if n % i == 0:
            return False
        i = i + 2

    return True

def manipulate_generator(g, n):
    if is_prime(n+1):
        next(g)
        manipulate_generator(g, n+1) 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Swapnil Godse
Solution 2 Irazza
Solution 3
Solution 4 omokehinde igbekoyi
Solution 5 Will