Add the karatsuba algorithm
This commit is contained in:
parent
1dc61ad51e
commit
9c537355e3
3 changed files with 55 additions and 8 deletions
45
python/calculators/karatsuba algorithm.py
Normal file
45
python/calculators/karatsuba algorithm.py
Normal file
|
@ -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 (:')
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue