DEV Community

loading...

Day 53 Of 100DaysOfCode: More About Algorithm

iamdurga profile image Durga Pokharel ・2 min read

Greetings everybody. I hope you all are great. Today I started to learn SQL from Data camp. Gain more knowledge about Algorithms. Tried to write some algorithm. Completed some assignment on coursera.

Below is my some algorithm today I tried to write myself.

Python code

python code to find Last Digit of a Large Fibonacci Number.

def calc_fib(n):
    a = 0
    b = 1
    fib = [a,b]
    if n <= 0:
        return n

    else:
        for i in range(1,n):
            f = a + b
            a = b
            b = f
        return b



n = int(input())
print(int(str(calc_fib(n))[-1]))
Enter fullscreen mode Exit fullscreen mode

Output of the code is,

999999
6
Enter fullscreen mode Exit fullscreen mode

Python code to compute gcd of two number

def gcd(a,b):
    m1 = min(a,b)
    m2 = max(a,b)
    CF = []
    for i in range(1,m1+1):
        if m1 % i == 0 and m2 % i == 0:
            CF.append(i)
        return max(CF)

a, b = map(int, input().split())
print(gcd(a,b))
Enter fullscreen mode Exit fullscreen mode

When above code run we can get

18 35
1
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

pic
Editor guide