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. LeetCode(42):接雨水

    Hard! 题目描述: 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度 ...

  2. Laravel firstOrNew 与 firstOrCreate 的区别

    例如: $item = App\Deployment::firstOrNew( ['name' => '问答小程序'], ['delayed' => 1] ); firstOrNew 需要 ...

  3. php-fpm 配置文件检测

    用过 Nginx 的兄弟都知道,修改 Nginx 配置文件之后,可以使用 nginx -t 来检测配置文件是否有语法错误. 今天配置 opcache 的时候,发现 php-fpm 也可以检测 php- ...

  4. Python decorator

    1.编写无参数的decorator Python的 decorator 本质上就是一个高阶函数,它接收一个函数作为参数,然后,返回一个新函数. 使用 decorator 用Python提供的 @ 语法 ...

  5. Fiddler抓包7-post请求(json)

    前言上一篇讲过get请求的参数都在url里,post的请求相对于get请求多了个body部分,本篇就详细讲解下body部分参数的几种形式. 一.body数据类型 常见的post提交数据类型有四种: 1 ...

  6. python 全栈开发,Day88(csrf_exempt,ES6 快速入门,Vue)

    BBS项目内容回顾 1. 登陆页面 1. 验证码 1. PIL(Pillow) 2. io 2. ORM 1. 增删改查 3. AJAX $.ajax({ url: '', type: '', dat ...

  7. python 全栈开发,Day57(响应式页面-@media介绍,jQuery补充,移动端单位介绍,Bootstrap学习)

    昨日内容回顾 ajax //get post 两种方式 做 请求 get 主要是获取数据 post 提交数据 同一个路由地址 既可以是get请求也可以是post请求 一个路由对应一个函数 get请求 ...

  8. 自己实现一个和PYTHON的库一模一样的sha_256算法

    同时在看一本书<从零开始-自己动手写区块链>, 这书讲得易懂,我也动手实践一下. 这个算法和python3本身的实现相同, 所以,同样的字串,摘要是相同的. import struct i ...

  9. optional

    public class Test { public static void main(String[] args) { People people = new People(); Optional& ...

  10. BZOJ4972 八月月赛 Problem B 小Q的方格纸 二维前缀和

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4972 八月月赛Problem B 题目概括 一个矩阵,一坨询问,问矩阵中一个特定方向的等腰直角三角 ...