def long_root_extraction(x, y):
s = str(x)
if len(s) % y != 0:
s = '0' * (y - (len(s) % y)) + s
groups = [s[i:i+y] for i in range(0, len(s), y)]
root = 0
remainder = 0
steps = []
for i, group in enumerate(groups):
current_group = int(group)
current_value = remainder * (10 ** y) + current_group
max_d = 0
term = 0
for d in range(9, -1, -1):
term_candidate = (root * 10 + d) ** y - (root * 10) ** y
if term_candidate <= current_value:
max_d = d
term = term_candidate
break
steps.append({
'step': i + 1,
'group': group,
'current_value': current_value,
'd': max_d,
'term': term,
'remainder': current_value - term,
'current_root': root * 10 + max_d
})
remainder = current_value - term
root = root * 10 + max_d
return root, steps
def print_steps(steps, y):
for step in steps:
print(f"步骤 {step['step']}:")
print(f"处理分组:{step['group']}")
print(f"当前值:{step['current_value']}")
print(f"尝试找到最大的d,使得 ({step['current_root'] // 10}×10 + d)^{y} - ({step['current_root'] // 10}×10)^{y} ≤ {step['current_value']}")
print(f"找到d = {step['d']},因为 {step['term']} ≤ {step['current_value']}")
print(f"余数:{step['current_value']} - {step['term']} = {step['remainder']}")
print(f"当前根:{step['current_root']}\n")
print("-" * 50)
x = int(input('需根数字:'))
y = int(input('需根次数:'))
root, steps = long_root_extraction(x, y)
print(f"最终结果:{x}的{y}次方根整数部分为{root}")
print("\n详细步骤:")
print_steps(steps, y)