add logo and update gcd and make lcm

This commit is contained in:
Rohit Tanwar
2018-01-09 02:00:17 +05:30
parent fd42cd7994
commit 2ab9ef8cd0
7 changed files with 83 additions and 11 deletions

View File

@ -6,20 +6,34 @@ The `helperGcdfunction` uses recursion. Base case is when `y` equals `0`. In thi
Uses the reduce function from the inbuild module `functools`. Also defines a method `spread` for javascript like spreading of arrays.
``` python
```python
from functools import reduce
def spread(arg):
ret = []
for i in arg:
if isinstance(i,list):
ret.extend(i)
else:
ret.append(i)
return ret
ret = []
for i in arg:
if isinstance(i, list):
ret.extend(i)
else:
ret.append(i)
return ret
def gcd(*args):
numbers = []
numbers.extend(spread(list(args)))
def helperGcd(x,y):
return x if not y else gcd(y,x%y)
return reduce((lambda x,y : helperGcd(x,y)),numbers)
def _gcd(x, y):
return x if not y else gcd(y, x % y)
return reduce((lambda x, y: _gcd(x, y)), numbers)
```
``` python
gcd(8,36); # 4
```

40
snippets/lcm.md Normal file
View File

@ -0,0 +1,40 @@
### lcm
Returns the least common multiple of two or more numbers.
Use the `greatest common divisor (GCD)` formula and the fact that `lcm(x,y) = x * y / gcd(x,y)` to determine the least common multiple. The GCD formula uses recursion.
```python
from functools import reduce
def spread(arg):
ret = []
for i in arg:
if isinstance(i, list):
ret.extend(i)
else:
ret.append(i)
return ret
def lcm(*args):
numbers = []
numbers.extend(spread(list(args)))
def _gcd(x, y):
return x if not y else gcd(y, x % y)
def _lcm(x, y):
return x * y / _gcd(x, y)
return reduce((lambda x, y: _lcm(x, y)), numbers)
```
``` python
lcm(12, 7); # 84
lcm([1, 3, 4], 5); # 60
```