幂次方算法pow

     最简单的,调用math头文件,使用pow函数

自定义的递归算法,代码要求的时间与空间复杂度较高,不适合嵌入式设备进行快速计算。代码由C++实现。

double mypow(double x, int y) //x不能为0
{
    if (y >= 0)
    {
        if (y == 0)
            return 1;
        else if (y==1)
        {
            return x;
        }
        else
        {
            return mypow(x, y - 1)*x;
        }   
    }
    else
    {
        if (y == -1)
            return 1 / x;
        else
        {
            return mypow(x, y + 1) / x;
        }

    }
}

迭代算法,复杂度也挺高的。

double mypow(double x, int y) //x不能为0
{
    double temp=1.0f;
    if (y >= 0)
    {
        if (y == 0)
            return 1;
        else
        {
            while (y >=1)
            {
                temp *=x;
                y--;
            }
            return temp;
        }
    }
    else
    {
        while (y <= -1)
        {
            temp /= x;
            y++;
        }
        return temp;
    }
}

优化后的递归算法:
原理:


递归优化
double mypow(double x, int y) 
{
    if (y == 0)
        return 1;
    if (y > 0)
    {
        if (!(y & 1))//偶数
        {
            return mypow(x, y / 2)*mypow(x, y / 2);
            y /= 2;
        }
        else//奇数
        {
            return mypow(x, y - 1)*x;
            --y;
        }
    }
    else//负数
    {
        if (!(y & 1))//偶数
        {
            return mypow(x, y / 2)*mypow(x, y/2);
            y /= 2;
        }
        else//奇数
        {
            return mypow(x, y + 1)/x;
            ++y;
        }
    }
}

同理,迭代pow也可以优化

double mypow(double x, int y) //x不能为0
{
    if (y == 0)
        return 1;
    int tempy = y, tempx = 1;
    double res = 1;
    if (y < 0)
    {
        tempx = -1;
    }
    while (tempy>0)
    {
        tempy /= 2;
        tempx *= 2;
    }
    while (tempy < 0)
    {
        tempy /= 2;
        tempx *= 2;
    }
    tempy = y;
    while (tempx>1)
    {
        tempx /= 2;
        res *= res;
        if (tempy >= tempx)
        {
            res *= x;
            tempy -= tempx;
        }
    }
    while (tempx < -1)
    {
        tempx /= 2;
        res *= res;
        if (tempy <= tempx)
        {
            res /= x;
            tempy -= tempx;
        }
    }
    return res;
}

原理其实也是根据指数奇偶来优化该算法。

double mypow(double x, int y) //x不能为0
{
    if (y == 0)
        return 1;
    double res = 1;
    int tag[65], index = 0,tempy=y;
    while (tempy != 0)
    {
        tag[index++] = tempy & 1;//奇偶标志
        tempy /= 2;
    }
    for (int i = index - 1; i >= 0; i--)
    {
        res *= res;
        if (y > 0)
        {
            if (tag[i] != 0)
            {
                res *= x;
            }
        }
        else if(y<0)
        {
            if (tag[i] != 0)
            {
                res /= x;
            }
        }
    }
    return res;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 写了个显眼的标题,就真得说几句有用的话。 5月份一个很偶然的机会,加了叶神的微信,还收到了祝福。一激动就承诺说写篇...
    XiaoTeng阅读 13,290评论 6 57
  • 周末两天抽空看完了领英中国出版的第一本实体书《你从未真正拼过》,感触颇多。很偶然的机会,关注了Linked...
    nana_missu阅读 1,328评论 0 0
  • 酥脆芝麻烧饼 白面酥油红炉火,烘烤熏焙木香存。 热气微凉齿间脆,柔化舌尖口中香。
    春分时候阅读 1,373评论 0 0
  • 在飞驰的列车上,单调而乏味。手机上的新闻APP看来看去,还是那几条新闻,几个公众号发的文章有几天没有阅读了,也一股...
    顿周的慢生活阅读 1,697评论 0 1

友情链接更多精彩内容