Professional Documents
Culture Documents
EE17B022-JEEVANA REDDY
October 15, 2020
Q3
[2]: n = int(input("Enter number of elements : "))
[999109, 998980, 998013, 988997, 985156, 985129, 983320, 982759, 982186, 981477]
Runtime of the program is 0.0010638236999511719
1
Runtime of the program is 0.00034308433532714844
Q4
[7]: def karatsubaMult(x ,y):
x = str(x)
y = str(y)
#base case for recursion
if len(x) == 1 and len(y) == 1:
return int(x) * int(y)
if len(x) < len(y):
x = x.zfill(len(y))
elif len(y) < len(x):
y = y.zfill(len(x))
n = len(x)
if (n % 2) != 0:
n += 1
x = x.zfill(n)
y = y.zfill(n)
j = n//2
a = int(x[:j])
b = int(x[j:])
c = int(y[:j])
d = int(y[j:])
#recursively calculate
ac = karatsubaMult(a, c)
bd = karatsubaMult(b, d)
k = karatsubaMult(a + b, c + d)
A = (int(str(ac)))*(2**n)
B = (int(str(k - ac - bd)))*(2**j)
return A + B + bd
[8]: X = 10011011
Y = 10111010
start = time.time()
print(karatsubaMult(X,Y))
end = time.time()
print(f"Runtime of the program is {end - start}")
28830
2
Runtime of the program is 0.0007526874542236328
Q5
[9]: A0 = [random.randint(-1000000, 1000000) for k in range(0, 100)]
A1=[random.randint(-1000000, 1000000) for k in range(0, 150)]
A0.sort()
A1.sort(reverse=True)
B = A0 + A1
[10]: print(max(B))
999955
mid = (a + b)//2
[13]: A
[13]: 999955
3
Q6
[22]: n = int(input("Enter number of elements : "))
[26]: C=list(set(A))
C.sort()
res.sort()
[27]: if res==C:
print("sorted and removed")