The problem statement goes like this:
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?
Here is my solution:
x=2520.0
list=[]
true_list=[11.0, 12.0, 13.0, 14.0, 16.0, 17.0, 18.0, 19.0, 20.0]
b=1
def test():
for n in true_list:
z=x/n
if z%2==0:
list.append(n)
while b==1:
test()
if list==true_list:
b=2
print x
else:
x=x+20
list=[]
-> Basically, I have defined an empty list which gets filled up by the function test(). What test() does is it checks if the given number (x in this case) is evenly divisible by values from 11-20. If it is, it places that value(between 11-20) in the empty list.
When test() has run it's course, the program checks whether the list is equal to a predefined true_list which contains all numbers from 11-20. If it is, x is printed. Else, the program continues after incrementing the value of x.
This means that if list is equal to true_list, all the numbers from 11-20 are evenly dividing our number (x), which is what's asked in the problem.
It gives the answer: 465585120.0 after running for a minute or so. This happens to be incorrect. I do not know why that is. I've been trying to solve this for 8+ hours now and am at my wit's end. What is the error?
You do not need to read ahead, but in case you have queries about why I've used certain things in my solution, then I've addressed some of them here:
->I have not used all 20 numbers in true_list to speed up the program as any number evenly divisible by 11-20 is also evenly divisible by 1-20 as well.
->I have used x=x+20 to speed up the program because it is just as valid as x=x+1 or x+2;only, it is faster.
->I have used float values because I am using z=x/n in the function 'test()', and i do not want to chop off the decimal part because doing that would make even float values eligible for the subsequent operation i.e. z%2.
example:
1) with int values:
x=17
n=2
z=x/n=8
Here, z%2==0 is valid which should not be the case since it is not actually valid in mathematics.
2) with float values:
x=17.0
n=2.0
z=x/n=8.5
Here, z%n != 0
which is how it should be.