Integer 仅使用整数求平方根

Integer 仅使用整数求平方根,integer,square-root,Integer,Square Root,最近,我在某人的编程课上遇到了一个问题。它要求他们只使用整数计算平方根;他们用一个整数表示小数点之前的部分,用另一个整数表示小数点之后的部分。问题是不允许使用浮点数 然而,经过一段时间的思考,我似乎无法想出一种不使用浮点的方法。我到处搜索,似乎找不到答案 我开玩笑地建议我的朋友实现一个FPU来实现这一点,但他并不觉得有趣 有人知道如何解决这个问题吗?假设您的原始号码是x 查找小数点之前的部分很容易-只需找到最大数,即平方小于或等于原始数 将原始数字乘以100,将sqrt的整数部分乘以10。向其添

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

然而,经过一段时间的思考,我似乎无法想出一种不使用浮点的方法。我到处搜索,似乎找不到答案

我开玩笑地建议我的朋友实现一个FPU来实现这一点,但他并不觉得有趣


有人知道如何解决这个问题吗?

假设您的原始号码是
x

  • 查找小数点之前的部分很容易-只需找到最大数,即平方小于或等于原始数

  • 将原始数字乘以100,将sqrt的整数部分乘以10。向其添加1,直到其小于或等于
    100x
    。做
    n
    次,最后除以
    10^n
    ,得到的最终答案被截断为
    n
    小数位


  • 假设您有一个算法,可以创建两个整数
    A
    B
    ,这样比率
    A/B
    就可以为您提供所需的平方根近似值,精确到小数位数。然后,您需要的两个数字将是:

    • 整数部分是
      (A-(A%B))/B
    • 小数部分:设
      X
      be
      A%B
      。然后,比率
      X/B
      表示小数部分。然后,通过反复计算
      (10*X-(10*X%B))/B
      并设置
      X=(10*X%B)
      来构建连续数字,直到获得所需的小数位数,从而完成计算小数部分

    要导出平方根的所需分数近似值,可以计算平方根的连分数。

    在伪代码中,它类似于:

    int whole_ans = sqrt(num); //stores final answer integer part
    int dec_ans; //stores final answer decimal part
    
    int num_of_decimals = x   //x is equal to the number of decimal places you want the answer to be
    int targetNum = (num - (whole_ans^2)) * 100;  //initialize target to residual * 100
    int currAns = whole_ans; //initialize target to residual of num - the answer so far
    
    for(int i=0;i<x;i++)
    {
        x = get_next_digit(targetNum,currAns));
        dec_ans = append(dec_ans, x);  //append the new found digit to the right of the current decimal answer
        targetNum = (targetNum - (currAns * 20 + x) * x) * 100; //update target number to be residual + 2 zero digits
        currAns = append(currAns,dec_ans) //append the decimal part of the answer to the current total answer so far
    
    }
    
    func get_next_digit(targetNum,currAns)
    {
    int attempt=0;
    int i=0;
       while(attempt <= targetNum)
       {
           i++;
           attempt = (20 * currAns + i) * i;
       }
    i--;
    return i;
    }
    
    int整数值=sqrt(num)//存储最终答案的整数部分
    国际数据中心//存储最终答案的小数部分
    int num_of_decimals=x//x等于您希望答案的小数位数
    int targetNum=(num-(整数2))*100//将目标初始化为剩余*100
    int currAns=整体//将目标初始化为num的剩余值-到目前为止的答案
    
    对于(int i=0;i假设要计算123.4567的平方根

    然后你所要做的就是将小数点向右移动足够远,以得到一个整数半径-,在本例中为四位-,因此,以下是正确的:

    sqrt(123.4567)=(1/100)*sqrt(1234567)

    这就是,你需要知道的是如何找到一个整数的平方根。为此,考虑下面的代码(C):

    unsigned int\u sqrt(unsigned int n){
    无符号整数结果=0;
    无符号整数计数=1;
    
    对于(;count*count我相信二进制搜索的改进形式将帮助您实现这一点。 让我用c代码详细说明一下

    int square\u number=findSquareRootFloor(给定值);

    int findSquareRootFloor(int编号)
    {
    int低=0;
    int高=数字;
    int-mid=0;
    while(低数量)
    高=中-1;
    否则如果(目标<数量)
    低=中+1;
    其他的
    //精确匹配
    中途返回;
    }
    //如果我们来到这里,中间商店的平方根是多少
    高回报;
    }
    
    您可以使用整数获得浮点的有限表示:这似乎不适用于sqrt(2)。我得到1。很好,但第一个十进制数字是3(1*10=10,小于10的最大平方是3),而不是4(sqrt(2)是的,我现在意识到了。因为这里乘以10是错误的…它是平方根,10不是完美的平方。编辑,这次应该是对的。。。
    unsigned int int_sqrt (unsigned int n) {
    
        unsigned int result = 0;
        unsigned int count  = 1;
    
        for (; count*count <= n; count += 1) {
    
            result = result + 1;
    
        }
    
        return result;
    
    }
    
    unsigned int int_sqrt (unsigned int n) {
    
        unsigned int result = 0;
        unsigned int odd    = 1;
        unsigned int oddsum = 1;
    
        while (oddsum <= n) {
    
            result = result + 1;
            odd = odd + 2;
            oddsum = oddsum + odd;
    
        }
    
        return result;
    
    }