Python Forum
first k non prime numbers print
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
first k non prime numbers print
#1
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
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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.
Reply
#4
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
Thanks for the kindly help
Reply
#6
Hi @bsrohith, how you have solved the issue? please!

I have stuck on the same point, can you help me, please!
Reply
#7
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  prime numbers with iterator and generator cametan_001 8 1,854 Dec-17-2022, 02:41 PM
Last Post: cametan_001
  prime numbers astral_travel 28 3,626 Nov-08-2022, 09:23 PM
Last Post: astral_travel
  Print max numbers in a list jimmoriarty 1 2,147 Sep-25-2020, 07:29 AM
Last Post: DPaul
  Is there a way i print odd and even numbers separately? spalisetty06 5 2,704 Jul-21-2020, 06:48 PM
Last Post: spalisetty06
  Supposed to print out even numbers DallasPCMan 4 1,941 May-21-2020, 05:50 PM
Last Post: ndc85430
  Return prime numbers from range krzyfigh 2 1,919 Apr-20-2020, 08:08 PM
Last Post: krzyfigh
  Prime numbers Anderi02 1 1,964 Oct-13-2019, 04:49 PM
Last Post: ichabod801
  first k non-prime numbers arycloud 11 7,216 Jul-09-2019, 02:19 PM
Last Post: abhi19935
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 3,707 May-09-2019, 12:19 PM
Last Post: Pleiades
  Why can't I print numbers in Python? skrivver99 2 2,771 Nov-04-2018, 09:47 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020