自我总结:

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. js面向对象设计之class继承

    EcmaScript 2015 (又称ES6)通过一些新的关键字,使类成为了JS中一个新的一等公民.但是目前为止,这些关于类的新关键字仅仅是建立在旧的原型系统上的语法糖,所以它们并没有带来任何的新特性 ...

  2. SQL Server 修改表结构被阻止 解决办法

    在我们的程序开发中,有时候会由于需求的变化而要修改数据库中的表结构.可能是增减列,也可能是修改数据类型,或者修改列名等等.但修改表结构是个危险操作,默认情况下,当你修改表结构时,会弹出如下提示框 上图 ...

  3. ifup / ifdown eth0 / eno1 reports unknown interface when it exists!

    li {list-style-type:decimal;}.wiz-editor-body ol.wiz-list-level2 > li {list-style-type:lower-lati ...

  4. 今年新鲜出炉的30个流行Android库,你一定需要

    作者|Michal Bialas 2017年快过去了,你年初的定的目标都快完成了吗?总结过去三个月内发布的 最新的30 个 Android 库和项目.你一定需要,建议收藏!让你事半功倍 1.Mater ...

  5. Gson基本操作,JsonObject,JsonArray,String,JavaBean,List互转

    (转自)https://www.cnblogs.com/robbinluobo/p/7217387.html String.JsonObject.JavaBean 互相转换 User user = n ...

  6. 分布式部署下的报表调用 API调用 权限问题以及性能方案

     背景描述: 客户的实际情况是需要在具体系统构架前,通过与厂商讨论确定最终的系统架构方案. 需求是客户自己有管理系统,希望建立一个独立的报表服务器,该报表服务器可以对多个管理系统提供报表服务,不知 ...

  7. ASP.NET Claims-based认证实现认证登录-claims基础知识

    claims-based认证这种方式将认证和授权与登录代码分开,将认证和授权拆分成另外的web服务.活生生的例子就是我们的qq集成登录,未必qq集成登录采用的是claims-based认证这种模式,但 ...

  8. Linux服务器安装JDK运行环境教程

    小Alan过些天可能就要去上海出差了,出差干啥?当然是部署项目上线咯!所以呢必须自己学会在Linux服务上面安装部署项目运行环境的啦!今天先跟大家聊聊最基本的jdk运行环境部署安装,后续再跟大家分享一 ...

  9. java web项目中引入spring

    自己动手实践了一次,发生中间出了一下问题,现整理出来,供参考. Step1: 新建一个java web项目 Step2:下载spring的jar包http://repo.spring.io/libs- ...

  10. ThinkPHP执行调用存储过程添加日志

    本文出至:新太潮流网络博客 //PHP代码部分 /** * [LogAdd 操作日志] * @param [string] $userid [用户的ID] * @param [string] $typ ...