I need to solve an exercise and I do not know how to finish it because I have a hard time making comparisons and other lists. The exercise asks me to build a program that uses the Egyptian multiplication method, giving me an example 125x41 . The Egyptian method consists of 2 columns:
Step 1:
Column 1: Put the powers of 2 (including 1) until you reach or approach the indicated number (41 in this case). We should stay like this [1, 2, 4, 8, 16, 32].
Column 2: It is multiplied by 2 the number indicated (125 in this case) the same number of times that the first column has been strengthened (the column of 41). We should stay like this [125, 250, 500, 1000, 2000, 4000].
Step 2:
Column 1: Now, taking the last number (32), we have to add it with the others until we get 41. Since 31 + 16 is greater than 41, we discard the 16 and continue with the other. We only add all the cases that give less than 41 until we reach the exact number.
Column 2: In this column we are only going to add specific results that have to do with the first Column. That is, for us to be 41 in column 1, we add only [1, 8, 32] and in column 2 we have to add those 3 numbers that are in the same position as the other column [125, 1000, 4000 ].
This will give us the final result.
So far I was able to do the step 1 , but I do not know how to do the step 2 , can anyone help me? is urgent, thank you very much :
lista1 = []
lista2 = []
n1 = int(input("Ingrese primer número: "))
n2 = int(input("Ingrese segundo número: "))
a = 1
cont1 = 0
cont2 = 0
while a < 2:
lista1.append(a)
a = a + 1
while a <= n2:
lista1.append(a)
a = a * 2
z = len(lista1)
while cont1 < z:
lista2.append(n1)
n1 = n1 * 2
cont1 = cont1 + 1