#!/usr/bin/env python3 from lib import primegen from fractions import Fraction def is_prime(n): return n in primes def Pprob(n): if is_prime(n): return Fraction(2, 3) return Fraction(1, 3) def croak(n): return {'P' : Pprob(n), 'N' : 1-Pprob(n)} def jump(sqrs, let): r = [0] * len(sqrs) for i in range(1, len(sqrs)-1): prob = croak(i+1)[let] * \ Fraction(1, 2) * sqrs[i] r[i+1] += prob r[i-1] += prob r[1] += sqrs[0] * croak(1)[let] r[-2] += sqrs[-1] * croak(len(sqrs))[let] return r def start(n): return [Fraction(1, n)] * n seq = "PPPPNNPPPNPPNPN" N = 500 primes = set(primegen(N)) sqrs = start(N) for letter in seq: sqrs = jump(sqrs, letter) print(sum(sqrs))