PQC Verified
산학계몽(1299년)의 원뿔대 관련 알고리즘
by
gg582 · 2026-11-05 11:58:48 · 54 views · 6 min read
Table of contents
今有圓臺一所 計積五千四十尺
지금 원대(원뿔대) 하나가 있는데 부피는 5,040척이다.
cone_volume = 5040
只云上下周相和得一百八尺
다만 윗둘레와 아랫둘레를 합하면 108척이고,
circ_total = 108
高不及上周一十六尺
높이는 윗둘레에 16척이 미치지 못한다고 한다.
# cone_height = upper_circ - 16
問上下周及高幾何
윗둘레, 아랫둘레, 높이는 각각 얼마인가?
術曰 立天元一爲臺高
풀이법에 이르기를, 천원 일(x)을 세워 대의 높이로 삼는다.
from sympy import Eq, solve, symbols
cone_height = symbols('x')
加一十六尺爲上周
(여기에) 16척을 더하여 윗둘레로 삼는다.
upper_circ = cone_height + 16
以減於相和數爲下周
이를 둘레의 합(108척)에서 감하여 아랫둘레로 삼는다.
lower_circ = circ_total - upper_circ
自乘 又上周自乘 又上下周相乘得
(아랫둘레를) 제곱하고, 또 윗둘레를 제곱하며, 또 윗둘레와 아랫둘레를 서로 곱하여 얻은 뒤,
lower_sq = lower_circ**2
upper_sq = upper_circ**2
upper_lower_mul = lower_circ * upper_circ
三位併之 又以高乘之 爲三十六段圓臺積 寄左列積
세 값을 합하고 다시 높이를 곱하여 '36배의 원대 부피'로 삼아 좌측 열의 식으로 놓아둔다.
left_expr = (lower_sq + upper_sq + upper_lower_mul) * cone_height
以三十六乘之 與寄左相消得開方數式
(원래 부피에) 36을 곱한 것을 좌측에 놓아둔 식과 서로 상쇄하여 개방(방정식) 식을 얻는다.
eq = Eq(left_expr, 36 * cone_volume)
飜法開之 得臺高
번법(개방법)으로 풀어 대의 높이를 얻는다.
roots = solve(eq, cone_height)
cone_height_val = [r for r in roots if r.is_real and 0 < r < (circ_total - 16)][0]
加不及卽上周
부족했던 수(16척)를 더하면 곧 윗둘레가 되고,
upper_circ_val = cone_height_val + 16
又上周減相和數 得下周也
또 윗둘레를 둘레의 합에서 감하면 아랫둘레를 얻는다.
lower_circ_val = circ_total - upper_circ_val
답안
from sympy import Eq, solve, symbols
# 今有圓臺一所 計積五千四十尺
cone_volume = 5040
# 只云上下周相和得一百八尺
circ_total = 108
# 術曰 立天元一爲臺高
cone_height = symbols('x')
# 加一十六尺爲上周
upper_circ = cone_height + 16
# 以減於相和數爲下周
lower_circ = circ_total - upper_circ
# 自乘 又上周自乘 又上下周相乘得
lower_sq = lower_circ**2
upper_sq = upper_circ**2
upper_lower_mul = lower_circ * upper_circ
# 三位併之 又以高乘之 爲三十六段圓臺積 寄左列積
left_expr = (lower_sq + upper_sq + upper_lower_mul) * cone_height
# 以三十六乘之 與寄左相消得開方數式
eq = Eq(left_expr, 36 * cone_volume)
# 飜法開之 得臺高
roots = solve(eq, cone_height)
cone_height_val = [r for r in roots if r.is_real and 0 < r < (circ_total - 16)][0]
# 加不及卽上周
upper_circ_val = cone_height_val + 16
# 又上周減相和數 得下周也
lower_circ_val = circ_total - upper_circ_val
print(cone_height_val, upper_circ_val, lower_circ_val)
미지수 입력으로 개선
from sympy import Eq, solve, symbols
def solve_cone(cone_volume, circ_total, height_diff):
# 術曰 立天元一爲臺高
x = symbols('x')
# 加一十六尺爲上周
upper_circ = x + height_diff
# 以減於相和數爲下周
lower_circ = circ_total - upper_circ
# 自乘 又上周自乘 又上下周相乘得
lower_sq = lower_circ**2
upper_sq = upper_circ**2
upper_lower_mul = lower_circ * upper_circ
# 三位併之 又以高乘之 爲三十六段圓臺積 寄左列積
left_expr = (lower_sq + upper_sq + upper_lower_mul) * x
# 以三十六乘之 與寄左相消得開方數式
eq = Eq(left_expr, 36 * cone_volume)
# 飜法開之 得臺高
roots = solve(eq, x)
cone_height_val = [r for r in roots if r.is_real and 0 < r < (circ_total - height_diff)][0]
# 加不及卽上周
upper_circ_val = cone_height_val + height_diff
# 又上周減相和數 得下周也
lower_circ_val = circ_total - upper_circ_val
return cone_height_val, upper_circ_val, lower_circ_val
def main():
cone_volume = float(input())
circ_total = float(input())
height_diff = float(input())
height, upper, lower = solve_cone(cone_volume, circ_total, height_diff)
print(height, upper, lower)
if __name__ == "__main__":
main()
일본산 원주율으로 정밀도 높이기
여기부턴 산학계몽에 나오는 범위 이상이다.
원주율 값이 일본풍으로 개조되어서 원문은 아니다.
관류에서 정리한 Pi를 쓴다. math.pi보다 정밀하다.
from sympy import Eq, Rational, solve, symbols
# 關流 松永良弼 方圓算術 零約術 密率
# 分子: 四百二十八兆二千二百四十五億九千三百三十四萬九千三百四
# 分母: 一百三十六兆三千八十一億二千一百五十七萬一百一十七
MATSUNAGA_PI_RATIONAL = Rational(428224593349304, 136308121570117)
def solve_cone(cone_volume, circ_total, height_diff):
# 術曰 立天元一爲臺高
x = symbols('x')
# 加一十六尺爲上周
upper_circ = x + height_diff
# 以減於相和數爲下周
lower_circ = circ_total - upper_circ
# 自乘 又上周自乘 又上下周相乘得
lower_sq = lower_circ**2
upper_sq = upper_circ**2
upper_lower_mul = lower_circ * upper_circ
# 三位併之 又以高乘之 寄左列積
left_expr = (lower_sq + upper_sq + upper_lower_mul) * x
# 昔用古率徑一周三 爲三十六段積 今易關流零分密率 爲一十二圓率段積
# 與寄左相消得開方數式
# V = (h / (12 * pi)) * (C1^2 + C2^2 + C1 * C2) 이므로
# (C1^2 + C2^2 + C1 * C2) * h = 12 * pi * V
eq = Eq(left_expr, 12 * MATSUNAGA_PI_RATIONAL * cone_volume)
# 飜法開之 得臺高
roots = solve(eq, x)
cone_height_val = [r for r in roots if r.is_real and 0 < r < (circ_total - height_diff)][0]
# 加不及卽上周
upper_circ_val = cone_height_val + height_diff
# 又上周減相和數 得下周也
lower_circ_val = circ_total - upper_circ_val
return cone_height_val, upper_circ_val, lower_circ_val
def main():
cone_volume = Rational(input())
circ_total = Rational(input())
height_diff = Rational(input())
height, upper, lower = solve_cone(cone_volume, circ_total, height_diff)
print(height.evalf(30))
print(upper.evalf(30))
print(lower.evalf(30))
if __name__ == "__main__":
main()
시각화(일본산 원주율 사용)
import math
import os
import sys
os.environ.setdefault("PYOPENGL_PLATFORM", "glx")
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from sympy import Eq, Rational, solve, symbols
# 關流 松永良弼 方圓算術 零約術 密率
# 分子: 四百二十八兆二千二百四十五億九千三百三十四萬九千三百四
# 分母: 一百三十六兆三千八十一億二千一百五十七萬一百一十七
MATSUNAGA_PI_RATIONAL = Rational(428224593349304, 136308121570117)
def solve_cone(cone_volume, circ_total, height_diff):
# 術曰 立天元一爲臺高
x = symbols('x')
# 加一十六尺爲上周
upper_circ = x + height_diff
# 以減於相和數爲下周
lower_circ = circ_total - upper_circ
# 自乘 又上周自乘 又上下周相乘得
lower_sq = lower_circ**2
upper_sq = upper_circ**2
upper_lower_mul = lower_circ * upper_circ
# 三位併之 又以高乘之 寄左列積
left_expr = (lower_sq + upper_sq + upper_lower_mul) * x
# 昔用古率徑一周三 爲三十六段積 今易關流零分密率 爲一十二圓率段積
# 與寄左相消得開方數式
eq = Eq(left_expr, 12 * MATSUNAGA_PI_RATIONAL * cone_volume)
# 飜法開之 得臺高
roots = solve(eq, x)
cone_height_val = [r for r in roots if r.is_real and 0 < r < (circ_total - height_diff)][0]
# 加不及卽上周
upper_circ_val = cone_height_val + height_diff
# 又上周減相和數 得下周也
lower_circ_val = circ_total - upper_circ_val
return cone_height_val, upper_circ_val, lower_circ_val
rot_x, rot_y = 25.0, -45.0
last_mouse_x, last_mouse_y = 0, 0
mouse_down = False
g_h, g_r_up, g_r_low = 1.0, 1.0, 1.0
def draw_frustum(r_top, r_bottom, height, segments=64):
y_top = height / 2.0
y_bottom = -height / 2.0
# 側象
dr = r_bottom - r_top
length = math.sqrt(height * height + dr * dr)
nx_ratio = height / length
ny_val = dr / length
glBegin(GL_QUAD_STRIP)
for i in range(segments + 1):
theta = 2.0 * math.pi * i / segments
cos_t = math.cos(theta)
sin_t = math.sin(theta)
glNormal3f(cos_t * nx_ratio, ny_val, sin_t * nx_ratio)
glVertex3f(r_top * cos_t, y_top, r_top * sin_t)
glVertex3f(r_bottom * cos_t, y_bottom, r_bottom * sin_t)
glEnd()
# 上面
glNormal3f(0.0, 1.0, 0.0)
glBegin(GL_TRIANGLE_FAN)
glVertex3f(0.0, y_top, 0.0)
for i in range(segments + 1):
theta = 2.0 * math.pi * i / segments
glVertex3f(r_top * math.cos(theta), y_top, r_top * math.sin(theta))
glEnd()
# 下面
glNormal3f(0.0, -1.0, 0.0)
glBegin(GL_TRIANGLE_FAN)
glVertex3f(0.0, y_bottom, 0.0)
for i in range(segments, -1, -1):
theta = 2.0 * math.pi * i / segments
glVertex3f(r_bottom * math.cos(theta), y_bottom, r_bottom * math.sin(theta))
glEnd()
def display():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
max_dim = max(g_h, g_r_low * 2.0)
gluLookAt(0.0, 0.0, max_dim * 2.5, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)
glRotatef(rot_x, 1.0, 0.0, 0.0)
glRotatef(rot_y, 0.0, 1.0, 0.0)
# 彩圖
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
glColor4f(0.2, 0.6, 0.7, 0.75)
draw_frustum(g_r_up, g_r_low, g_h)
glDisable(GL_BLEND)
# 稜
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)
glColor3f(0.9, 0.9, 0.9)
draw_frustum(g_r_up, g_r_low, g_h, segments=32)
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)
glutSwapBuffers()
def reshape(w, h):
if h == 0:
h = 1
glViewport(0, 0, w, h)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45.0, float(w) / float(h), 0.1, 1000.0)
glMatrixMode(GL_MODELVIEW)
def mouse(button, state, x, y):
global mouse_down, last_mouse_x, last_mouse_y
if button == GLUT_LEFT_BUTTON:
if state == GLUT_DOWN:
mouse_down = True
last_mouse_x = x
last_mouse_y = y
elif state == GLUT_UP:
mouse_down = False
def motion(x, y):
global rot_x, rot_y, last_mouse_x, last_mouse_y
if mouse_down:
rot_y += (x - last_mouse_x) * 0.5
rot_x += (y - last_mouse_y) * 0.5
last_mouse_x = x
last_mouse_y = y
glutPostRedisplay()
def init_gl():
glEnable(GL_DEPTH_TEST)
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0)
glEnable(GL_COLOR_MATERIAL)
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE)
glLightfv(GL_LIGHT0, GL_POSITION, [1.0, 1.0, 1.0, 0.0])
glLightfv(GL_LIGHT0, GL_DIFFUSE, [1.0, 1.0, 1.0, 1.0])
glLightfv(GL_LIGHT0, GL_AMBIENT, [0.3, 0.3, 0.3, 1.0])
glClearColor(0.1, 0.1, 0.12, 1.0)
def main():
global g_h, g_r_up, g_r_low
cone_volume = Rational(input())
circ_total = Rational(input())
height_diff = Rational(input())
height, upper, lower = solve_cone(cone_volume, circ_total, height_diff)
print(height.evalf(30))
print(upper.evalf(30))
print(lower.evalf(30))
pi_val = float(MATSUNAGA_PI_RATIONAL)
g_h = float(height)
g_r_up = float(upper) / (2.0 * pi_val)
g_r_low = float(lower) / (2.0 * pi_val)
glutInit(sys.argv)
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
glutInitWindowSize(800, 600)
glutCreateWindow(b"Conical Frustum Display")
init_gl()
glutDisplayFunc(display)
glutReshapeFunc(reshape)
glutMouseFunc(mouse)
glutMotionFunc(motion)
# 本象
glutMainLoop()
if __name__ == "__main__":
main()
Comments
같은 것을 구하는 다른 방법
Report