-1

This is supposed to be a prime generator. I try running this but it doesn't print out the integers. The program will also end right away. I don't know if its running

def prime_gen():

    start = 1

    while start:

        if start % start == 0:
            print(start)

            start += 1
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
  • this is python? – Suraj Rao Jun 05 '17 at 06:46
  • Yes, I forgot to say that sorry D: –  Jun 05 '17 at 06:47
  • make sure you set the right tags .. – Suraj Rao Jun 05 '17 at 06:47
  • okay I will next time –  Jun 05 '17 at 06:48
  • You did not call the function. And **spoiler alert** use Ctrl + C to end the program's endless loop. – Klaus D. Jun 05 '17 at 06:54
  • https://stackoverflow.com/questions/15706911/generator-in-python-generating-prime-numbers – Rolf of Saxony Jun 05 '17 at 07:06
  • This is way harder than I expected. To be honest here I've hit my first wall and it's been frustration for a couple of days. How do you develop yourself properly to understand how to make your own stuff on the fly? Is it always a struggle? Is this the kind of thing you literally have to take one tiny baby step at a time? I am currently learning in treehouse right now and I was learning the basics the I get clubbed with simple projects like shopping lists and number guessing games. If any of you have any advice to grow I would be a very happy camper! –  Jun 05 '17 at 07:19
  • @RussellAlson Read. Read code. Take the [tour]. Read [ask] thoroughly, particularly how to create a [mcve]. – Peter Wood Jun 05 '17 at 07:51
  • Like anything, programming is tough at first. You've never done anything like it. But it does get easier, one step at a time; always try coding a minimal example that works, and keep on going from there, to the next minimal step that works, which means adding features / optimizing / taking on a new, slightly more challenging project. Also, trying to learn algorithms while also learning how to code is a difficult task. pick one. In my opinion, you should either go through the algorithm in English on paper, or implement an existing algorithm, just to get the hang of it. – nadavvadan Jun 05 '17 at 07:54

3 Answers3

4

First what is a prime? A number that have no divisors but itself. The easy way is to load that numbers, starting by two and check if that number can't be divided by all the previous ones, if its so then you got a new one; you can make it a generator by using yield instead of return

def prime_gen():
    primes = [2]
    yield 2
    n = 3
    while True:
        for p in primes:
            if n % p == 0:
                n += 1
                break
        else:
            primes.append(n)
            yield n

Example:

pg = prime_gen()
next(pg)
2
next(pg)
3
next(pg)
5
next(pg)
7
next(pg)
11
Netwave
  • 40,134
  • 6
  • 50
  • 93
2

You have 2 main issues here:

  1. Your algorithm doesn't calculate primes - it runs infinitely (while start), and prints a number when number % number == 0 (aka number modulo number) which is always true. So this is an infinite loop printing the numbers from 1 up.

  2. you never call prime_gen() which is why the program doesn't do anything. You have created a function but did not call it.

for a nice way of calculating primes, take a look at Sieve of Eratosthenes (Wikipedia).

nadavvadan
  • 3,930
  • 1
  • 17
  • 29
1

You can do something like:

def prime_gen(limit):
    current = 2 # 1 is not exactly prime
    primes = []
    while current <= limit:
        isPrime = True
        for p in primes:
            if current % p == 0:
                isPrime = False
                break
        if isPrime:
            primes.append(current)
            print current, ", ",
        current += 1
    return primes
Shai
  • 111,146
  • 38
  • 238
  • 371