自我总结:

1.编程的思维不够,虽然分析有哪些需要的函数,但是不能比较好的汇总整合

2.写代码能力,容易挫败感,经常有bug,很烦心,耐心不够好

题目:

In this programming assignment you will implement one or more of the integer multiplication algorithms described in lecture.

To get the most out of this assignment, your program should restrict itself to multiplying only pairs of single-digit numbers. You can implement the grade-school algorithm if you want, but to get the most out of the assignment you'll want to implement recursive integer multiplication and/or Karatsuba's algorithm.

So: what's the product of the following two 64-digit numbers?

3141592653589793238462643383279502884197169399375105820974944592

2718281828459045235360287471352662497757247093699959574966967627

[TIP: before submitting, first test the correctness of your program on some small test cases of your own devising. Then post your best test cases to the discussion forums to help your fellow students!]

[Food for thought: the number of digits in each input number is a power of 2. Does this make your life easier? Does it depend on which algorithm you're implementing?]

The numeric answer should be typed in the space below. So if your answer is 1198233847, then just type 1198233847 in the space provided without any space / commas / any other punctuation marks.

(We do not require you to submit your code, so feel free to use any programming language you want --- just type the final numeric answer in the following space.)

答案:

import math
# Base
B=10
# No. of digits
n=64
# Numbers
x=3141592653589793238462643383279502884197169399375105820974944592
y=2718281828459045235360287471352662497757247093699959574966967627 def karatsuba(x, y):
if x < 10 or y < 10:
return x * y
# get longest digits
n = max(math.log10(x) + 1, math.log10(y) + 1)
# catch where n is odd
n -= n % 2
bn = B ** (n // 2)
x1, x2 = divmod(x, bn)
y1, y2 = divmod(y, bn)
ac = karatsuba(x1, y1)
bd = karatsuba(x2, y2)
# caluclate a+b and c + d subtracting already
# calculated ac and bd leaving ad + bc
adbc = karatsuba(x1 + x2, y1 + y2) - ac - bd
# x . y = 10 ^ n ac + 10^n/2 (ad + bc) + bd
return ((B ** n) * ac) + bn * adbc + bd res = karatsuba(x, y) print('%d * %d = %d' % (x, y, res))

运行的结果:

3141592653589793238462643383279502884197169399375105820974944592 * 2718281828459045235360287471352662497757247093699959574966967627 = 8539734222673565727722948056719317944556312698501627377409191379033726264982769845827675624200334881483773142083314390902243328

几个亮点:

1.通过求对数来求数字的长度
# get longest digits
n = max(math.log10(x) + 1, math.log10(y) + 1) 2.通过除以10^(n/2)的商和余数来区分一个数前半部分和后半部分,速度更快 超级好的参考资料:
https://courses.csail.mit.edu/6.006/spring11/exams/notes3-karatsuba

Algorithms: Design and Analysis, Part 1 - Programming Assignment #1的更多相关文章

  1. Algorithms: Design and Analysis, Part 1 - Problem Set 1 - Question 5

    最后一个图像,用画图软件绘制了一下,自己的直接主观判断还是有些小问题的 注意:最后的灰色的线条会超过橙色的线条

  2. 6.046 Design and Analysis of Algorithms

    课程信息 6.046 Design and Analysis of Algorithms

  3. Algorithms : Programming Assignment 3: Pattern Recognition

    Programming Assignment 3: Pattern Recognition 1.题目重述 原题目:Programming Assignment 3: Pattern Recogniti ...

  4. Design and Analysis of Algorithms_Decrease-and-Conquer

    I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...

  5. Design and Analysis of Algorithms_Divide-and-Conquer

    I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...

  6. Design and Analysis of Algorithms_Brute Froce

    I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...

  7. Design and Analysis of Algorithms_Fundamentals of the Analysis of Algorithm Efficiency

    I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...

  8. Design and Analysis of Algorithms_Introduction

    I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...

  9. 课程一(Neural Networks and Deep Learning),第三周(Shallow neural networks)—— 3.Programming Assignment : Planar data classification with a hidden layer

    Planar data classification with a hidden layer Welcome to the second programming exercise of the dee ...

随机推荐

  1. LOJ#6271. 「长乐集训 2017 Day10」生成树求和 加强版

    传送门 由于是边权三进制不进位的相加,那么可以考虑每一位的贡献 对于每一位,生成树的边权相当于是做模 \(3\) 意义下的加法 考虑最后每一种边权的生成树个数,这个可以直接用生成函数,在矩阵树求解的时 ...

  2. 关于j使用ava匿名类的好处总结

    匿名类,除了只能使用一次,其实还有其他用处,比如你想使用一个类的protected方法时,但是又和这个类不在同一个包下,这个时候匿名类就派上用场了,你可以定义一个匿名类继承这个类,在这个匿名类中定义一 ...

  3. AngularJs 第一个自定义指令编写

    公司在做一个OA系统, 包括移动端(从微信企业号进入OA系统),电脑端. 电脑端还是用的传统的easyui做界面,asp.net mvc作为服务端.这个技术已经很成熟了配合权限框架很快就能开发出来.但 ...

  4. Windows win7下VMware Virtual Ethernet Adapter未识别网络解决方法

    win7下VMware Virtual Ethernet Adapter未识别网络解决方法[摘] by:授客 QQ:1033553122 问题描述 win7系统下安装VMware,查看网卡适配器设置, ...

  5. Flutter 控件之 AppBar 和 SliverAppBar

    AppBar 和 SliverAppBar 是纸墨设计中的 App Bar,也就是 Android 中的 Toolbar,关于 Toolbar 的设计指南请参考纸墨设计中 Toolbar 的内容. A ...

  6. 通过ajax记录打印信息

     润乾自带的打印直接可以通过触发js事件来进行调用.onClick="report1_print();return false;" 如果客户需要记录某个用户在某个时间段进行打印 ...

  7. pytts3语音合成遇到的中文问题

    在使用pytts3语音合成时,遇到中文语音错乱.程序代码本身很简单,也是网上公认的一种写法: #coding: UTF-8import pyttsx3; engine = pyttsx3.init() ...

  8. Jmeter 测试 JMS (Java Message Service)/ActiveMQ 性能

    前言 JMS介绍:JMS即Java消息服务(Java Message Service)应用程序接口,是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送 ...

  9. Eigen学习之简单线性方程与矩阵分解

    Eigen提供了解线性方程的计算方法,包括LU分解法,QR分解法,SVD(奇异值分解).特征值分解等.对于一般形式如下的线性系统: 解决上述方程的方式一般是将矩阵A进行分解,当然最基本的方法是高斯消元 ...

  10. Sql语法高级应用之三:存储过程

    一.存储过程概述 SQL Server中的存储过程是使用T_SQL编写的代码段.它的目的在于能够方便的从系统表中查询信息,或者完成与更新数据库表相关的管理任务和其他的系统管理任务.T_SQL语句是SQ ...