multiples of 2 and 3 in python

Asked

Viewed 1,665 times

0

I’m making a code to compute the first(s) n(s) multiples of two values, and put it in a list. until ai perfect but in the list occurs multiple equal numbers, as could take the equal number from the list?

n = int(input('Quantidade de múltiplos'))
i = int(input('Primeiro valor '))
j = int(input('Segundo valor '))
s = 0
lista = [0]
while s < n:
    s += 1
    m1 = i * s
    m2 = j * s
    lista += [m1, m2]
    lista.sort()
print(lista)

3 answers

0

To calculate the multiples of two values you can use the following code:


m = int(input('Digite o primeiro número: '))
n = int(input('Digite o segundo número: '))
q = int(input(f'Desejas calcular quantos múltiplo de {m} e de {n}? '))

soma1 = soma2 = 0
mult1 = list()
mult2 = list()
while len(mult1) < q and len(mult2) < q:
    soma1 += m
    soma2 += n
    mult1.append(soma1)
    mult2.append(soma2)

print(f'Os {q} primeiros múltiplos de {m} são: {mult1}')
print(f'Os {q} primeiros múltiplos de {n} são: {mult2}')

Note that when we execute the code we receive the following message: Digite o primeiro número: . At this point we must enter the first value and press enter. Then we received the second message: Digite o segundo número: . At this point we must enter the second number and press enter. Then we received the third message: Desejas calcular quantos múltiplos de {m} e {n}? . At this point we must enter the amount of multiples of both numbers and press enter.

After that the block while will perform q iterations and, for face iteration will be added the accumulated sum of its values in the lists multi1 and multi2 respectively and then display tabs the lists containing their respective multiples.

Observing:

This code is capable of calculating a number of multiples of any two numbers. integers.

0

Don’t try to do everything in one operation. Divide your code into small steps:

  • Collect user’s initial data.

  • Create two lists. A list will store multiples of the first number, the second list will store multiples of the second number.

  • Eternal through the interval [1 , Number of multiples + 1[ and join to each of their respective multiples.

  • Join the two lists and remove duplicates(step later implemented in the language creating a set which is an unordered collection without duplicate elements).

  • Sort the result (later implemented in the language using the bulti-in function sorted())

Example:

n = int(input('Quantidade de múltiplos: '))
i = int(input('Primeiro número: '))
j = int(input('Segundo número: '))

#Declare l1 e l2 duas listas vazias.
l1, l2 = [], [] 

#Para c variando no intervalo [1, n + 1[
for c in range(1, n + 1):
  l1.append(c * i)   #Apense o c-ésimo múltiplo de i a lista l1.
  l2.append(c * j)   #Apense o c-ésimo múltiplo de j a lista l2.    

s1 = set( l1 + l2 )  #Remove os duplicados ao unir as listas l1 e l2.
print(sorted(s1))    #Imprime o resultado ordenado

Upshot:

Quantidade de múltiplos: 5
Primeiro número: 2
Segundo número: 3
[2, 3, 4, 6, 8, 9, 10, 12, 15]

Test the code on Repl.it

0

If what you want are the n numbers that are simultaneously multiples of the two numbers try:

n = int(input('Quantidade de múltiplos: '))
i = int(input('Primeiro valor '))
j = int(input('Segundo valor '))
s = 0
lista = []
while len(lista) < n:
     s += 1
     if (s % i == 0 and s % j == 0):
         lista.append(s)
print(lista)

Browser other questions tagged

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