Ejemplo n.º 1
0
def num_nondiv_binom(N, b):
    '''Number of binomial coefficients in rows 0-(N-1) not divisible by b. b must be prime.''' 
    a = np.array(base_digits(N, b), dtype=np.long)
    return polyval(np.array(map(triangle, a)) * np.flipud(np.cumprod(np.concatenate(([1], np.flipud(a[1:] + 1))))), triangle(b))
Ejemplo n.º 2
0
Thus, D0 = "Fa", D1 = "FaRbFR", D2 = "FaRbFRRLFaLbFR", and so on.

These strings can be interpreted as instructions to a computer graphics program, with "F" meaning "draw forward one unit", "L" meaning "turn left 90 degrees", "R" meaning "turn right 90 degrees", and "a" and "b" being ignored. The initial position of the computer cursor is (0,0), pointing up towards (0,1).

Then Dn is an exotic drawing known as the Heighway Dragon of order n. For example, D10 is shown below; counting each "F" as one step, the highlighted spot at (18,16) is the position reached after 500 steps.


What is the position of the cursor after 1012 steps in D50 ?
Give your answer in the form x,y with no spaces.
============================================================
'''
from itertools import islice
from math import log
from problem036 import base_digits

base_str = lambda n, b: ''.join(str(x) for x in reversed(base_digits(n, b)))
largest_m_le = lambda N, k : int(log(N / k) / log(2))

def dragon_curve((x, y)=(0, 0), (a, b)=(0, 1)):
    '''Yield the coordinates, directions and step numbers of the dragon curve after each step,
    starting at (x,y) with direction (a,b).'''
    n = 0
    yield (x, y), (a, b), n
    while True:
        x += a; y += b
        n += 1
        s = (((n & -n) << 1) & n) != 0
        a, b = (-b if s else b), (a if s else -a)
        yield (x, y), (a, b), n

def x_coef(k):