Hello Functions!


Good Morning, CWDC students!



An anonymous lambda function: 35 * 1134 = 39690
            # A lambda function can take any number of arguments, 
            # but can only have one expression.

            multiplyTwoNumbers = lambda x,y: x * y
            print(multiplyTwoNumbers(35,1134))
        


Greatest Common Divisor of 35, 1134 = 7 (Euclides' GCD)
            def gcd(x, y):
                while x:
                    x, y = y % x, x
                return y
            

		35, 1134 = 1134 % 35, 35
		14, 35 = 35 % 14, 14
		7, 14 = 14 % 7, 7
		0, 7 => return 7

Thinking binary.

11 * 13 = 143 (Product using shifts - example)
            def Product (a, b):
                p = 0

                while (b != 0):
                    if (b & 1):
                        p += a
                    a <<= 1
                    b >>= 1

                return p


                1011 ×         11 ×                                    11 ×
                1101           13                                      13
                ----     --------                                --------
                1011 +   1*1*1011 + 1011 << 0                     3*01*11 + 11 << 0
                   0     0*2*1011   0000 << 1                     1*10*11   11 << 1
              101100     1*4*1011   1011 << 2                    --------
             1011000     1*8*1011   1011 << 3                         143
             -------     --------
            10001111          143
        
35 * 1134 = 39690 (Product using shifts)

                     100011 × 
10001101110
----------- 0 1000110 10001100 100011000 0 10001100000 100011000000 0 0 0 1000110000000000 ----------------
1001101100001010 = 39690

Python is a functional language!!

Iterables vs. Iterators vs. Generators

The smallest integer divisible by all integers in the range(1, i+1= ) is:

LCM = lambda m: reduce(lambda a,b:a*b//gcd(a,b),range(1,m+1))

In Python 3, int corresponds to long of Python 2, and there is no maximum / minimum limit.

LCM(i) =

1100