# 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) =

i
1100

function getFactors() {
let n = document.getElementById('n');
let m = document.getElementsByTagName('ins');
if (n) {
let factors  = n.value;
// Python2 sys.maxint = 9223372036854775807
// factors = 44    ->   9419588158802421600
// In Python3, int corresponds to long of Python2, and there is no maximum / minimum limit.
let mcolor = factors > 43? "red": "green";
for ( i of m ) {
i.style.color = mcolor;
}
// python runs on the server, and javascript on the browser
\$.ajax({
type: "GET",
url: "./functions.py",
data: factors,
success: function(response) {
let f = response;
document.getElementById('prod').textContent = `\${f}`;
let bits = Math.trunc(Math.log2(f.split(" = ")[0])) + 1
document.getElementById("range").innerHTML = `(\${bits} bits)`
}
}).fail(function() {
console.log("Could not get data");
});
document.getElementById('i1').textContent = (n.valueAsNumber+1).toString();
document.getElementById('i').textContent = (n.valueAsNumber).toString();
}
}
getFactors();

``````