Multiplication algorithm by sum for any integer

Asked

Viewed 2,698 times

1

I have to develop a sum multiplication algorithm for any integer in the most optimized way possible.

I thought the following: x*y=z;x,y belongs to the whole, so z=x+x y times. I know practically nothing of programming, but I managed to get on these lines:

n1 = int(input('Digite um número'))
n2= int(input('Digite um número'))
multiplicação = (n1 + n1) range (n2)
print("A multiplicação de", n1, "(n1 + n1) range (n", n2, "eh igual a", multiplicação)

My problem is I can’t define the law of multiplication as n1+n1 n2 times. I need someone to give me a light in this aspect, just need this definition. The calculation should still be done in the most optimized way possible ex:

5.2=5+5

nay:

5.2=2+2+2+2+2

If anyone has any idea of that too I would be extremely grateful.

1 answer

1


My other answer already answers that, just do a check to know which value is higher:

n1 = int(input('Digite um número'))

n2 = int(input('Digite um número'))

# Cria uma variável que vai receber um valor booleano (true = positivo, false = negativo)
sinal = True

# Verifica se um dos números é negativo, se for muda o valor de sinal para false (negativo)
if (n1 < 0) != (n2 < 0):
    sinal = False

# Muda o sinal de n1 para positivo se ele for negativo
if n1 < 0:
    n1 *= -1

# Muda o sinal de n2 para positivo se ele for negativo
if n2 < 0:
    n2 *= -1

resultado = 0

if n1 > n2:
    for i in range(n2):
        resultado += n1
else:
    for i in range(n1):
        resultado += n2

# Se o sinal for false transforma o resultado para negativo
if sinal == False:
    resultado *= -1

print("A multiplicação de", n1, "(n1 + n1) range (n", n2, "eh igual a", resultado)

An expression n *= -1 is the same as n = n * -1

  • The optimization part yes,but friend the line "(n1 + n1) range (N2)" is wrong,this is what I can not do,I still could not transcribe to python n1+n1 N2 times,I want to know how to do this line,since the one I did is wrong.

  • I edited the answer, I believe it’s as close as possible

  • It still has to define multiplication,

  • What would be "define multiplication"?

  • "print("The multiplication of", n1, "(n1 + n1) range (n", N2, "eh equal to", result)" Multiplication is not defined as anything,.

  • change multiplicação for resultado as in the answer

  • n1 = int(input('Type a number')) N2= int(input('Type a number') result = 0 if n1 > N2: for n1 in range(N2): result += n1 Else: for N2 in range(n1): result += N2 ("result ", n1, "(n1 + n1) range (n", N2, "eh equal to", result) So it doesn’t work, how would it be? Excuse my ignorance

  • I edited the answer with the correction

  • Thank you so much bro! Seriously thank you for all your patience,I just have to thank you, extremely grateful!

  • Just one more question,?

  • I edited the answer with an explanation

  • Dude without kidding, you are God! The only one who had the patience to help me,I just have to thank, thank you very much msm! From heart!!!! Extremely grateful! Sorry anything!

  • I’m not god I’m just in a similar situation with you, although manjar in php, javascript... I’m starting in python, actually I have to thank, I’m learning teaching, thanks to this question I learned about booleans, loops and xor operator in Python

  • Bro I just have to thank really,my teacher passed this job worth 10 points,without explaining shit,stayed talking about his family three lessons,thanks for all msm!

  • Do you use something specific for study and research? Any website?

  • As I know how to do in another language I search by name in the other python language in google, for example "python array", in js and php is array in python is list

  • Ah got it, anyway thank you very much!

Show 12 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.