Python Forum

Full Version: first k non prime numbers print
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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

1
[1]
[1]
2
[1, 4]
[1, 4]
3
[1, 4, 6]
[1, 4, 6]
4
[1, 4, 6, 8]
5
[1, 4, 6, 8, 9]
6
[1, 4, 6, 8, 9, 10]
[1, 4, 6, 8, 9, 10]
7
[1, 4, 6, 8, 9, 10, 12]
[1, 4, 6, 8, 9, 10, 12]
8
[1, 4, 6, 8, 9, 10, 12, 14]
9
[1, 4, 6, 8, 9, 10, 12, 14, 15]
10
[1, 4, 6, 8, 9, 10, 12, 14, 15, 16]
[1, 4, 6, 8, 9, 10, 12, 14, 15, 16]
11
[1, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18]
[1, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18]
12
[1, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20] my output

expected output
1
4
6
8
9
10
12
14
15
16
18
20
We can't diagnose the problem from just the output. You need to show us your code. Please use python and output tags when posting code and results. Here are instructions on how to use them.
def manipulate_generator(generator, n):
  	# Enter your code here
    str1=[1]
    for num in range(2,100):
     if num > 1:
        for i in range(2,num):
            if (num % i) == 0:
              str1.append(num)
              break
        if(len(str1)==n):
         # for x in str1:
            print(str1)
        
    else:
       None  

  
    pass
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)
Here def manipulate_generator(generator, n) function we need to write others are locked.
You're looking at this the wrong way. Your goal is not to print the non-prime numbers. The printing is being done by the rest of the code. You need to trick the rest of the code into skipping the prime numbers. The generator is the first parameter to your function. If the generator is going to produce a prime number next, you need to call next(generator) to get it to skip that prime number.
Thanks for the kindly help
Hi @bsrohith, how you have solved the issue? please!

I have stuck on the same point, can you help me, please!
If you are having problems, please post your own thread with your code in Python tags, and clearly explain the problem you are having, including the full text of any errors.
I have posted a new thread and explain my problem, take a look at: https://python-forum.io/Thread-first-k-n...me-numbers
please!