비례식 계산기
비례식을 풀고 a/b = c/d에서 미지수를 구합니다.
비례식: a / b = c / d
/=/
구하려는 값을 비워두세요
자주 묻는 질문
코드 구현
def solve_proportion(a, b, c, d):
"""
Solve a/b = c/d for the missing value (pass None for the unknown).
Returns the computed value.
"""
values = [a, b, c, d]
unknowns = [i for i, v in enumerate(values) if v is None]
if len(unknowns) != 1:
raise ValueError("Exactly one value must be None")
idx = unknowns[0]
a, b, c, d = values
if idx == 0: # x/b = c/d -> x = b*c/d
return b * c / d
elif idx == 1: # a/x = c/d -> x = a*d/c
return a * d / c
elif idx == 2: # a/b = x/d -> x = a*d/b
return a * d / b
else: # a/b = c/x -> x = b*c/a
return b * c / a
# Example: 3/4 = x/8 -> x = 6
result = solve_proportion(3, 4, None, 8)
print(f"x = {result}") # x = 6.0
# Scale recipe: 2 cups for 4 servings, how many for 10?
cups = solve_proportion(2, 4, None, 10)
print(f"{cups} cups for 10 servings") # 5.0 cups for 10 servingsComments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.