python - I am trying to run a Collatz conjecture sequence to see if it always ends at 1, but my number is stuck at 999. Why is this? -


here code:

def iseven(number):     return number % 2 == 0  def run(x):     z = x     while z != 1:         if iseven(z) == true:             z = z/2             print z         else:             z = (3*z)+1             print z     else:         print 'got one!'         #to see if collatz indeed work     x+=1 

it works until 999, @ continues indefinitely, print got one! 999, raising segmentation fault: 22. how fix this?

use sys.setrecursionlimit stop 999 repeating forever. here edited version of code. put sys.argv[] well, fun because imported sys. sys.argv[] gets argument after run program this: python myscript.py 13.5 in this, sys.argv[1] 13.5. set recursion limit pretty high, downfall of segmentation fault: 22 still occurs, , haven't found way rid of that.

import time import sys while true:     try:         var = int(sys.argv[1])         break     except indexerror:         print 'you have enter number!'         var = int(raw_input('enter number: '))         break  def check(x):     z = x     try:         while z != 1:             if iseven(z) == true:                 z = z/2                 print z             else:                 z = (3*z)+1                 print z           else:             print 'got one!'             x = x+1             print 'trying %d...' %(x)             check(x)     except:         print 'you @ number %d' %(x)         check(x)  def iseven(number):         return number % 2 == 0  sys.setrecursionlimit(10000000) check(var) 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -