mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
Some directories had a capital in their name [fixed]. Added a recursive factorial algorithm. (#763)
* Renaming directories * Adding a recursive factorial algorithm
This commit is contained in:
19
maths/3n+1.py
Normal file
19
maths/3n+1.py
Normal file
@@ -0,0 +1,19 @@
|
||||
def main():
|
||||
def n31(a):# a = initial number
|
||||
c = 0
|
||||
l = [a]
|
||||
while a != 1:
|
||||
if a % 2 == 0:#if even divide it by 2
|
||||
a = a // 2
|
||||
elif a % 2 == 1:#if odd 3n+1
|
||||
a = 3*a +1
|
||||
c += 1#counter
|
||||
l += [a]
|
||||
|
||||
return l , c
|
||||
print(n31(43))
|
||||
print(n31(98)[0][-1])# = a
|
||||
print("It took {0} steps.".format(n31(13)[1]))#optional finish
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
14
maths/FindMax.py
Normal file
14
maths/FindMax.py
Normal file
@@ -0,0 +1,14 @@
|
||||
# NguyenU
|
||||
|
||||
def find_max(nums):
|
||||
max = nums[0]
|
||||
for x in nums:
|
||||
if x > max:
|
||||
max = x
|
||||
print(max)
|
||||
|
||||
def main():
|
||||
find_max([2, 4, 9, 7, 19, 94, 5])
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
12
maths/FindMin.py
Normal file
12
maths/FindMin.py
Normal file
@@ -0,0 +1,12 @@
|
||||
def main():
|
||||
def findMin(x):
|
||||
minNum = x[0]
|
||||
for i in x:
|
||||
if minNum > i:
|
||||
minNum = i
|
||||
return minNum
|
||||
|
||||
print(findMin([0,1,2,3,4,5,-3,24,-56])) # = -56
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
18
maths/abs.py
Normal file
18
maths/abs.py
Normal file
@@ -0,0 +1,18 @@
|
||||
def absVal(num):
|
||||
"""
|
||||
Function to fins absolute value of numbers.
|
||||
>>absVal(-5)
|
||||
5
|
||||
>>absVal(0)
|
||||
0
|
||||
"""
|
||||
if num < 0:
|
||||
return -num
|
||||
else:
|
||||
return num
|
||||
|
||||
def main():
|
||||
print(absVal(-34)) # = 34
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
25
maths/absMax.py
Normal file
25
maths/absMax.py
Normal file
@@ -0,0 +1,25 @@
|
||||
def absMax(x):
|
||||
"""
|
||||
#>>>absMax([0,5,1,11])
|
||||
11
|
||||
>>absMax([3,-10,-2])
|
||||
-10
|
||||
"""
|
||||
j =x[0]
|
||||
for i in x:
|
||||
if abs(i) > abs(j):
|
||||
j = i
|
||||
return j
|
||||
|
||||
|
||||
def main():
|
||||
a = [1,2,-11]
|
||||
print(absMax(a)) # = -11
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
"""
|
||||
print abs Max
|
||||
"""
|
||||
20
maths/absMin.py
Normal file
20
maths/absMin.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from Maths.abs import absVal
|
||||
def absMin(x):
|
||||
"""
|
||||
# >>>absMin([0,5,1,11])
|
||||
0
|
||||
# >>absMin([3,-10,-2])
|
||||
-2
|
||||
"""
|
||||
j = x[0]
|
||||
for i in x:
|
||||
if absVal(i) < absVal(j):
|
||||
j = i
|
||||
return j
|
||||
|
||||
def main():
|
||||
a = [-3,-1,2,-11]
|
||||
print(absMin(a)) # = -1
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
14
maths/average.py
Normal file
14
maths/average.py
Normal file
@@ -0,0 +1,14 @@
|
||||
def average(nums):
|
||||
sum = 0
|
||||
n = 0
|
||||
for x in nums:
|
||||
sum += x
|
||||
n += 1
|
||||
avg = sum / n
|
||||
print(avg)
|
||||
|
||||
def main():
|
||||
average([2, 4, 6, 8, 20, 50, 70])
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
51
maths/extended_euclidean_algorithm.py
Normal file
51
maths/extended_euclidean_algorithm.py
Normal file
@@ -0,0 +1,51 @@
|
||||
# @Author: S. Sharma <silentcat>
|
||||
# @Date: 2019-02-25T12:08:53-06:00
|
||||
# @Email: silentcat@protonmail.com
|
||||
# @Last modified by: silentcat
|
||||
# @Last modified time: 2019-02-26T07:07:38-06:00
|
||||
|
||||
import sys
|
||||
|
||||
# Finds 2 numbers a and b such that it satisfies
|
||||
# the equation am + bn = gcd(m, n) (a.k.a Bezout's Identity)
|
||||
def extended_euclidean_algorithm(m, n):
|
||||
a = 0; aprime = 1; b = 1; bprime = 0
|
||||
q = 0; r = 0
|
||||
if m > n:
|
||||
c = m; d = n
|
||||
else:
|
||||
c = n; d = m
|
||||
|
||||
while True:
|
||||
q = int(c / d)
|
||||
r = c % d
|
||||
if r == 0:
|
||||
break
|
||||
c = d
|
||||
d = r
|
||||
|
||||
t = aprime
|
||||
aprime = a
|
||||
a = t - q*a
|
||||
|
||||
t = bprime
|
||||
bprime = b
|
||||
b = t - q*b
|
||||
|
||||
pair = None
|
||||
if m > n:
|
||||
pair = (a,b)
|
||||
else:
|
||||
pair = (b,a)
|
||||
return pair
|
||||
|
||||
def main():
|
||||
if len(sys.argv) < 3:
|
||||
print('2 integer arguments required')
|
||||
exit(1)
|
||||
m = int(sys.argv[1])
|
||||
n = int(sys.argv[2])
|
||||
print(extended_euclidean_algorithm(m, n))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
13
maths/factorial_recursive.py
Normal file
13
maths/factorial_recursive.py
Normal file
@@ -0,0 +1,13 @@
|
||||
def fact(n):
|
||||
"""
|
||||
Return 1, if n is 1 or below,
|
||||
otherwise, return n * fact(n-1).
|
||||
"""
|
||||
return 1 if n <= 1 else n * fact(n-1)
|
||||
|
||||
"""
|
||||
Shown factorial for i,
|
||||
where i ranges from 1 to 20.
|
||||
"""
|
||||
for i in range(1,21):
|
||||
print(i, ": ", fact(i), sep='')
|
||||
18
maths/find_lcm.py
Normal file
18
maths/find_lcm.py
Normal file
@@ -0,0 +1,18 @@
|
||||
def find_lcm(num_1, num_2):
|
||||
max = num_1 if num_1 > num_2 else num_2
|
||||
lcm = max
|
||||
while (True):
|
||||
if ((lcm % num_1 == 0) and (lcm % num_2 == 0)):
|
||||
break
|
||||
lcm += max
|
||||
return lcm
|
||||
|
||||
|
||||
def main():
|
||||
num_1 = 12
|
||||
num_2 = 76
|
||||
print(find_lcm(num_1, num_2))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user