I am trying to calculate the total number of convergent hailstone sequences. In the code below I am determining whether a sequence is convergent or divergent for different values of a, b, and x and outputting a list (seems to be a list of lists with one element in each sublist). My issue here is I can't seem to find the TOTAL number of convergent sequences. I would like to add all the 1's in my list and output the final value to obtain this answer. Can anybody please help?
P.S. In the output below, 1 = convergent 0 = divergent.
Here's my code:
def hailstone(a, b, x):
list = []
c = 0
count = []
for i in range(1, 100):
if x%2 == 0:
x = x/2
else:
x = a * x + b
if x not in list:
list.append(x)
else:
break
if len(list) < 99:
c = 1 + c
count.append(c)
else: pass
return(count)
def run():
for a in range(1, 4):
for b in range(1, 4):
for x in range(1, 4):
print(hailstone(a,b,x))
run()
Here's the output
[1]
[1]
[1]
[]
[]
[]
[1]
[1]
[1]
[]
[]
[]
[1]
[1]
[1]
[]
[]
[]
[1]
[1]
[1]
[]
[]
[]
[1]
[1]
[1]
Also, here's the data type for my list count:
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
Aucun commentaire:
Enregistrer un commentaire