Algorithms: Design and Analysis, Part 1 - Programming Assignment #1
自我总结:
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的更多相关文章
- Algorithms: Design and Analysis, Part 1 - Problem Set 1 - Question 5
最后一个图像,用画图软件绘制了一下,自己的直接主观判断还是有些小问题的 注意:最后的灰色的线条会超过橙色的线条
- 6.046 Design and Analysis of Algorithms
课程信息 6.046 Design and Analysis of Algorithms
- Algorithms : Programming Assignment 3: Pattern Recognition
Programming Assignment 3: Pattern Recognition 1.题目重述 原题目:Programming Assignment 3: Pattern Recogniti ...
- Design and Analysis of Algorithms_Decrease-and-Conquer
I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...
- Design and Analysis of Algorithms_Divide-and-Conquer
I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...
- Design and Analysis of Algorithms_Brute Froce
I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...
- 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 ...
- Design and Analysis of Algorithms_Introduction
I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...
- 课程一(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 ...
随机推荐
- LeetCode SQL:Employees Earning More Than Their Managers
# Write your MySQL query statement below SELECT a.Name FROM Employee AS a INNER JOIN Employee AS b O ...
- 修改input被选中的默认样式
input:focus{ outline: none; border: 1px solid #fff; } 或者 input[type=text]:focus{ outline: n ...
- 国内阿里云Maven镜像(速度飞起)
修改maven根目录下的conf文件夹中的setting.xml文件,内容如下: <mirrors> <mirror> <id>alimaven</id> ...
- flutter控件之ListView滚动布局
ListView即滚动列表控件,能将子控件组成可滚动的列表.当你需要排列的子控件超出容器大小,就需要用到滚动块. import 'package:flutter/material.dart'; cla ...
- struts2 开发模式
在struts.xml中增加: <constant name="struts.devMode" value="true" />
- 那些年vue踩过的坑
1.前言 学习Vue前端框架已经一个月了,作为一个web刚入门的菜鸟,在学习的过程中,网上有些技术博客往往没有什么可以借鉴的地方,在这里 我特意将我从开始一直到登录的过程记录下来.希望看到我的文章的朋 ...
- 你写的什么垃圾代码让Vsync命令不能及时处理呢?(1)
想想自己写的什么垃圾代码导致Vsync不能及时处理#(不高兴) 想不开? 实际开发中性能问题不好复现?这你就可能需要一些工具来帮你检测这种情况. 首先是Android系统自带的工具(4.1之后的版本) ...
- ASP.NET MVC 实现区域 项目分离 (比较好的方式)
说明: ZRT.Web 是前台网站,目录[D:\ZRT.Web\] ZRT.Admin 是后台管理,目录[D:\ZRT.Web\Applications\Admin\],删除文件[Global.asa ...
- 2.MyBatis 动态SQL
动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么痛苦.拼接的时候要确保不能忘了必要的空格 ...
- PHP解决网站大数据大流量与高并发
1:硬件方面 普通的一个p4的服务器每天最多能支持10万左右的IP,如果访问量超过10W那么需要专用的服务器才能解决,如果硬件不给力软件怎么优化都是于事无补的.主要影响服务器的速度 有:网络-硬盘读写 ...