[英]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.我有一个 hackkerank 编码挑战来打印前 n 个非素数,我有工作代码,但问题是他们有一个锁定的代码,它打印从 1 到 n 的数字以及 output,为了通过我需要的测试只打印非素数而不是 1...n 数字。 I cant comment the printing part of 1...n as it is blocked.我无法评论 1...n 的打印部分,因为它被阻止了。 please let me know the idea to print only 1st n non prime numbers:请让我知道只打印第一个 n 个非素数的想法:
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:关键是 for _ in range(k): 已经打印出数字,其中包括我不想打印出来的数字:这是我想要的 output 的类型:对于 n=10 我希望它打印出来: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.我不确定您的hackerrank情况,但是可以通过这种方式有效地打印前N个非质数。
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.还要记住,您只能检查数字的 sqrt 以确定它是否是素数。
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这个链接python 生成器帮助我理解了 python 生成器
I just solved that right now.我现在刚刚解决了这个问题。 Like Swapnil Godse said you need to deal with all special cases to optimize computations.就像 Swapnil Godse 所说,您需要处理所有特殊情况以优化计算。 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}')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.