SICP:反复用加法来实现乘法 1.17

#lang racket

(define (fast-multiplication a b n);a*n
  (cond ((= n 0) b);n==0
    ((even? n)
     (fast-multiplication (double a)
                  b
                  (halve n)));even? n
    (else
     (fast-multiplication a
                  (+ a b)
                  (- n 1))
     );else

    );cond
  );fast-multiplication  it‘s iteration

(define (fast-multiplication-new a n)
  (cond ((= n 0) 0)
    ((= n 1) a)
    ((even? n) (double (fast-multiplication-new a
                        (halve n))))

    (else (a +
         (fast-multiplication-new a
                      (- n 1)))
     );else
   );cond
  );fast-multiplication-new it‘s recursive

(define (double x)
  (+ x x));double

(define (halve x)
  (/ x
     2)
  );halve

(define (even? x)
  (= (remainder x
        2)
    0);=
  );even

(fast-multiplication-new 8 0 1)
(fast-multiplication-new 8 0 2)
(fast-multiplication-new 8 0 3)
(fast-multiplication-new 8 0 4)
(fast-multiplication-new 8 0 9)
时间: 2024-08-08 18:21:14

SICP:反复用加法来实现乘法 1.17的相关文章

高进度加法与高精度乘法

正整数的高精度加法和高精度乘法(C++) 加法运算如下: // 高精度加法 string add(string a, string b) { // 确保 a >= b if (a.size() < b.size()) { string temp = a; a = b; b = temp; } int len1 = a.size(), len2 = b.size(); // a, b前缀补零 ,比如 a = 12345, b = 678时,补零之后 a = 012345,b = 0000678.

【002】}链表或字符串模拟加法/加一/乘法

链表模拟加法/字符串模拟二进制加法/数组模拟加一操作/打印1到最大的n位数/字符串模拟乘法 ============================================ Add Two Numbers 两个链表代表两个数字,每个结点的值都是一位数字,单链表逆序存放这两个数字, 构造出一个新的链表,代表这两个链表的和. 链表的尾插法,头结点dummy结点的运用,统一对prev指针的操作, C++ Code 1234567891011121314151617181920212223242

大数加法、大数乘法

大数加法 hdu1002 #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <cmath> #include <sstream> #include <algorithm> #include <set> #include <map> #include <vector> #i

大整数算法[11] Karatsuba乘法

★ 引子         前面两篇介绍了 Comba 乘法,最后提到当输入的规模很大时,所需的计算时间会急剧增长,因为 Comba 乘法的时间复杂度仍然是 O(n^2).想要打破乘法中 O(n^2) 的限制,需要从一个完全不同的角度来看待乘法.在下面的乘法算法中,需要使用 x 和 y 这两个大整数的多项式基表达式 f(x) 和 g(x) 来表示. 令 f(x) = a * x + b,g(x) = c * x + d,h(x) = f(x) * g(x).这里的 x 相当于一个基,比如十进制下,

[CareerCup] 7.4 Implement Multiply Subtract and Divide 实现乘法减法和除法

7.4 Write methods to implement the multiply, subtract, and divide operations for integers. Use only the add operator. 这道题让我们实现乘法加法和除法,而且规定了只能使用加法.那么我们先来看如何用加法来实现减法,我们知道,减去一个数就等于加上这个数的负数.那么我们先写一个求负数的函数,对于n来说,我们累计n个-1,对于-n来说,我们累计n个1.这样减法的就搞定了,我们再来看乘法,乘

[转]大整数算法[11] Karatsuba乘法

★ 引子         前面两篇介绍了 Comba 乘法,最后提到当输入的规模很大时,所需的计算时间会急剧增长,因为 Comba 乘法的时间复杂度仍然是 O(n^2).想要打破乘法中 O(n^2) 的限制,需要从一个完全不同的角度来看待乘法.在下面的乘法算法中,需要使用 x 和 y 这两个大整数的多项式基表达式 f(x) 和 g(x) 来表示. 令 f(x) = a * x + b,g(x) = c * x + d,h(x) = f(x) * g(x).这里的 x 相当于一个基,比如十进制下,

快速乘法取模算法

原理: 32+16+4=52 1 LL qmul(LL x, LL y, LL mod) { // 乘法防止溢出, 如果p * p不爆LL的话可以直接乘: O(1)乘法或者转化成二进制加法 2 //快速乘法取模算法 3 4 LL ret = 0; 5 while(y) { 6 if(y & 1) 7 ret = (ret + x) % mod; 8 x = x * 2 % mod; 9 y >>= 1; 10 } 11 return ret; 12 } 原文地址:https://www

TCP/IP详细说明--滑模、拥塞窗口、慢启动、Negle算法

TCP的数据流大致能够分为两类,交互数据流与成块的数据流. 交互数据流就是发送控制命令的数据流.比方relogin,telnet.ftp命令等等.成块数据流是用来发送数据的包,网络上大部分的TCP包都是这样的包. 非常明显.TCP在传输这两种类型的包时的效率是不一样的,因此为了提高TCP的传输效率,应该对这两种类型的包採用不同的算法. 总之.TCP的传输原则是尽量降低小分组传输的数量. TCP的交互式数据流 ? 经受时延的确认技术 TCP的交互式数据流通常使用"经过时延的确认"技术.通

JVM的重排序

重排序一般是编译器或执行时环境为了优化程序性能而採取的对指令进行又一次排序执行的一种手段.重排序分为两类:编译期重排序和执行期重排序,分别相应编译时和执行时环境. 在并发程序中,程序猿会特别关注不同进程或线程之间的数据同步.特别是多个线程同一时候改动同一变量时,必须採取可靠的同步或其他措施保障数据被正确地改动.这里的一条重要原则是:不要如果指令运行的顺序,你无法预知不同线程之间的指令会以何种顺序运行. 可是在单线程程序中,通常我们easy如果指令是顺序运行的,否则能够想象程序会发生什么可怕的变化