Stop at 1; otherwise if odd then load 3*x+1; or if even then divide by two
()
| 59 | import sys |
| 60 | |
| 61 | def worker(): |
| 62 | 'Stop at 1; otherwise if odd then load 3*x+1; or if even then divide by two' |
| 63 | name = threading.currentThread().getName() |
| 64 | while True: |
| 65 | x = q.get() |
| 66 | sys.stdout.write('%s\t%d\n' % (name, x)) |
| 67 | if x <= 1: |
| 68 | sys.stdout.write('!\n') |
| 69 | elif x % 2 == 1: |
| 70 | q.put(x * 3 + 1) |
| 71 | else: |
| 72 | q.put(x // 2) |
| 73 | q.task_done() |
| 74 | |
| 75 | q = TaskQueue() |
| 76 | numworkers = 4 |