Пример #1
0
 def __init__(self, MAX_N):
     self.F = list(islice(fibonacci(), largest_fib_ind_le(MAX_N) + 1))
     self._z = {}
Пример #2
0
How many 6-input binary truth tables, tau, satisfy the formula

tau(a, b, c, d, e, f) AND tau(b, c, d, e, f, a XOR (b AND c)) = 0

for all 6-bit inputs (a, b, c, d, e, f)?
============================================================
'''
import networkx as nx
from numpy import prod
from itertools import islice
#import matplotlib.pylab as P
from problem002 import fibonacci

'''Cache enough Fibonacci numbers.'''
MAX_RING_SIZE = 50
F = list(islice(fibonacci(1, 2), MAX_RING_SIZE + 1))

'''Check if a connected graph is a ring.'''
is_ring = lambda g: g.number_of_nodes() >= 3 and all(g.degree(x) == 2 for x in g.nodes_iter())

def is_chain(g):
    '''Check if a connected graph is a chain.'''
    if g.number_of_nodes() == 1: return True  # Lone node is a chain
    degree1_count = 0
    for d in g.degree().itervalues():
        if d > 2: return False
        if d == 1:
            degree1_count += 1
            if degree1_count > 2: return False
    return degree1_count == 2
Пример #3
0
def digits(a, b, n_max):
    '''Return the digit D_{a,b}(n). Assuming the digit sequences a and b are of equal length.'''
    # Cache all relevant Fibonacci word lengths, which are proportional to the Fibonacci numbers
    s = len(a)
    l = map(lambda x: s * x, islice(fibonacci(), k(n_max, s) + 1))
    for n in xrange(n_max): yield d(a, b, l, k(n, s), (127 + 19 * n) * 7 ** n)
Пример #4
0
We'll consider the three-heap normal-play version of Nim, which works as follows:
- At the start of the game there are three heaps of stones.
- On his turn the player removes any positive number of stones from any single heap.
- The first player unable to move (because no stones remain) loses.

If (n1,n2,n3) indicates a Nim position consisting of heaps of size n1, n2 and n3 then there is a simple function X(n1,n2,n3) that you may look up or attempt to deduce for yourself that returns:

zero if, with perfect strategy, the player about to move will eventually lose; or
non-zero if, with perfect strategy, the player about to move will eventually win.
For example X(1,2,3) = 0 because, no matter what the current player does, his opponent can respond with a move that leaves two heaps of equal size, at which point every move by the current player can be mirrored by his opponent until no stones remain; so the current player loses. To illustrate:
- current player moves to (1,2,1)
- opponent moves to (1,0,1)
- current player moves to (0,0,1)
- opponent moves to (0,0,0), and so wins.

For how many positive integers n?=?230 does X(n,2n,3n) = 0 ?
============================================================
'''
from problem002 import fibonacci
from itertools import islice

num_nims = lambda n: islice(fibonacci(), n + 1, n + 2).next()
if __name__ == "__main__":
    # Testing that lead to a pattern: no adjacent 1's in n. Fibonacci sequence.
    for x in filter(lambda x: x ^ (2 * x) == 3 * x, xrange(1, 9)): print x, bin(x)
    for k in xrange(1, 10): 
        print k, 2 ** k, len(filter(lambda x: x ^ (2 * x) == 3 * x, xrange(1, 2 ** k + 1))), num_nims(k)
    
    print num_nims(30)