简体   繁体   中英

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

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 !

您可以打印从 1 到第一个素数的所有数字,然后从第一个数字到下一个数字,直到达到 n。

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.

I eventually came up with this answer which I believe should solve it but id there;sa 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

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) 
prime = int(input('Please enter the range: '))

prime_number = []

for num in range(prime):
    if num > 1:
        for i in range(2,num):
            if num % i == 0:
                break
        else:
            prime_number.append(num)
        
print(f'Prime numbers in range {prime} is {prime_number}')

all_number = []
for i in range(2,prime+1):
    all_number.append(i)



Non_prime_number = []

for element in all_number:
    if element not in prime_number:
        Non_prime_number.append(element)

print(f'Non Prime numbers in range {prime} is {Non_prime_number}')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM