How is the checker function implemented and updated here? (from CS61a UCB)

120 Views Asked by At

The question description is as follows: Implement div_by_primes_under, which takes in an integer n and returns an n-divisibility checker. An n-divisibility-checker is a function that takes in an integer k and returns whether k is divisible by any integers between 2 and n, inclusive. Equivalently, it returns whether k is divisible by any primes less than or equal to n.

[cs61a ](https://cs61a.org/lab/sol-lab03/

and the solution is:

def div_by_primes_under(n):
    """
    >>> div_by_primes_under(10)(11)
    False
    >>> div_by_primes_under(10)(121)
    False
    >>> div_by_primes_under(10)(12)
    True
    >>> div_by_primes_under(5)(1)
    False
    """
    checker = lambda x: False
    i = 2
    while i <= n:
        if not checker(i):
            checker = (lambda f, i: lambda x: x % i == 0 or f(x))(checker, i)
        i = i + 1
    return checker

Does checker always return True since "i%i == 0",How does the checker function work, where is the essence of this lambda function, I am confused about this.

0

There are 0 best solutions below