[Edu-sig] Another idea of Conway
Gregor Lingl
gregor.lingl at aon.at
Sun Sep 13 14:08:07 CEST 2009
Although my posting to this list seems to tend to become
a somewhat autistic activity I'd like to reveal the 'mystery'
behind the script below:
http://esolangs.org/wiki/Fractran
http://en.wikipedia.org/wiki/FRACTRAN
Would writing a fractran interpreter in python be an interesting
project for teaching CS? (My first try below allows for a lot of
optimizations, e.g. not to use Fraction but resort to (long) ints.)
Best wishes,
Gregor
Gregor Lingl schrieb:
> Hi all,
>
> on vacation in the Tyrolean Alps one evening
> I've found the time to implement another (I assume
> less well known) idea of John Conway.
>
> Just for fun.
>
> from fractions import Fraction
>
> fracs = [Fraction(f) for f in
> "17/91 78/85 19/51 23/38 29/33 77/29 95/23 77/19 1/17 11/13 13/11
> 15/14 15/2 55/1".split()]
>
> def fracgame():
> z = Fraction(2,1)
> while True:
> for f in fracs:
> n = z * f
> if n.denominator == 1:
> break
> yield int(n)
> z = n
>
> def pow2(z):
> n = 0
> while z % 2 == 0:
> n += 1
> z //= 2
> return (z == 1) * n
> def what():
> fg = fracgame()
> while True:
> z = next(fg)
> n = pow2(z)
> if n != 0:
> yield n
>
> what = what()
> print(next(what))
> print(next(what))
>
> # the following will take 1 or 2 minutes
>
> ##w = 0 ##while w < 100:
> ## w = next(what)
> ## print(w)
>
> Comments or discussion may follow when
> I'm back to Vienna.
>
> All the best,
> Gregor
>
> _______________________________________________
> Edu-sig mailing list
> Edu-sig at python.org
> http://mail.python.org/mailman/listinfo/edu-sig
>
More information about the Edu-sig
mailing list