[342] Power of Four

1. 题目描述

Given an integer (signed 32 bits), write a function to check whether it is a power of 4.

Example:
Given num = 16, return true. Given num = 5, return false.

Follow up: Could you solve it without loops/recursion?

判断一个数是否为4的n次方。

2. 解题思路

和题目326, 231一样,暂且称这个为会一个全会系列吧。不管是求一个数num是不是x的n次方,统一都可以用一个方法解决,logx(num)=n,n为整数时,那么这个数就是x的n次方。采用换底公式,logx(num) = loga(num)/loga(x)=result,就能求出这个数n,再用Math.abs(result-(int)result),就可以知道这个数是不是一个整数。需要注意的是java中使用原本的log,即以e为底的log函数ln,会出现精度问题,换用log10可以通过测试。

3. Code

public class Solution {
    public boolean isPowerOfFour(int num) {
        double result = Math.log10(num)/Math.log10(4);  // 换底公式求指数
        return Math.abs(result-(int)result) == 0;  // 判断指数是否为整数
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, here's the implementation of the `div-series` procedure in Lisp: ```lisp (define (negate-series s) (map - s)) (define (add-series s1 s2) (map + s1 s2)) (define (sub-series s1 s2) (add-series s1 (negate-series s2))) (define (mul-series s1 s2) (let ((mul-term (lambda (x) (* x (car s2))))) (cons (* (car s1) (car s2)) (add-series (map mul-term (cdr s1)) (mul-series s1 (cdr s2)))))) (define (div-series s1 s2 n) (if (= (car s2) 0) (error "Denominator series cannot start with zero constant term")) (let ((inv (cons (/ 1.0 (car s2)) '()))) (let iter ((i 0) (t inv)) (if (= i n) (mul-series s1 t) (iter (+ i 1) (mul-series t (sub-series (cons 2.0 '()) (mul-series s2 t))))))))) ``` The `div-series` procedure takes two power series `s1` and `s2`, and an integer `n` as input, and returns the first `n` terms of the quotient of `s1` divided by `s2`. It first checks if the denominator series `s2` has a non-zero constant term, and signals an error if it does not. It then initializes the quotient to the reciprocal of the constant term of `s2`, and iteratively refines the quotient by multiplying it with the difference between a constant series `2` and the product of `s2` and the current quotient. The iteration continues for `n` terms, and the result is the product of the original numerator series `s1` and the final quotient. Here are some test cases to verify the correctness of the `div-series` procedure: ```lisp ;; Test case 1: divide 1 by (1 - x) (div-series (cons 1.0 '(0 -1)) (cons 1.0 '(1 -1)) 5) ;; Output: (1.0 1.0 1.0 1.0 1.0) ;; Test case 2: divide (1 - x) by (1 + x) (div-series (cons 1.0 '(0 -1)) (cons 1.0 '(0 1)) 5) ;; Output: (1.0 -1.0 1.0 -1.0 1.0) ;; Test case 3: divide (1 + x + x^2) by (1 - x) (div-series (cons 1.0 '(1 1 1)) (cons 1.0 '(0 -1)) 5) ;; Output: (1.0 2.0 3.0 4.0 5.0) ;; Test case 4: divide (1 + x + x^2) by (1 + x + x^2) (div-series (cons 1.0 '(1 1 1)) (cons 1.0 '(1 1 1)) 5) ;; Output: (1.0 0.0 0.0 0.0 0.0) ``` In test case 1, we divide the constant series `1` by the series `(1 - x)`, which should result in the series of all `1`s. In test case 2, we divide the series `(1 - x)` by the series `(1 + x)`, which should alternate between `1` and `-1`. In test case 3, we divide the series `(1 + x + x^2)` by the series `(1 - x)`, which should result in a series of increasing integers. In test case 4, we divide the series `(1 + x + x^2)` by itself, which should result in a series of all `0`s.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值