Asking you to implement the Math.pow method

The navie implemenation can be:

// O(N)
const pow1 = (x, n) => {
if (n === 0) {
return 1;
} else {
return x * pow1(x, n - 1)
}
} console.log(pow1(2, 8))
// F(8) - (F7) - (F6) - (F5) - (F4) - (F3) - (F2) - (F0)

It takes O(N) time.

Now if we want to improve it to O(logN) time. we can do:

// O(logN)
const pow2 = (x, n) => {
if (n === 0) {
return 1;
}
if (n % 2 === 0) {
const y = pow2(x, n/2);
return y * y;
} else {
return x * pow2(x, n-1);
}
} console.log(pow2(2, 8))
// F(8) - (F4) - (F2) - (F1) - (F0)

[Algorithm] Calculate Pow(x,n) using recursion的更多相关文章

  1. CURLOPT_SSL_VERIFYPEER CURLOPT_SSL_VERIFYHOST

    w /** * Set curl options relating to SSL. Protected to allow overriding. * @param $ch curl handle */ ...

  2. Java-Class-C:java.util.BigDecimal

    ylbtech-Java-Class-C:java.util.BigDecimal 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部 1. /* * Copyright (c) 19 ...

  3. C Primer Plus_第9章_函数_编程练习

    1.题略 /*返回较小值,设计驱动程序测试该函数*/ #include <stdio.h> double min (double a, double b); int main (void) ...

  4. 动态时间归整/规整/弯曲(Dynamic time warping,DTW)

    动态时间规整DTW   在日常的生活中我们最经常使用的距离毫无疑问应该是欧式距离,但是对于一些特殊情况,欧氏距离存在着其很明显的缺陷,比如说时间序列,举个比较简单的例子,序列A:1,1,1,10,2, ...

  5. 数学之路-python计算实战(14)-机器视觉-图像增强(直方图均衡化)

    我们来看一个灰度图像,让表示灰度出现的次数,这样图像中灰度为 的像素的出现概率是  是图像中全部的灰度数, 是图像中全部的像素数,  实际上是图像的直方图,归一化到 . 把  作为相应于  的累计概率 ...

  6. [Algorithms] Divide and Recurse Over an Array with Merge Sort in JavaScript

    Merge sort is a recursive sorting algorithm. If you don't understand recursion, I recommend finding ...

  7. 数据库路由中间件MyCat - 使用篇(4)

    此文已由作者张镐薪授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 配置MyCat 3. 配置conf/rule.xml 1.5GA版本中的规则配置比较笨,2.0中优化了一些, ...

  8. OPENCV(5) —— 图像直方图

    新版本对直方图不再使用之前的histogram的形式,而是用统一的Mat或者MatND的格式来存储直方图,可见新版本Mat数据结构的优势. C++: void calcHist(const Mat* ...

  9. 50. Pow(x, n) (recursion)

    Implement pow(x, n), which calculates x raised to the power n (xn). Example 1: Input: 2.00000, 10 Ou ...

随机推荐

  1. TensorFlow 框架

    TensorFlow TensorFlow核心程序由2个独立部分组成:   a:Building the computational graph构建计算图   b:Running the comput ...

  2. 兼容IE8以下,获取className节点的元素(document.getElementsByClassName()兼容写法)。

    因为ie8一下不兼容                 document.getElementsByClassName()                 功能:通过class的名字获取符合条件的元素 ...

  3. DOM树示意图

  4. centos下配置DNS

    centos网络配置实例 1,配置DNSvi /etc/resolv.conf加入: 代码如下: nameserver 192.168.0.1 nameserver 8.8.8.8 nameserve ...

  5. ERP发货系统的修改(四十三)

    产品添加批号后相应修改产品库存表中对应批次产品的数量: /// <summary> /// 产品添加批号后相应修改产品库存表中对应批次产品的数量 /// </summary> ...

  6. JQUery利用Uploadify插件实现文件异步上传(十一)

    一:简介: Uploadify是JQuery的一个上传插件,实现的效果非常好,带进度显示 ,且Ajax异步,能一次性上传多个文件,功能强大,使用简单 1.支持单文件或多文件上传,可控制并发上传的文件数 ...

  7. 【APUE | 10】函数signal

    函数signal 函数signal介绍 typedef void (*sighandler_t)(int); sighandler_t signal(int signum, sighandler_t ...

  8. hdu 1875 给出每个结点的坐标 权值为两点间的距离 (MST)

    Sample Input2210 10 //坐标20 2031 12 21000 1000 Sample Output1414.2   //最小权值和*100  保留1位小数oh!       //不 ...

  9. B 找规律

    Description 对于正整数n,k,我们定义这样一个函数f,它满足如下规律f(n,k=1)=-1+2-3+4-5+6...nf(n,k=2)=-1-2+3+4-5-6...nf(n,k=3)=- ...

  10. CentOS 用挂了dev/sda1:UNEXPECTED INCONSISTENCY;RUN fsck MANUALLY .

    dev/sda1:UNEXPECTED INCONSISTENCY;RUN fsck MANUALLY .(i.e. ,without -a or -p options)fsck died with ...