thebeast1235
thebeast1235

Reputation: 3

Am I able to extend the range in a for loop?

This is a problem from the Project Euler website and I can not seem to get it right. I want to extend the range of the for loop if the number i am trying to divide is not evenly divisible by x. The question is at the top. Any ideas?

# 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
# What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

def main():
    num = 0
    to = 20
    for num in range(1, to):
        isDivisible = True
        for x in range(1, 20):
            if num % x != 0:
                isDivisible = False
                to += 1    #Here I try to extend the loop
                continue

        if isDivisible:
            print(num)
            break


main()

Upvotes: 0

Views: 53

Answers (1)

Ahmet Bilgin
Ahmet Bilgin

Reputation: 154

I'm not sure it's true but:

def f(x):
    z = 1
    for i in range(1, x + 1):
        for i_2 in range(1,i + 1): #Check the multipliers! For example, since the number 9 is multiplied by 3 before, if it is multiplied by 3 again, it becomes a layer.
            if i % i_2 == 0 and ( z * i_2 ) % i == 0:
                z *= i_2
                break
    print(z)

f(20)

232792560

Upvotes: 1

Related Questions