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://www.cnblogs.com/captain-dl/p/10013924.html

时间: 2024-11-07 21:15:49

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

[Stanford Algorithms: Design and Analysis, Part 2]

Specific topics in Part 2 include: greedy algorithms (scheduling, minimum spanning trees, clustering, Huffman codes), dynamic programming (knapsack, sequence alignment, optimal search trees, shortest paths), NP-completeness and what it means for the

Algorithms: Design and Analysis, Part 1 【program 1/逆序数】

#include<string> #include <vector> #include <fstream> using namespace std; std::vector<int> v; int bigArr[100000]; int helpArr[100000]; _int64 MergeAndCount( int* arr, int left, int mid, int right ) { if( left >= right ) { retur

Algorithms: Design and Analysis, Part 1 【program 2/统计快排比较次数】

#include<string> #include <vector> #include <fstream> using namespace std; std::vector<int> v; int bigArr[10000]; int helpArr[10000]; int partition( int* arr, int left, int right ); int QSort( int* arr, int left, int right ) { if(

Algorithms: Design and Analysis Note

Week2 Master method Assumption:all subproblems has same size Recurrence Format The Master Method formula

[Stanford Algorithms: Design and Analysis, Part 2] c25 HUFFMAN CODES

原文地址:https://www.cnblogs.com/ecoflex/p/10577522.html

[Stanford Algorithms: Design and Analysis, Part 2] c27 The Knapsack Problem

原文地址:https://www.cnblogs.com/ecoflex/p/10640959.html

[Stanford Algorithms: Design and Analysis, Part 2] c28 Sequence Alignment Optimal Substructure

原文地址:https://www.cnblogs.com/ecoflex/p/10658398.html

Design and Analysis of Algorithms_Fundamentals of the Analysis of Algorithm Efficiency_Pseudocode

This pseudocode from the book: <<Introduction to the Design and Analysis of Algorithms_Second Edition>> _ Anany LevitinNote that throughout the paper, we assume that inputs to algorithms fall within their specified ranges and hence require no

Design and Analysis of Algorithms_Brute Froce_Pseudocode

This pseudocode from the book: <<Introduction to the Design and Analysis of Algorithms_Second Edition>> _ Anany LevitinNote that throughout the paper, we assume that inputs to algorithms fall within their specified ranges and hence require no