How to avoid manually setting the initial value of a variable in a recursive function?

I found this solution for Euler 5 project (What is the smallest positive number that is evenly divided by all numbers from 1 to 20?) With a variable range of integer values ​​to evenly divide by:

def Euler5(start, end, counter):
    x = counter
    while start <= end:
        if x%counter == x%start:
            return Euler5(start+1, end, x)
        else:
            x += counter
    return x

However, I have to manually set the counter to the smallest integer value (initial value counter= start). Is there a way to automatically do this and support the algorithm?

+4
source share
1 answer

If I understand correctly, you want the counter == startinitial call without countermanually specifying the first call.

counter None , counter , :

def Euler5(start, end, counter=None):
    if counter is None:
        counter = start

    x = counter
    while start <= end:
        if x % counter == x % start:
            return Euler5(start+1, end, x)
        else:
            x += counter
    return x
+3

Source: https://habr.com/ru/post/1657106/


All Articles