Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions week2_algorithmic_warmup/Fibo_sum_last_digit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
n = int(input())
n=(n)%60
if n <= 1:
print(n)
else:

previous = 0
current = 1
sum=1
for _ in range(n-1):
previous, current = current, (previous + current)%10
sum+=current

print(sum%10)




16 changes: 16 additions & 0 deletions week2_algorithmic_warmup/fibonacci.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Uses python3
def calc_fib(n):
if (n <= 1):
return n

else:
f=0
s=1
temp=f+s
for i in range(1,n):
temp=f+s
f=s
s=temp
return temp
n = int(input())
print(calc_fib(n))
30 changes: 30 additions & 0 deletions week2_algorithmic_warmup/fibonacci_huge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Uses python3
import sys
def calc_period(m):
previous = 0
current = 1
p=0
for i in range(m*m):
previous, current = current, previous + current
first = previous % m
sec = current % m
if (first == 0 and sec == 1):
return int(i+1)

def get_fibonacci_huge_naive(n,m):
p=calc_period(m)
n=n%p
if n <= 1:
return n

previous = 0
current = 1

for _ in range(n-1):
previous, current = current, previous + current
return current % m

if __name__ == '__main__':
input = sys.stdin.read()
n, m = map(int, input.split())
print(get_fibonacci_huge_naive(n, m))
16 changes: 16 additions & 0 deletions week2_algorithmic_warmup/fibonacci_last_digit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def solution(num):
n=num%60
if n <= 1:
return(n)

previous = 0
current = 1

for _ in range(1,n):
previous, current = current, previous + current

return (current % 10)

if __name__ == "__main__":
inp=int(input())
print(solution(inp))
13 changes: 13 additions & 0 deletions week2_algorithmic_warmup/gcd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

def gcd_naive(a, b):
if a<b:
return gcd_naive(b,a)
elif b==0:
return a
else:
return gcd_naive(b,a%b)

if __name__ == "__main__":

a, b = map(int, input().split())
print(gcd_naive(a, b))
21 changes: 21 additions & 0 deletions week2_algorithmic_warmup/lcm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Uses python3
import sys


def gcd(a, b):
if (a < b):
return gcd(b, a)
elif (b == 0):
return a
else:
return gcd(b, a % b)


def lcm_naive(a, b):
return (a * b) / gcd(a, b)


if __name__ == '__main__':
input = sys.stdin.read()
a, b = map(int, input.split())
print(int(lcm_naive(a, b)))