From 101ad7e17777a646dc74cc88c335eecb9b21f431 Mon Sep 17 00:00:00 2001 From: newt! Date: Thu, 26 Aug 2021 01:11:44 +0100 Subject: [PATCH] Add the karatsuba algorithm --- python/calculators/karatsuba algorithm.py | 45 +++++++++++++++++++ .../ngrams/comparison/comparison.py | 17 +++---- readme.md | 1 + 3 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 python/calculators/karatsuba algorithm.py diff --git a/python/calculators/karatsuba algorithm.py b/python/calculators/karatsuba algorithm.py new file mode 100644 index 0000000..f80dd88 --- /dev/null +++ b/python/calculators/karatsuba algorithm.py @@ -0,0 +1,45 @@ +def karatsuba(x, y): + xLen = len(str(x)) + yLen = len(str(y)) + # handle single digit multiplication at the end of the iteration + # this gives the loop an end, and gives us our final results + if xLen == 1 or yLen == 1: + return x * y + else: + n = max(xLen, yLen) // 2 # choose the longest length + # calculate a, b, c, d for the iteration + a = x // (10 ** n) + b = x % (10 ** n) + c = y // (10 ** n) + d = y % (10 ** n) + # run karatsuba to resolve ac and bd for the iteration + ac = karatsuba(a, c) + bd = karatsuba(b, d) + # use karatsuba again to resolve adbc for the iteration + adbc = karatsuba(a + b, c + d) - ac - bd + # return the solution for the digit pairs of the iteration + return ac * 10 ** (2 * n) + (adbc * 10 ** n) + bd + +# helper method to easily take in our inputs +def takeInput(text): + while True: + try: + x = int(input(text)) + return x + except ValueError: + print('You must input an integer!\n') + +num1 = takeInput('Please enter a number (:\n') +num2 = takeInput('Please enter a number to multiply it by!\n') + +# Calculate the result and check if it is right +res = num1 * num2 +karatsubaRes = karatsuba(num1, num2) + +print() +print('Quadratic Result: ', res) +print('Karatsuba Result: ', karatsubaRes) + +if res == karatsubaRes: + print() + print('The algorithm worked (:') diff --git a/python/data science/ngrams/comparison/comparison.py b/python/data science/ngrams/comparison/comparison.py index 7785a1d..daef1fc 100644 --- a/python/data science/ngrams/comparison/comparison.py +++ b/python/data science/ngrams/comparison/comparison.py @@ -21,17 +21,18 @@ frames = [] for x in data: frame = {} + points = x['timeseries'] frame['word'] = x['ngram'] - frame['stdev'] = numpy.std(x['timeseries']) - frame['mean'] = numpy.mean(x['timeseries']) - frame['median'] = numpy.median(x['timeseries']) - frame['mode'] = statistics.mode(x['timeseries']) - frame['range'] = max(x['timeseries']) - min(x['timeseries']) - frame['q1'] = numpy.percentile(x['timeseries'], 25) - frame['q3'] = numpy.percentile(x['timeseries'], 75) + frame['stdev'] = numpy.std(points) + frame['mean'] = numpy.mean(points) + frame['median'] = numpy.median(points) + frame['mode'] = statistics.mode(points) + frame['range'] = max(points) - min(points) + frame['q1'] = numpy.percentile(points, 25) + frame['q3'] = numpy.percentile(points, 75) frame['iqr'] = frame['q3'] - frame['q1'] frames.append(frame) - plt.plot(years, x['timeseries'], label=frame['word']) + plt.plot(years, points, label=frame['word']) df = pandas.DataFrame(frames) print(df) diff --git a/readme.md b/readme.md index 2526075..06cd797 100644 --- a/readme.md +++ b/readme.md @@ -17,6 +17,7 @@ - [Popularity](python/data%20science/ngrams/popularity/popularity.py) - [Calculators](python/calculators) - [Binomial Distribution](python/calculators/binomial%20distribution.py) + - [Karatsuba Algorithm](python/calculators/karatsuba%20algorithm.py) - [Pearson's Product-Moment Correlation Coefficient](python/calculators/pmcc.py) - [Quadratic nth Term](python/calculators/quadratic%20nth%20term.py) - [Square Root](python/calculators/sqrt.py)