0% found this document useful (0 votes)
0 views

Phython_Week1

The document contains Python code examples demonstrating various implementations of the GCD (Greatest Common Divisor) algorithm, including iterative and recursive methods, as well as optimizations. It also includes a quiz section with questions related to functions and their outputs. The document serves as a tutorial for learning Python programming concepts related to GCD calculations.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Phython_Week1

The document contains Python code examples demonstrating various implementations of the GCD (Greatest Common Divisor) algorithm, including iterative and recursive methods, as well as optimizations. It also includes a quiz section with questions related to functions and their outputs. The document serves as a tutorial for learning Python programming concepts related to GCD calculations.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

NPTEL courses...

1> Simple GCD phython programs

'''

Online Python Compiler.


Code, Compile, Run and Debug python program online.
Write your code in this editor and press "Run" button to execute it.

'''

def gcd(m,n):
"""docstring for gcd"""
# TODO: write code...
fm = []
for i in range(1,m+1):
if (m%i) == 0:
# TODO: write code...
fm.append(i)

fn = []
for j in range(1,n+1):
if (n%j) == 0:
# TODO: write code...
fn.append(j)

cf = []
for k in fm:
if k in fn:
cf.append(k)
# below code returns right most number from the list
return(cf[-1])

print("Hello World")

m=63
n=9
print(gcd(m,n))

2> Improve coed for for gcd instead of having multipl loops..replaced with single
loop

def gcd(m,n):
"""docstring for gcd"""
# TODO: write code...
cf = []
for i in range(1, min(m,n)+1):
#print(i)
if (m%i) == 0 and (n%i) == 0:
# TODO: write code...
cf.append(i)
#print(i)

return(cf[-1])

print("Hello World")
m=63
n=9
print(gcd(m,n))gcd algorithm

3> More improving --> Not using lists at all...

def gcd(m,n):
"""docstring for gcd"""
# TODO: write code...
factorLatest = 1
for i in range(1, min(m,n)+1):
#print(i)
if (m%i) == 0 and (n%i) == 0:
# TODO: write code...
factorLatest=i
#print(i)

return(factorLatest)

print("Hello World")

m=63
n=9
print(gcd(m,n))

4> more optimizaion..doing in backward..(scan backward)

def gcd(m,n):
"""docstring for gcd"""
# TODO: write code...
factorLatest = 1
i=min(m,n)

while i>0:
# TODO: write code...if
if (m%i) == 0 and (n%i) == 0:
return(i)
else:
i=i-1

print("Hello World")

m=63
n=9
print(gcd(m,n))

5> Euclid's algorithm for gcd od 2 numbers

Phython exchanging 2 variables values

m=1
n=3
(m,n)=(n,m)
#after above step m =3 and n= 1

Euclid's Algorithm: #in case of m > n

d is a number which is divides m and n


if d divides m a times and d divides n b times then m = ad, n = bd
so m-n = (a-b)d
and d divides (m-n) as well
so gcd (m,n) = gcd(n,m-n)

inthe

def gcd(m,n):
"""gcd by using recursion"""
# Here assumption is that m is always greater than n
# TODO: write code...
if (m<n):
(m,n)=(n,m)

if(m%n) == 0:
return(n)
else:
diff = m-n
gcd(max(n,diff), min(n,diff))

print("Hello World")

m=63
n=9
print(gcd(m,n))

def gcd(m,n):
"""gcd by using recursion"""
# Here assumption is that m is always greater than n
# TODO: write code...
if (m<n):
(m,n)=(n,m)

while (m%n) != 0:
diff = m-n
(m,n) = (max(n,diff), min(n,diff))

return(n)

print("Hello World")

m=63
n=9
print(gcd(m,n))

QUIZ>

Week 1 Quiz

1> What is the value of f(4000) for the function below?


def f(x):
d=0
while x >= 1:
(x,d) = (x/5,d+1)
return(d)
1> 800, 1
2> 160, 2
3> 32, 3
4> 6, 4
5> 1, 5
6> 0, 6

ans: 6

2> What is h(36)-h(34), given the definition of h below?


def h(n):
s = 0
for i in range(2,n):
if n%i == 0:
s = s+i
return(s)

h(36)
: 36%2
s =2
s = 2+3
s = 5+4
s = 9+6
s= 15+9
s = 24+12
s = 36+18
s = 54 +

h(34)
s = 2
s = 2+17
s = 19 +
Ans: 35

for loop range checks less than outter range..

For what value of n would g(637,n) return 4? If there are multiple possibilities,
write any one.
def g(m,n):
res = 0
while m >= n:
(res,m) = (res+1,m/n)
return(res)
5
ANs:5

You might also like