Problem:
Koch Curve Write a program which reads an integer n and draws a Koch curve based on recursive calles of depth n . The Koch curve is well known as a kind of fractals . You can draw a Koch curve in the following algorithm:  Divide a given segment (p1, p2) into three equal segments.  Replace the middle segment by the two sides of an equilateral triangle (s, u, t) of the same length as the segment.  Repeat this procedure recursively for new segments (p1, s), (s, u), (u, t), (t, p2).    You should start (0, 0), (100, 0) as the first segment. Input An integer n is given. Output Print each point (x, y) of the Koch curve. Print a point in a line. You should start the point(0, 0), which is the endpoint of the first segment and end with the point (100, 0), the other endpoint so that you can draw the Koch curve as an unbroken line. Each solution should be given as a decimal with an arbitrary number of fractional digits, and with an absolute error of at most 10-4. Constraints  0 ≤ n ≤ 6  Sample Input 1 1 Sample Output 1 0.00000000 0.00000000
33.33333333 0.00000000
50.00000000 28.86751346
66.66666667 0.00000000
100.00000000 0.00000000 Sample Input 2 2 Sample Output 2 0.00000000 0.00000000
11.11111111 0.00000000
16.66666667 9.62250449
22.22222222 0.00000000
33.33333333 0.00000000
38.88888889 9.62250449
33.33333333 19.24500897
44.44444444 19.24500897
50.00000000 28.86751346
55.55555556 19.24500897
66.66666667 19.24500897
61.11111111 9.62250449
66.66666667 0.00000000
77.77777778 0.00000000
83.33333333 9.62250449
88.88888889 0.00000000
100.00000000 0.00000000 Notes

Solution (Python):
import math

def plot(x, y):
    print(f'{x:.8f} {y:.8f}')

def Koch(n, x1, y1, x2, y2):
    if n == 0:
        plot(x1, y1)
        return
    sx = (2 * x1 + x2) / 3
    sy = (2 * y1 + y2) / 3
    tx = (x1 + 2 * x2) / 3
    ty = (y1 + 2 * y2) / 3
    ux = (tx - sx) * (1/2) - (ty - sy) * (math.sqrt(3)/2) +sx
    uy = (tx - sx) * (math.sqrt(3)/2) + (ty - sy) * (1/2) +sy
    
    Koch(n-1, x1, y1, sx, sy) 
    Koch(n-1, sx, sy, ux, uy)
    Koch(n-1, ux, uy, tx, ty)
    Koch(n-1, tx, ty, x2, y2) 
    
    
    
    
    
    

n = int(input())
Koch(n, 0, 0, 100, 0)
plot(100, 0)

Solution (Python):
import math
cos = math.cos(math.radians(60))
sin = math.sin(math.radians(60))

def koch(count, p1x, p1y, p2x, p2y):
    global cos
    global sin
    if count == 0:
        return
    sx, sy = (2*p1x + p2x)/3, (2*p1y + p2y)/3
    tx, ty = (p1x + 2*p2x)/3, (p1y + 2*p2y)/3
    ux, uy = cos*(sx + tx) - sin*(ty - sy), sin*(tx - sx) + cos*(sy + ty)

    koch(count-1, p1x ,p1y, sx, sy)
    print(sx, sy)
    koch(count-1, sx, sy, ux, uy)
    print(ux, uy)
    koch(count-1, ux, uy, tx, ty)
    print(tx, ty)
    koch(count-1, tx, ty, p2x, p2y)

n = int(input())
print(0, 0)
koch(n, 0, 0, 100, 0)
print(100, 0)

Solution (Python):
import math

def Koch(Px, Py, Qx, Qy, n) :
    if n == 0 :
        return
    else :
        Ax = (2 * Px + Qx) / 3
        Ay = (2 * Py + Qy) / 3
        Bx = (Px + 2 * Qx) / 3
        By = (Py + 2 * Qy) / 3
        Cx = Ax + (Bx - Ax) * math.cos(math.pi/3) - (By - Ay) * math.sin(math.pi/3) 
        Cy = Ay + (Bx - Ax) * math.sin(math.pi/3) + (By - Ay) * math.cos(math.pi/3)
        
        Koch(Px, Py, Ax, Ay, n-1)
        print(Ax, Ay)
        Koch(Ax, Ay, Cx, Cy, n-1)
        print(Cx, Cy)
        Koch(Cx, Cy, Bx, By, n-1)
        print(Bx, By)
        Koch(Bx, By, Qx, Qy, n-1)

n = int(input())
print(0, 0.00000000)
Koch(0.00000000, 0.00000000, 100.00000000, 0.00000000, n)
print(100.00000000, 0.00000000)

Solution (Python):


import math

def plot(x,y):
    print(f'{x:.8f} {y:.8f}')
    
def Koch(n, x1,y1, x2,y2):
    if n==0:
        plot(x1,y1)
        return
    
    sx=(2*x1+x2)/3
    sy=(2*y1+y2)/3
    tx=(x1+2*x2)/3
    ty=(y1+2*y2)/3
    ux=(tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy=(tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    Koch(n-1, x1,y1, sx,sy)
    Koch(n-1, sx,sy, ux,uy)
    Koch(n-1, ux,uy, tx,ty)
    Koch(n-1, tx,ty, x2,y2)
n=int(input())
Koch(n, 0,0, 100,0)
plot(100,0)

Solution (Python):
import math

def Koch(Px, Py, Qx, Qy, n) :
    if n == 0 :
        return
    else :
        Ax = (2 * Px + Qx) / 3
        Ay = (2 * Py + Qy) / 3
        Bx = (Px + 2 * Qx) / 3
        By = (Py + 2 * Qy) / 3
        Cx = Ax + (Bx - Ax) * math.cos(math.pi/3) - (By - Ay) * math.sin(math.pi/3) 
        Cy = Ay + (Bx - Ax) * math.sin(math.pi/3) + (By - Ay) * math.cos(math.pi/3)
        
        Koch(Px, Py, Ax, Ay, n-1)
        print(Ax, Ay)
        Koch(Ax, Ay, Cx, Cy, n-1)
        print(Cx, Cy)
        Koch(Cx, Cy, Bx, By, n-1)
        print(Bx, By)
        Koch(Bx, By, Qx, Qy, n-1)

n = int(input())
print(0, 0.00000000)
Koch(0.00000000, 0.00000000, 100.00000000, 0.00000000, n)
print(100.00000000, 0.00000000)

Solution (Python):
import math
pi = math.pi

th = pi * 60.0 / 180.0
cos = math.cos(th)
sin = math.sin(th)

class Point():
    def __init__(self):
        self.x = 0.0
        self.y = 0.0
        
def koch(n, p1, p2):
    if n == 0:
        return None
    
    s = Point()
    t = Point()
    u = Point()
    
    s.x = (2.0 * p1.x + 1.0 * p2.x) / 3.0
    s.y = (2.0 * p1.y + 1.0 * p2.y) / 3.0
    t.x = (1.0 * p1.x + 2.0 * p2.x) / 3.0
    t.y = (1.0 * p1.y + 2.0 * p2.y) / 3.0
    u.x = (t.x - s.x) * cos - (t.y - s.y) * sin + s.x
    u.y = (t.x - s.x) * sin + (t.y - s.y) * cos + s.y
    
    koch(n-1, p1, s)
    print(s.x, s.y)
    koch(n-1, s, u)
    print(u.x, u.y)
    koch(n-1, u, t)
    print(t.x, t.y)
    koch(n-1, t, p2)
    
def main():
       
    p1 = Point()
    p2 = Point()
    
    n = int(input())
    
    p2.x = 100
    
    print(p1.x, p1.y)
    koch(n, p1, p2)
    print(p2.x, p2.y)
    
    return 0
    
if __name__ == '__main__':
    main()

Solution (Python):
import cmath

def koch(n, p1, p2):
  if not n:
    return None
  s = (2*p1 + p2) / 3
  t = (p1 + 2*p2) / 3
  u = s + cmath.rect(1, cmath.pi/3) * (t - s)
  koch(n-1, p1, s)
  print(s.real, s.imag)
  koch(n-1, s, u)
  print(u.real, u.imag)
  koch(n-1, u, t)
  print(t.real, t.imag)
  koch(n-1, t, p2)

p1, p2 = 0j, 100+0j
print(p1.real, p1.imag)    
koch(int(input()), p1, p2)
print(p2.real, p2.imag)

Solution (Python):
import cmath

def koch(n, p1, p2):
  if not n:
    return None
  s = (2*p1 + p2) / 3
  t = (p1 + 2*p2) / 3
  u = s + cmath.rect(1, cmath.pi/3) * (t - s)
  koch(n-1, p1, s)
  print(s.real, s.imag)
  koch(n-1, s, u)
  print(u.real, u.imag)
  koch(n-1, u, t)
  print(t.real, t.imag)
  koch(n-1, t, p2)

p1, p2 = 0j, 100+0j
print(p1.real, p1.imag)    
koch(int(input()), p1, p2)
print(p2.real, p2.imag)

Solution (Python):
  
import math

class Pos:
  def init(x, y):
    self.x = x
    self.y = y

def kock(n, p1, p2):
  if n == 0: return

  s, t = Pos(), Pos()
  s.x = (2 * p1.x + 1 * p2.x) / 3
  s.y = (2 * p1.y + 1 * p2.y) / 3
  t.x = (1 * p1.x + 2 * p2.x) / 3
  t.y = (1 * p1.y + 2 * p2.y) / 3

  cos60 = math.cos(math.radians(60))
  sin60 = math.sin(math.radians(60))

  u = Pos()
  u.x = (t.x - s.x) * cos60 - (t.y - s.y) * sin60 + s.x
  u.y = (t.x - s.x) * sin60 + (t.y - s.y) * cos60 + s.y

  kock(n-1, p1, s)
  print("{:.5f} {:.5f}".format(s.x, s.y))
  kock(n-1, s, u)
  print("{:.5f} {:.5f}".format(u.x, u.y))
  kock(n-1, u, t)
  print("{:.5f} {:.5f}".format(t.x, t.y))
  kock(n-1, t, p2)

n = int(input())

p1, p2 = Pos(), Pos()
p1.x, p1.y = 0.0, 0.0
p2.x, p2.y = 100.0, 0.0

print("{:.5f} {:.5f}".format(p1.x, p1.y))
kock(n, p1, p2)
print("{:.5f} {:.5f}".format(p2.x, p2.y))

Solution (Python):
import math

def Koch(Px, Py, Qx, Qy, n) :
    if n == 0 :
        return
    else :
        Ax = (2 * Px + Qx) / 3
        Ay = (2 * Py + Qy) / 3
        Bx = (Px + 2 * Qx) / 3
        By = (Py + 2 * Qy) / 3
        Cx = Ax + (Bx - Ax) * math.cos(math.pi/3) - (By - Ay) * math.sin(math.pi/3) 
        Cy = Ay + (Bx - Ax) * math.sin(math.pi/3) + (By - Ay) * math.cos(math.pi/3)
        
        Koch(Px, Py, Ax, Ay, n-1)
        print(Ax, Ay)
        Koch(Ax, Ay, Cx, Cy, n-1)
        print(Cx, Cy)
        Koch(Cx, Cy, Bx, By, n-1)
        print(Bx, By)
        Koch(Bx, By, Qx, Qy, n-1)

n = int(input())
print(0, 0.00000000)
Koch(0.00000000, 0.00000000, 100.00000000, 0.00000000, n)
print(100.00000000, 0.00000000)

Solution (Python):
import math

c60 = math.cos(math.radians(60))
s60 = math.sin(math.radians(60))

def koch(d, p1, p2):
    """ Print the coordinates of the vertices of Koch Curve.
    d is the depth of recursion.
    p1, p2 are coordinates of end point of the initial state.
    """
    if d == 0:
        return

    sx = (2 * p1[0] + p2[0]) / 3
    sy = (2 * p1[1] + p2[1]) / 3

    tx = (p1[0] + 2 * p2[0]) / 3
    ty = (p1[1] + 2 * p2[1]) / 3

    dx = tx - sx
    dy = ty - sy

    ux = dx * c60 - dy * s60 + sx
    uy = dx * s60 + dy * c60 + sy

    koch(d - 1, p1, (sx, sy))
    print("{0:.8f} {1:.8f}".format(sx, sy))

    koch(d - 1, (sx, sy), (ux, uy))
    print("{0:.8f} {1:.8f}".format(ux, uy))

    koch(d - 1, (ux, uy), (tx, ty))
    print("{0:.8f} {1:.8f}".format(tx, ty))

    koch(d - 1, (tx, ty), p2)


n = int(input())

ax = 0
ay = 0

bx = 100
by = 0

print("{0:.8f} {1:.8f}".format(ax, ay))
koch(n, (ax, ay), (bx, by))
print("{0:.8f} {1:.8f}".format(bx, by))

Solution (Python):
import math

def Koch(Px, Py, Qx, Qy, n) :
    if n == 0 :
        return
    else :
        Ax = (2 * Px + Qx) / 3
        Ay = (2 * Py + Qy) / 3
        Bx = (Px + 2 * Qx) / 3
        By = (Py + 2 * Qy) / 3
        Cx = Ax + (Bx - Ax) * math.cos(math.pi/3) - (By - Ay) * math.sin(math.pi/3) 
        Cy = Ay + (Bx - Ax) * math.sin(math.pi/3) + (By - Ay) * math.cos(math.pi/3)
        
        Koch(Px, Py, Ax, Ay, n-1)
        print(Ax, Ay)
        Koch(Ax, Ay, Cx, Cy, n-1)
        print(Cx, Cy)
        Koch(Cx, Cy, Bx, By, n-1)
        print(Bx, By)
        Koch(Bx, By, Qx, Qy, n-1)

n = int(input())
print(0, 0.00000000)
Koch(0.00000000, 0.00000000, 100.00000000, 0.00000000, n)
print(100.00000000, 0.00000000)

Solution (Python):
import math

pi = math.pi

n = int(input())
p1 = [0,0]
p2 = [100,0]

def koch(n, p1, p2):
    if n == 0:
        return
    s = [(2*p1[0]+p2[0])/3.0,(2*p1[1]+p2[1])/3.0]
    t = [(1*p1[0]+2*p2[0])/3.0,(p1[1]+2*p2[1])/3.0]
    u = [(t[0]-s[0])*math.cos(pi/3) - (t[1]-s[1])*math.sin(pi/3) + s[0], 
         (t[0]-s[0])*math.sin(pi/3) + (t[1]-s[1])*math.cos(pi/3) + s[1]]
         

    koch(n - 1, p1, s)
    print(*s)
    koch(n - 1, s, u)
    print(*u)
    koch(n - 1, u, t)
    print(*t)
    koch(n - 1, t, p2)

print(*p1)
koch(n, p1, p2)
print(*p2)

Solution (Python):
import math


def koch(n, p1_x, p1_y, p2_x, p2_y):
    if n == 0:
        print(f"{p1_x:.8f} {p1_y:.8f}")
        return

    

    
    s_x = (2 * p1_x + 1 * p2_x) / 3
    s_y = (2 * p1_y + 1 * p2_y) / 3

    t_x = (p1_x + 2 * p2_x) / 3
    t_y = (p1_y + 2 * p2_y) / 3

    
    u_x = (
        (t_x - s_x) * math.cos(math.pi / 3) - (t_y - s_y) * math.sin(math.pi / 3) + s_x
    )
    u_y = (
        (t_x - s_x) * math.sin(math.pi / 3) + (t_y - s_y) * math.cos(math.pi / 3) + s_y
    )

    
    koch(n - 1, p1_x, p1_y, s_x, s_y)
    koch(n - 1, s_x, s_y, u_x, u_y)
    koch(n - 1, u_x, u_y, t_x, t_y)
    koch(n - 1, t_x, t_y, p2_x, p2_y)


n = int(input())
koch(n, 0, 0, 100, 0)
print("100.00000000 0.00000000")

Solution (Python):
import math
def kock(n,p1,p2):
    a=math.radians(60)
    if n==0:
        return 0
    s=[(2*p1[0]+p2[0])/3,(2*p1[1]+p2[1])/3]
    t=[(p1[0]+2*p2[0])/3,(p1[1]+2*p2[1])/3]
    u=[((t[0]-s[0])*math.cos(a)-(t[1]-s[1])*math.sin(a))+s[0],(t[0]-s[0])*math.sin(a)+(t[1]-s[1])*math.cos(a)+s[1]]
    kock(n-1,p1,s)
    print(" ".join(map(str,s)))
    kock(n-1,s,u)
    print(" ".join(map(str,u)))
    kock(n-1,u,t)
    print(" ".join(map(str,t)))
    kock(n-1,t,p2)
n=int(input())
start=[float(0),float(0)]
end=[float(100),float(0)]
print(" ".join(map(str,start)))
kock(n,start,end)
print(" ".join(map(str,end)))

Solution (Python):
import math
cos60 = math.cos(math.pi*60/180)
sin60 = math.sin(math.pi*60/180)


def add_triangle(start, end):
    x1 = (2*start[0] + end[0]) / 3
    x3 = (start[0] + 2*end[0]) / 3
    y1 = (2*start[1] + end[1]) / 3
    y3 = (start[1] + 2*end[1]) / 3
    x2 = x1 + (x3-x1)*cos60 - (y3-y1)*sin60
    y2 = y1 + (x3-x1)*sin60 + (y3-y1)*cos60
    return [(x1,y1), (x2,y2), (x3,y3)]



def koch_recursion(list1):
    output = []
    for i in range(len(list1)-1):
        output.append(list1[i])
        output += add_triangle(list1[i], list1[i+1])
    output.append(list1[len(list1)-1])
    return output


def Koch(start, end, n):
    output = [start, end]
    for i in range(n):
        output = koch_recursion(output)
    for x, y in output:
        print("{:.8f} {:.8f}".format(x, y))

n = int(input())
Koch((0,0), (100,0), n)

Solution (Python):

def s(p,q):
    a=p[0]
    b=p[1]
    c=q[0]
    d=q[1]
    return [(2*a+c)/3,(2*b+d)/3]
def t(p,q):
    a=p[0]
    b=p[1]
    c=q[0]
    d=q[1]    
    return [(a+2*c)/3,(b+2*d)/3]
def u(p,q):
    a=p[0]
    b=p[1]
    c=q[0]
    d=q[1]
    return [(a+c)/2+(3**(1/2))*(b-d)/6,(b+d)/2+(3**(1/2))*(c-a)/6]

def koch(p1,p2,n):
    if n==0:
        return [p1]
    else:
        return koch(p1,s(p1,p2),n-1)+koch(s(p1,p2),u(p1,p2),n-1)+koch(u(p1,p2),t(p1,p2),n-1)+koch(t(p1,p2),p2,n-1)

n=int(input())
a=[0,0]
b=[100,0]
for i in koch(a,b,n):
    print(str(i[0])+" "+str(i[1]))

print(str(b[0])+" "+str(b[1]))

Solution (Python):
import math
def plot(x,y):
    print(f'{x:.8f} {y:.8f}')
def Koch(n, x1,y1,x2,y2):
    if n==0:
        plot(x1,y1)
        return
    sx=(2*x1+x2)/3
    sy=(2*y1+y2)/3
    tx=(x1+2*x2)/3
    ty=(y1+2*y2)/3
    ux=(tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy=(tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    
    Koch(n-1, x1,y1, sx,sy)
    Koch(n-1, sx,sy, ux,uy)
    Koch(n-1, ux,uy, tx,ty)
    Koch(n-1, tx,ty, x2,y2)
    
n=int(input())
Koch(n, 0,0, 100,0)
plot(100,0)

Solution (Python):
from math import sin, cos, pi

def koch_curve(d, p1, p2):
    if d == 0:
        return

    s = [None, None]
    u = [None, None]
    t = [None, None]
    
    s[0] = (2*p1[0] + p2[0])/3
    s[1] = (2*p1[1] + p2[1])/3
    t[0] = (p1[0] + 2*p2[0])/3
    t[1] = (p1[1] + 2*p2[1])/3
    u[0] = (t[0] - s[0])*cos(1/3*pi) - (t[1] - s[1])*sin(1/3*pi) + s[0]
    u[1] = (t[0] - s[0])*sin(1/3*pi) + (t[1] - s[1])*cos(1/3*pi) + s[1]

    koch_curve(d-1, p1, s)
    print(s[0], s[1])
    koch_curve(d-1, s, u)
    print(u[0], u[1])
    koch_curve(d-1, u, t)
    print(t[0],t[1])
    koch_curve(d-1, t, p2)

num = int(input())
print(0.0, 0.0)
koch_curve(num, [0.0, 0.0], [100.0, 0.0])
print(100.0, 0.0)

Solution (Python):
import math
def plot(x,y):
    print(f"{x:.8f} {y:.8f}")
def koch(n,x1,y1,x2,y2):
    if n==0:
        plot(x1,y1)
        return
    sx=(2*x1+x2)/3
    sy=(2*y1+y2)/3
    tx=(x1+2*x2)/3
    ty=(y1+2*y2)/3
    ux=(tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy=(tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    koch(n-1,x1,y1,sx,sy)
    koch(n-1,sx,sy,ux,uy)
    koch(n-1,ux,uy,tx,ty)
    koch(n-1,tx,ty,x2,y2)
n=int(input())
koch(n,0,0,100,0)
plot(100,0)

Solution (Python):
import math

def koch(n,p1,p2):

    sx = 2/3 * p1[0] + 1/3 * p2[0]
    sy = 2/3 * p1[1] + 1/3 * p2[1]
    s = (sx,sy)

    tx = 1/3 * p1[0] + 2/3 * p2[0]
    ty = 1/3 * p1[1] + 2/3 * p2[1]
    t = (tx,ty)

    theta = math.radians(60)
    ux = (tx - sx)*math.cos(theta) - (ty - sy)*math.sin(theta) + sx
    uy = (tx - sx)*math.sin(theta) + (ty - sy)*math.cos(theta) + sy
    u = (ux,uy)

    if n>0:
        koch(n-1,p1,s)
        print(*s)
        koch(n-1,s,u)
        print(*u)
        koch(n-1,u,t)
        print(*t)
        koch(n-1,t,p2)
    else: return

n = int(input())
a = [0.00000000,0.00000000]
b = [100.00000000,0.00000000]
print(*map(float,a))
koch(n,a,b)
print(*map(float,b))

Solution (Python):
import math

def plot(x,y):
    print(f'{x:.8f} {y:.8f}')

def Koch(n, x1, y1, x2, y2):
    if n == 0:
        plot(x1, y1)
        return
    sx = (2 * x1 + 1 * x2)/3
    sy = (2 * y1 + 1 * y2)/3
    tx = (1 * x1 + 2 * x2)/3
    ty = (1 * y1 + 2 * y2)/3
    ux = (tx - sx) * (1/2) - (ty - sy) * (math.sqrt(3)/2) + sx
    uy = (tx - sx) * (math.sqrt(3)/2) + (ty - sy) * (1/2) + sy
    Koch(n-1, x1,y1, sx,sy)
    Koch(n-1, sx,sy, ux,uy)
    Koch(n-1, ux,uy, tx,ty)
    Koch(n-1, tx,ty, x2,y2)

n = int(input())
Koch(n, 0,0, 100,0)
plot(100,0)

Solution (Python):
import sys, os, math, bisect, itertools, collections, heapq, queue





sys.setrecursionlimit(10000000)

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def koch(N, ax, ay, bx, by):
    if N == 0: return

    
    radians = math.radians(60)
    
    
    sx = (2 * ax + bx) / 3
    sy = (2 * ay + by) / 3
    tx = (ax + 2 * bx) / 3
    ty = (ay + 2 * by) / 3

    
    ux = (tx - sx) * math.cos(radians) - (ty - sy) * math.sin(radians) + sx
    uy = (tx - sx) * math.sin(radians) + (ty - sy) * math.cos(radians) + sy

    
    koch(N - 1, ax, ay, sx, sy)
    print('{:.08f} {:.08f}'.format(sx, sy))  

    
    koch(N - 1, sx, sy, ux, uy)
    print('{:.08f} {:.08f}'.format(ux, uy))  

    
    koch(N - 1, ux, uy, tx, ty)
    print('{:.08f} {:.08f}'.format(tx, ty))  

    
    koch(N - 1, tx, ty, bx, by)


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N = ii()
    print('{:.08f} {:.08f}'.format(0, 0))  
    koch(N, 0, 0, 100, 0)
    print('{:.08f} {:.08f}'.format(100, 0))  


if __name__ == '__main__':
    main()

Solution (Python):
def koch(n, p1, p2):
    if n == 0:
        return

    s_x = p1[0] * 2/3 + p2[0] * 1/3
    s_y = p1[1] * 2/3 + p2[1] * 1/3

    t_x = p1[0] * 1/3 + p2[0] * 2/3
    t_y = p1[1] * 1/3 + p2[1] * 2/3

    u_x = (t_x - s_x) * 1/2 - (t_y - s_y) * 3 ** 0.5 / 2 + s_x
    u_y = (t_x - s_x) * 3 ** 0.5 / 2 + (t_y - s_y) / 2 + s_y

    s, t, u = (s_x, s_y), (t_x, t_y), (u_x, u_y)
    
    koch(n - 1, p1, s)
    print(' '.join(str(i) for i in s))
    koch(n - 1, s, u)
    print(' '.join(str(i) for i in u))
    koch(n - 1, u, t)
    print(' '.join(str(i) for i in t))
    koch(n - 1, t, p2)
    
    
S, G = (0, 0), (100, 0)
print(' '.join(str(i) for i in S))
koch(int(input()), S, G)
print(' '.join(str(i) for i in G))

Solution (Python):
import math
n = int(input())

A = []
def koch(d, p1, p2):
    rot = math.radians(60)
    if d == 0:
        return 0
    else:
        s = ((2.0 * p1[0] + p2[0]) / 3.0, (2.0 * p1[1] + p2[1]) / 3.0)
        t = ((2.0 * p2[0] + p1[0]) / 3.0, (2.0 * p2[1] + p1[1]) / 3.0)
        ux = (t[0] - s[0]) * math.cos(rot) - (t[1] - s[1]) * math.sin(rot) + s[0]
        uy = (t[0] - s[0]) * math.sin(rot) + (t[1] - s[1]) * math.cos(rot) + s[1]
        u = (ux, uy)
        koch(d-1, p1, s) 
        print(s[0], s[1])
        koch(d-1, s, u)
        print(u[0], u[1])
        koch(d-1, u, t)
        print(t[0], t[1])
        koch(d-1, t, p2)

p1 = (0.0, 0.0)
p2 = (100.0, 0.0)
print(p1[0], p1[1])
koch(n, p1, p2)
print(p2[0], p2[1])

Solution (Python):
from math import sqrt
q3 = sqrt(3)
fmt = "%.8f %.8f"
def koch(x0, y0, x1, y1, c):
    if c == n:
        return (fmt % (x0, y0),)
    xp = (x0*2 + x1) / 3
    yp = (y0*2 + y1) / 3

    xq = (x0 + x1*2) / 3
    yq = (y0 + y1*2) / 3

    dx = (x1 - x0) / 6
    dy = (y1 - y0) / 6

    xr = xp + (dx - dy * q3)
    yr = yp + (dx * q3 + dy)

    return koch(x0, y0, xp, yp, c+1) + koch(xp, yp, xr, yr, c+1) + koch(xr, yr, xq, yq, c+1) + koch(xq, yq, x1, y1, c+1)
n = int(input())
print(*koch(0, 0, 100, 0, 0)+(fmt % (100, 0),), sep='\n')

Solution (Python):
n = int(input())
r3 = 3 ** 0.5
A = (0.0, 0.0)
B = (100.0, 0.0)

def koch(n, A, B):
    if n == 0:
        return

    C1 = ((2 * A[0] + B[0]) / 3, (2 * A[1] + B[1]) / 3)
    C3 = ((A[0] + 2 * B[0]) / 3, (A[1] + 2 * B[1]) / 3)
    X, Y = C3[0] - C1[0], C3[1] - C1[1]
    C2 = (C1[0] + (X - Y * r3) / 2, C1[1] + (X * r3 + Y) / 2)

    koch(n - 1, A, C1)
    print("{:.5f} {:.5f}".format(C1[0], C1[1]))
    koch(n - 1, C1, C2)
    print("{:.5f} {:.5f}".format(C2[0], C2[1]))
    koch(n - 1, C2, C3)
    print("{:.5f} {:.5f}".format(C3[0], C3[1]))
    koch(n - 1, C3, B)

print("{:.5f} {:.5f}".format(A[0], A[1]))
koch(n, A, B)
print("{:.5f} {:.5f}".format(B[0], B[1]))

Solution (Python):
from math import sqrt
q3 = sqrt(3)
fmt = "%.8f %.8f"
def koch(x0, y0, x1, y1, c):
    if c == n:
        return (fmt % (x0, y0),)
    xp = (x0*2 + x1) / 3
    yp = (y0*2 + y1) / 3

    xq = (x0 + x1*2) / 3
    yq = (y0 + y1*2) / 3

    dx = (x1 - x0) / 6
    dy = (y1 - y0) / 6

    xr = xp + (dx - dy * q3)
    yr = yp + (dx * q3 + dy)

    return koch(x0, y0, xp, yp, c+1) + koch(xp, yp, xr, yr, c+1) + koch(xr, yr, xq, yq, c+1) + koch(xq, yq, x1, y1, c+1)
n = int(input())
print(*koch(0, 0, 100, 0, 0)+(fmt % (100, 0),), sep='\n')

Solution (Python):
from math import cos, sin, pi


class Point():
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def show(self):
        print(f"{self.x:.8f} {self.y:.8f}")


def calc_s(p1, p2) -> Point:
    x = p1.x * 2 / 3 + p2.x / 3
    y = p1.y * 2 / 3 + p2.y / 3
    s = Point(x, y)
    return s


def calc_t(p1, p2) -> Point:
    x = p1.x / 3 + p2.x * 2 / 3
    y = p1.y / 3 + p2.y * 2 / 3
    t = Point(x, y)
    return t


def calc_u(s, t) -> Point:
    theta = pi / 3
    x = s.x + (t.x - s.x) * cos(theta) - (t.y - s.y) * sin(theta)
    y = s.y + (t.x - s.x) * sin(theta) + (t.y - s.y) * cos(theta)
    u = Point(x, y)
    return u


def koch(n, p1, p2):
    if n == 0:
        return

    s = calc_s(p1, p2)
    t = calc_t(p1, p2)
    u = calc_u(s, t)

    koch(n - 1, p1, s)
    s.show()

    koch(n - 1, s, u)
    u.show()

    koch(n - 1, u, t)
    t.show()

    koch(n - 1, t, p2)


def main():
    n = int(input())
    p1 = Point(0, 0)
    p2 = Point(100, 0)

    p1.show()
    koch(n, p1, p2)
    p2.show()


if __name__ == "__main__":
    main()

Solution (Python):
import math
def plot(x,y):
    print(f'{x:.8f} {y:.8f}')
def koch(n,x1,y1,x2,y2):
    if n==0:
        plot(x1,y1)
        return
    sx=(2*x1+x2)/3
    sy=(2*y1+y2)/3
    tx=(x1+2*x2)/3
    ty=(y1+2*y2)/3
    ux=(tx-sx)*math.cos(math.radians(60))-(ty-sy)*math.sin(math.radians(60))+sx
    uy=(tx-sx)*math.sin(math.radians(60))+(ty-sy)*math.cos(math.radians(60))+sy
    koch(n-1,x1,y1,sx,sy)
    koch(n-1,sx,sy,ux,uy)
    koch(n-1,ux,uy,tx,ty)
    koch(n-1,tx,ty,x2,y2)
n=int(input())
koch(n,0,0,100,0)
plot(100,0)

Solution (Python):
import math

def KC(n,p1,p2):
    if n==0:
        return
    s=[(2*p1[0]+p2[0])/3,(2*p1[1]+p2[1])/3]
    t=[(p1[0]+2*p2[0])/3,(p1[1]+2*p2[1])/3]
    u=[(t[0]-s[0])*math.cos(math.pi/3)-(t[1]-s[1])*math.sin(math.pi/3)+s[0],(t[0]-s[0])*math.sin(math.pi/3)+(t[1]-s[1])*math.cos(math.pi/3)+s[1]]
    KC(n-1,p1,s)
    print(str(format(s[0],'.8f'))+" "+str(format(s[1],'.8f')))
    KC(n-1,s,u)
    print(str(format(u[0],'.8f'))+" "+str(format(u[1],'.8f')))
    KC(n-1,u,t)
    print(str(format(t[0],'.8f'))+" "+str(format(t[1],'.8f')))
    KC(n-1,t,p2)
    
  
if __name__=="__main__":
    n=int(input())
    p1=[0,0]
    p2=[100,0]
    print(str(format(p1[0],'.8f'))+" "+str(format(p1[1],'.8f')))
    KC(n,p1,p2)
    print(str(format(p2[0],'.8f'))+" "+str(format(p2[1],'.7f')))

Solution (Python):
import math

class Point:
    def __init__(self,arg_x,arg_y):
        self.x = arg_x
        self.y = arg_y


def roll(div1,div2):
    tmp_start = Point(div2.x-div1.x,div2.y-div1.y)
    tmp_end = Point(tmp_start.x*math.cos(math.pi/3) -tmp_start.y*math.sin(math.pi/3),
                    tmp_start.x*math.sin(math.pi/3) + tmp_start.y*math.cos(math.pi/3))
    ret = Point(tmp_end.x + div1.x,tmp_end.y + div1.y)
    return ret;


def outPut(point):
    print("%.8f %.8f"%(point.x,point.y))


def calc(left,right,count):
    div1 = Point((2*left.x + right.x)/3,(2*left.y + right.y)/3)
    div2 = Point((2*right.x + left.x)/3,(2*right.y + left.y)/3)
    peek = roll(div1,div2)

    if count > 1:
        calc(left,div1,count-1)
        calc(div1,peek,count-1)
        calc(peek,div2,count-1)
        calc(div2,right,count-1)
    else:
        outPut(left);
        outPut(div1);
        outPut(peek);
        outPut(div2);


N = int(input())
left = Point(0,0)
right = Point(100,0)

if N == 0:
    outPut(left)
    outPut(right)
else:
    calc(left,right,N)
    outPut(right)

Solution (Python):
import math

def plot(x,y):
    print(f'{x:.8f} {y:.8f}')

def Koch(n, x1,y1, x2,y2):
    if n==0:
        plot(x1,y1)
        return
    
    sx=(2*x1+x2)/3
    sy=(2*y1+y2)/3
    tx=(x1+2*x2)/3
    ty=(y1+2*y2)/3
    ux=(tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy=(ty-sy)*(1/2)+(tx-sx)*(math.sqrt(3)/2)+sy
    
    Koch(n-1, x1,y1, sx,sy)
    Koch(n-1, sx,sy, ux,uy)
    Koch(n-1, ux,uy, tx,ty)
    Koch(n-1, tx,ty, x2,y2)

n=int(input())

Koch(n, 0,0, 100,0)
plot(100,0)

Solution (Python):
import math

def plot(x,y):
    print(f'{x:.8f} {y:.8f}')
    
def koch(n,x1,y1,x2,y2):
    if n==0:
        plot(x1,y1)
        return
    
    sx=(2*x1+x2)/3
    sy=(2*y1+y2)/3
    tx=(x1+2*x2)/3
    ty=(y1+2*y2)/3
    
    ux=(tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy=(tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    koch(n-1,x1,y1,sx,sy)
    koch(n-1,sx,sy,ux,uy)
    koch(n-1,ux,uy,tx,ty)
    koch(n-1,tx,ty,x2,y2)
    
n=int(input())
koch(n,0,0,100,0)
plot(100,0)

Solution (Python):
import math

def plot(x, y):
    print(f'{x:.8f} {y:.8f}')

def Koch(n, x1, y1, x2, y2):
    if n == 0:
        plot(x1, y1)
        return
    sx = (2*x1 + x2) / 3
    sy = (2*y1 + y2) / 3
    tx = (x1 + 2*x2) / 3
    ty = (y1 + 2*y2) / 3
    ux = (tx - sx) * (1/2) - (ty - sy) * (math.sqrt(3))/2 + sx
    uy = (tx - sx) * (math.sqrt(3))/2 + (ty - sy) * (1/2) + sy
    Koch(n-1, x1, y1, sx, sy)
    Koch(n-1, sx, sy, ux, uy)
    Koch(n-1, ux, uy, tx, ty)
    Koch(n-1, tx, ty, x2, y2)


n = int(input())
Koch(n, 0, 0, 100, 0)
    
plot(100, 0)

Solution (Python):
import math

def plot(x,y):
    print(f'{x:.8f} {y:.8f}')
    
def Koch(n, x1,y1, x2,y2):
    if n==0:
        plot(x1,y1)
        return
    sx=(2*x1+x2)/3
    sy=(2*y1+y2)/3
    tx=(x1+2*x2)/3
    ty=(y1+2*y2)/3
    ux=(tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy=(tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    Koch(n-1, x1,y1, sx,sy)
    Koch(n-1, sx,sy, ux,uy)
    Koch(n-1, ux,uy, tx,ty)
    Koch(n-1, tx,ty, x2,y2)
    
n=int(input())
Koch(n, 0,0, 100,0)

plot(100,0)

Solution (Python):
import math
def plot(x,y):
    print(f'{x:.8f} {y:.8f}')

def Koch(n,x1,y1,x2,y2): 
    if n==0:
        plot(x1,y1) 
        return
    sx=(2*x1+x2)/3
    sy=(2*y1+y2)/3
    tx=(x1+2*x2)/3
    ty=(y1+2*y2)/3      
    
    ux=(tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy=(tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy  
    
    Koch(n-1,x1,y1,sx,sy) 
    Koch(n-1,sx,sy,ux,uy)
    Koch(n-1,ux,uy,tx,ty)
    Koch(n-1,tx,ty,x2,y2)
    
    
n=int(input())    
Koch(n,0,0,100,0)
plot(100,0)

Solution (Python):
import math


def matrix_mul(mx1, mx2):
    n = len(mx1)
    m = len(mx1[0])
    _m = len(mx2)
    l = len(mx2[0])
    if m != _m:
        print("[!] Invalid Matrix Size.")
        return - 1

    ans = [[None] * l for _ in range(n)]

    for i in range(n):
        for j in range(l):
            _e = 0
            for k in range(m):
                _e += mx1[i][k] * mx2[k][j]
            ans[i][j] = _e

    return ans


def roll(p1, p2, r: int):
    """r must be radian"""
    _tmp = [[p2[0] - p1[0]], [p2[1] - p1[1]]]
    rad = math.pi*r/180
    matrix = [[math.cos(rad), -math.sin(rad)], [math.sin(rad), math.cos(rad)]]
    _tmp = matrix_mul(matrix, _tmp)
    return [p1[0]+_tmp[0][0], p1[1]+_tmp[1][0]]


def print_point(p):
    print("%.8f %.8f" % (p[0], p[1]))


def koch_curve(p1, p2, d: int):
    """p1: start point, p2: end point, d: depth of recursive"""
    if d == 0:
        print_point(p2)
    else:
        s = [p1[0] + (p2[0] - p1[0])/3, p1[1] + (p2[1] - p1[1])/3]
        t = [p1[0] + (p2[0] - p1[0])*2/3, p1[1] + (p2[1] - p1[1])*2/3]
        u = roll(s, t, 60)
        koch_curve(p1, s, d - 1)
        koch_curve(s, u, d - 1)
        koch_curve(u, t, d - 1)
        koch_curve(t, p2, d - 1)


n = int(input())
p1, p2 = [0.0, 0.0], [100.0, 0.0]
print_point(p1)
koch_curve(p1, p2, n)

Solution (Python):
import math
def plot(x,y):
    print(f'{x:.8f} {y:.8f}')
 
def Koch(n,x1,y1,x2,y2):
    if n == 0:
        plot(x1,y1)
        return
    sx = ( 2 * x1 + x2 ) / 3
    sy = ( 2 * y1 + y2 ) / 3
    tx = ( x1 + 2 * x2 ) / 3
    ty = ( y1 + 2 * y2 ) / 3
    ux = ( tx - sx ) * ( 1 / 2 ) - ( ty - sy ) * ( math.sqrt(3)/2 ) + sx
    uy = ( tx - sx ) * ( math.sqrt(3)/2 ) + ( ty - sy ) * ( 1 / 2 ) + sy
    Koch(n-1, x1,y1, sx,sy)
    Koch(n-1, sx,sy, ux,uy)
    Koch(n-1, ux,uy, tx,ty)
    Koch(n-1, tx,ty, x2,y2)
n=int(input())
Koch(n,0,0,100,0)
plot(100,0)

Solution (Python):
import math
def plot(x,y):
    print(f'{x:.8f} {y:.8f}')
def Koch(n,x1,y1,x2,y2):
    if n==0:
        plot(x1,y1)
        return
    sx=(2*x1+x2)/3
    sy=(2*y1+y2)/3
    tx=(x1+2*x2)/3
    ty=(y1+2*y2)/3
    ux=(tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy=(tx-sx)*(math.sqrt(3)/2)+sy+(ty-sy)*(1/2)
    Koch(n-1,x1,y1,sx,sy)
    Koch(n-1,sx,sy,ux,uy)
    Koch(n-1,ux,uy,tx,ty)
    Koch(n-1,tx,ty,x2,y2)
n=int(input())
Koch(n,0,0,100,0)
plot(100,0)

Solution (Python):
import math

def plot(x,y):
    print(f'{x:.8f} {y:.8f}')

def koch(n, x1,y1,x2,y2):
    if n==0:
        plot(x1,y1)
        return
    sx=(2*x1+x2)/3
    sy=(2*y1+y2)/3
    tx=(x1+2*x2)/3
    ty=(y1+2*y2)/3
    ux=(tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy=(tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    koch(n-1, x1,y1, sx,sy)
    koch(n-1, sx,sy, ux,uy)
    koch(n-1, ux,uy, tx,ty)
    koch(n-1, tx,ty, x2,y2)
    
    
n=int(input())
koch(n, 0,0, 100,0)
plot(100,0)

Solution (Python):
import math

def plot(x,y):
    print(f'{x:.8f} {y:.8f}')
    
def Koch(n, x1,y1,x2,y2):
    if n == 0:
        plot(x1,y1)
        return
    sx = (2 * x1 + x2) / 3
    sy = (2 * y1 + y2) / 3
    tx = (x1 + 2 * x2) / 3
    ty = (y1 + 2 * y2) / 3
    ux = (tx-sx) * (1/2) - (ty-sy) * (math.sqrt(3)/2) + sx
    uy = (tx-sx) * (math.sqrt(3)/2) + (ty-sy) * (1/2) + sy
    Koch(n-1, x1,y1, sx,sy)
    Koch(n-1, sx,sy, ux,uy)
    Koch(n-1, ux,uy, tx,ty)
    Koch(n-1, tx,ty, x2,y2)
    
n=int(input())
Koch(n, 0,0, 100,0)
plot(100,0)

Solution (Python):
import math
def plot(x,y):
    print(f'{x:.8f} {y:.8f}')
def koch(n,x1,y1,x2,y2):
    if n==0:
        plot(x1,y1)
        return
    sx=(2*x1+x2)/3
    sy=(2*y1+y2)/3
    tx=(x1+2*x2)/3
    ty=(y1+2*y2)/3
    ux=(tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy=(tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    koch(n-1,x1,y1,sx,sy)
    koch(n-1,sx,sy,ux,uy)
    koch(n-1,ux,uy,tx,ty)
    koch(n-1,tx,ty,x2,y2)
    
n=int(input())
koch(n,0,0,100,0)
plot(100,0)

Solution (Python):
import math
def plot(x,y):
    print(f'{x:.8f} {y:.8f}')
def koch(n,x1,y1,x2,y2):
    if n==0:
        plot(x1,y1)
        return
    sx = (2*x1+x2)/3
    sy = (2*y1+y2)/3
    tx = (x1+2*x2)/3
    ty = (y1+2*y2)/3
    ux = (tx-sx)*(1/2)-(ty-sy)*(math.sqrt(3)/2)+sx
    uy = (tx-sx)*(math.sqrt(3)/2)+(ty-sy)*(1/2)+sy
    koch(n-1,x1,y1,sx,sy)
    koch(n-1,sx,sy,ux,uy)
    koch(n-1,ux,uy,tx,ty)
    koch(n-1,tx,ty,x2,y2)
    
n=int(input())
koch(n,0,0,100,0)
plot(100,0)