Anonymous user
One-dimensional cellular automata: Difference between revisions
One-dimensional cellular automata (view source)
Revision as of 16:29, 10 October 2008
, 15 years ago→{{header|Python}}: Boolean version
(added Haskell) |
(→{{header|Python}}: Boolean version) |
||
Line 240:
Generation 7: __##_____#__________
Generation 8: __##________________
Generation 9: __##________________</pre>The following implementation uses boolean operations to realize the function.
<python>import random
maxgenerations = 10
#a = int('01110110101010100100', 2)
a = random.getrandbits(20)
tr = ('____', '___#', '__#_', '__##', '_#__', '_#_#', '_##_', '_###',
'#___', '#__#', '#_#_', '#_##', '##__', '##_#', '###_', '####' )
for i in range(maxgenerations):
print "Generation %3i: %s" % (i,(''.join(tr[int(t,16)] for t in ('%05x'%a))))
a = (a & ((a<<1) | (a>>1))) ^ ((a<<1) & (a>>1))</python>
|