[Python] Finding multiples of 3 or 5 below 1000

Discussão em 'Python' iniciado por Stack, Março 2, 2025.

  1. Stack

    Stack Membro Participativo

    I am just starting to try my hands in the problems of Project Euler and i have got stuck in the first code itself. Can someone please tell me in the below code where i am going wrong.

    The problem statement is as follows:


    If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

    Find the sum of all the multiples of 3 or 5 below 1000.
    ​

    And my solution is as follows:

    multiple1 = 5
    multiple2 = 3
    index = 2
    sum = 0
    while multiple1 < 1000 or multiple2 < 1000:
    if multiple1 < 1000:
    sum = sum + multiple1
    multiple1 = 5 * index
    if multiple2 < 1000:
    sum = sum + multiple2
    multiple2 = 3 * index
    index = index + 1

    print (sum)


    I know someone has already posted the solution somewhere, but i just want to figure out where i am going wrong with the logic.

    Continue reading...

Compartilhe esta Página