Simple prime number generator in Python

前端 未结 26 1792
感情败类
感情败类 2020-11-22 07:16

Could someone please tell me what I\'m doing wrong with this code? It is just printing \'count\' anyway. I just want a very simple prime generator (nothing fancy).

26条回答
  •  鱼传尺愫
    2020-11-22 08:03

    If you wanted to find all the primes in a range you could do this:

    def is_prime(num):
    """Returns True if the number is prime
    else False."""
    if num == 0 or num == 1:
        return False
    for x in range(2, num):
        if num % x == 0:
            return False
    else:
        return True
    num = 0
    itr = 0
    tot = ''
    while itr <= 100:
        itr = itr + 1
        num = num + 1
        if is_prime(num) == True:
            print(num)
            tot = tot + ' ' + str(num)
    print(tot)
    

    Just add while its <= and your number for the range.
    OUTPUT:
    2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101

提交回复
热议问题