Power of Four

Given an integer (signed 32 bits), write a function to check whether it is a power of 4.(给定一个32位整型数,判断其是否是4的幂)

1.个人分析
判断一个整型数是否是4的幂,必定是从2的幂中寻找符合条件的。1,2,4,8,16,32,64都是2的幂,其中1,4,16,64为4的幂,观察这些数的二进制位发现,它们的1都处在奇数位上,而1在偶数位上的数则都不是4的幂,所以可以用(n & 0xAAAAAAAA)==0作为筛选条件。

2.个人解法
(1)位操作

bool isPowerOfFour(int num)
{
    return (num >=1) && (0 == (num&(num-1))) && (0 == (num&0xAAAAAAAA));
}

(2)迭代

bool isPowerOfFour(int num) 
{      
    if(num<1)
        return false;

    while(num%4 == 0)
        num /= 4;

    return num == 1; 
}

3.总结
结果显示,位操作解法要比循环迭代解法要慢一倍;但如果把(0 == (num&(num-1))) && (0 == (num&0xAAAAAAAA))两者的顺序对调一下,结果运行时间和迭代解法是一样的,很可能是因为逻辑与运算是短路操作,前面的条件语句为false就不用执行后面的条件语句,所以程序就加快运行了。

PS:

  • 题目的中文翻译是本人所作,如有偏差敬请指正。
  • 其中的“个人分析”和“个人解法”均是本人最初的想法和做法,不一定是对的,只是作为一个对照和记录。
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、付费专栏及课程。

余额充值