自我总结:

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

最新文章

  1. c++ 指针(二)
  2. Docker-compose命令详解
  3. PHP中常用的函数
  4. C# 杂项
  5. 使用VSCode创建Asp.Net Core
  6. Java~关于开发工具和包包
  7. Python菜鸟快乐游戏编程_pygame(2)
  8. ABP框架(asp.net core 2.X+Vue)模板项目学习之路(二)--切换MySql数据库
  9. 【深入Java虚拟机(1)】:Java内存区域与内存溢出
  10. 贝叶斯分类器,随机森林,梯度下载森林,神经网络相关参数的意义和data leakage
  11. 【Linux基础】VI命令模式下大小写转换
  12. Feature Extractor[inception v2 v3]
  13. Oracle数据库实例的启动及关闭
  14. Array.prototype.slice.call引发的思考
  15. 为什么要用redis
  16. netcore高性能Web服务器Kestrel分析
  17. Java设计模式(10)代理模式(Proxy模式)
  18. ps -a,job,netstat,daemons
  19. 更换bbr内核
  20. 笔记:javascript操作iframe内的DOM元素,及调用iframe内的方法

热门文章

  1. 列表 enumerat 解包, 针对索引和元素
  2. 详解php 获取文件名basename()函数的用法
  3. javaSE——字节流
  4. JS将秒换成时分秒实现代码 [mark]
  5. SwipeRefreshLayout的高度测量
  6. C# Base64Helper
  7. 关于 未在本地计算机上注册“VFPOLEDB.1” 的解决方案
  8. Oracle EBS 键弹性域 段限定词取值
  9. 上拉加载下拉刷新控件WaterRefreshLoadMoreView
  10. ajax Post数据,并得到返回结果,密码加密(Select,checkbox)