I am trying to find the soln to the following problem:

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?

I am trying to solve this using Python 3.6.1 by creating a function and using the following code:

#a, i and n are used for looping

def smallest_multiple(x):

num=1

multiplier_list=[]

for i in range(1,x+1):

a=1

while a in range (1,i+1):

if a%i==0:

multiplier_list.append(a)

for n in range(0,len(multiplier_list)):

num=num*int(multiplier_list[n])

print (num)

x=int(input("Enter a positive integer:\n"))

smallest_multiple(x)

I am still quite new in Python and I have just started learning 'Classes' in it.

Any idea as to why the program keeps running forever and other ways(however simple or complex) will be extremely appreciated.]]>

https://weltam.wordpress.com/topcoder-cookbook/]]>

you can keep( or save) index value (k) of the number found a[k] which is less than a[i] ( where k < i ) and also the max of each element from 0 to i-1 .

Then, when you check the same thing for i+1 th element, you gradually come to left. When you will come to a[i], you will see that this element has already found its desired value whose index is stored in the i-th element of an array. You can then compare this value with the new one and can take decision.

This algorithm has worst case complexity of O(n2), when all the values will be -1. Otherwise, this algorithm is much faster than O(n2). I solved this problem in a contest and got accepted.

If I could not properly explain, please see the illustration below:

index ={ -1, -1, -1, -1, -1, -1, -1}

input = { 3, 1, 6, 8, 2, 0, 1 }

start with value=3, index=0

there is no value left in three, so index[0] = -1 will remain unchanged.

value=1, index=1

loop through left. Found index[0]=-1 , so will have to go more left, but cannot go beyond 0.

So, index[1] = -1

value=6, index=2

loop through left. Found index[1]=-1 , so will have to go more left

Found index[0] = 3. 3 is less than 6 and also the max value till left-end.

So, we have to keep the index value of 3 as our memory.

So, index[2] = 0

value=8, index=3 (the tricky part is here!)

loop through left. Found index[2]=0 . What does this mean?

This says us, hey! I am index number 2 and I have found my best answer value which is stored in index number 0.

Getting this information, you found that index number 0 contains 3 which is the best value for 6.

Now, we just have to keep the index value of max(3,6)

So, index[3] = 2

This is how the process will go on and fill up the index array.

Finally, to print the output, you just check index array, if there is a -1, you just print -1.

If index[i] contains any other value than -1,

you just print -> input[ index[i] ] like this.

Hope I could make it understood.

Please let me know if you find any problem in the solution.]]>

{ 3, 1, 6, 8, 2, 0, 1 }

I need to find the maximum element on the left hand side of each element which is smaller than the element, or print -1 if that maximum element doesn't exist. So, solution for this problem will be:

{ -1, -1, 3, 6, 1, -1, 0 }

I can solve this in O(n^2) using two loops. Inner loop will find maximum element which is smaller than the given element. But is there any better approach to solve this?]]>

this my code http://ideone.com/FtH8Fs

can anybody tell me the error in the code or testcase on which my code fails]]>

Steven S. Skiena

The Algorithm Design Manual]]>

http://apps.topcoder.com/forums/?module=Thread&threadID=700080&start=0

http://apps.topcoder.com/forums/?module=Thread&threadID=697925&start=0]]>