0

I wrote ths program that is supposed to print out the prime numbers between 100 and 200. (I added some extra print commands along the way so I could see where it was getting to.) Can anyone give me a hint as to why it stops at 137, while the script is checking this number?

Thanks!

Here is the script:

import math
import sys
index=100.0
checker=2.0

def start():
    print("A")
    global index
    while index<=200.0:
        fu1()
    if index>200.0:
        end()

def fu1():
    print ("B")
    print ("checker", checker)
    print ("index", index)
    global index
    global checker
    varb=index/checker
    varb2=round(index/checker, 0)
    if checker>=index:
        print ("C")
        print(index)
        index=index+1.0
        checker=2.0
        start()
    if varb2==varb:
        print("D")
        checker=2.0
        index=index+1.0
        start()
    checker=checker+1.0
    fu1()


def end():
    print("E")
    print("ENDED")
    sys.exit()

start()
Cannoliopsida
  • 2,779
  • 4
  • 29
  • 61
  • Maybe this one: http://stackoverflow.com/questions/3289430/python-recursion – PeterMmm Jun 16 '12 at 18:42
  • @DavidSchwartz - recursion? ;) Although somewhat odd-looking recursion, I'll admit. – weronika Jun 16 '12 at 18:59
  • "Why does fu1 call itself? – David Schwartz " --It calls itself to begin that same process again, but with the next higher "index". The program actually works, but is there a different way it should be done? – user1460981 Jun 16 '12 at 19:09
  • You should use `while` loops for code that will be run an indeterminate number of times. In this case, you have a fixed range to iterate over, so use a `for` loop instead. – Joel Cornett Jun 16 '12 at 19:33
  • @Peter - Thanks, I checked into the incursion limits, changed it to 5000 and it works up to 200, but it still crashes if I change it to check for primes higher than 1,000,000, which is the original goal. Instead of this recursion with "while" can you suggest another type? Obviously, I am just getting the hang of this, and your hint would be very helpful. – user1460981 Jun 16 '12 at 19:33
  • This may be useful: http://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-python – Daenyth Jun 16 '12 at 19:50

1 Answers1

2

your program has reached a limit in the number of recursive calls (default to 1000 in python, I think)... you can increase it by using this command sys.setrecursionlimit(5000) for instance.

dcasas
  • 21
  • 3
  • Great! Thanks so much. I did that and it worked. But could you tell me one more thing please. After it finishes printing all the primes up to 200 and then prints "End" it prints a whole lot of red messages like this: File " File "C:\Users\Marcela\Documents\HH.txt", line 27, in fu1 start() File "C:\Users\Marcela\Documents\HH.txt", line 12, in start end() File "C:\Users\Marcela\Documents\HH.txt", line 34, in end sys.exit() SystemExit Could you tell me now to avoid this? – user1460981 Jun 16 '12 at 19:02
  • Thanks again -- it works, but not for checking larger numbers. It seems (by Weronika's comment) that I have created problem recalling Fu1 with "While" -- if you have a suggestion about how to make this process recur in the correct way, please let me know. – user1460981 Jun 16 '12 at 19:32