仅使用整数查找平方根 [英] Finding a square root using only integers

查看:134
本文介绍了仅使用整数查找平方根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我在某人的编程课中遇到了一个问题。它要求他们仅使用整数计算平方根;它们使用一个整数来表示小数点前的部分,使用另一个整数来表示小数点后的部分。问题是不允许使用浮点数。

Recently, I came across a problem in someone's programming class. It asked them to compute a square root using only integers; they were to use one integer to represent the part before the decimal point and another integer to represent the part after the decimal point. The problem said that using floating point numbers was not allowed.

然而,在考虑了一段时间之后,我似乎无法想出办法它没有使用浮点。我用Google搜索了高低,我似乎无法找到答案。

However, after thinking about it for some time, I can't seem to come up with a way of doing it without using floating point. I've Googled high and low and I can't seem to find an answer.

我开玩笑地建议我的朋友实施一个FPU来做这个,但他不是'很开心。

I jokingly suggested that my friend implement an FPU to do this, but he wasn't so amused.

有没有人对如何解决这个问题有任何想法?

Does anyone have any ideas about how to go about solving this?

推荐答案

假设您的原始号码是 x


  1. 在小数点之前查找部分很简单 - 只需找到最大数字,哪个方格小于或等于原始数字。

  1. Finding part before decimal point is easy - just find the maximal number, which square is less or equal to the original number.

将原始数字乘以100,将sqrt的整数部分乘以10.将1加1,直到它小于或等于 100x 。在结尾处 n 次并除以 10 ^ n ,将最终答案截断为 n 小数位。

Multiply original number by 100 and the integer part of sqrt by 10. Add 1 to it until it's less or equal to 100x. Do it n times and divide by 10^n at the end to get the final answer truncated to n decimal places.

这篇关于仅使用整数查找平方根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆