Thursday, July 16, 2015

Euler Problem 220 Heighway Dragon or Dragon Curve

The subject of this problem is Heighway Dragon~!
https://projecteuler.net/problem=220

Let D0 be the two-letter string "Fa". For n≥1, derive Dn from Dn-1 by the string-rewriting rules:
"a" → "aRbFR"
"b" → "LFaLb"
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.


Approaches

1. make this way using by phyton and turtle.
2. think more deeply to solve it.
3. I have to find pattern or math skill.

this is my phython coding.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
__author__ = 'ddavid'


import turtle

a = 'aRbFR'
b = 'LFaLb'

s = 'Fa'

wn = turtle.Screen()      # Creates a playground for turtles
tt = turtle.Turtle()    # Create a turtle, assign to alex
tt.speed(0)
tt.left(90)
tt.left(90)

for i in range(14):
    s = s.replace('a', 't')
    s = s.replace('b', 'p')
    s = s.replace('t', a)
    s = s.replace('p', b)

    print(s)
    print('\n')

    if i == 9:
        for d in range(len(s)):
            print(s[d-1])
            if s[d-1] == 'F':

                tt.forward(2)
            elif s[d-1] == 'L':
                tt.left(90)
            elif s[d-1] == 'R':
                tt.right(90)

wn.mainloop()             # Wait for user to close window


if you run this code
you will see this result.

Have Fun~ enyoing euler problem site.

reference
https://en.wikipedia.org/wiki/Dragon_curve


No comments:

Post a Comment