mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
increment 1
This commit is contained in:
74
maths/BasicMaths.py
Normal file
74
maths/BasicMaths.py
Normal file
@@ -0,0 +1,74 @@
|
||||
import math
|
||||
|
||||
def primeFactors(n):
|
||||
pf = []
|
||||
while n % 2 == 0:
|
||||
pf.append(2)
|
||||
n = int(n / 2)
|
||||
|
||||
for i in range(3, int(math.sqrt(n))+1, 2):
|
||||
while n % i == 0:
|
||||
pf.append(i)
|
||||
n = int(n / i)
|
||||
|
||||
if n > 2:
|
||||
pf.append(n)
|
||||
|
||||
return pf
|
||||
|
||||
def numberOfDivisors(n):
|
||||
div = 1
|
||||
|
||||
temp = 1
|
||||
while n % 2 == 0:
|
||||
temp += 1
|
||||
n = int(n / 2)
|
||||
div = div * (temp)
|
||||
|
||||
for i in range(3, int(math.sqrt(n))+1, 2):
|
||||
temp = 1
|
||||
while n % i == 0:
|
||||
temp += 1
|
||||
n = int(n / i)
|
||||
div = div * (temp)
|
||||
|
||||
return div
|
||||
|
||||
def sumOfDivisors(n):
|
||||
s = 1
|
||||
|
||||
temp = 1
|
||||
while n % 2 == 0:
|
||||
temp += 1
|
||||
n = int(n / 2)
|
||||
if temp > 1:
|
||||
s *= (2**temp - 1) / (2 - 1)
|
||||
|
||||
for i in range(3, int(math.sqrt(n))+1, 2):
|
||||
temp = 1
|
||||
while n % i == 0:
|
||||
temp += 1
|
||||
n = int(n / i)
|
||||
if temp > 1:
|
||||
s *= (i**temp - 1) / (i - 1)
|
||||
|
||||
return s
|
||||
|
||||
def eulerPhi(n):
|
||||
l = primeFactors(n)
|
||||
l = set(l)
|
||||
s = n
|
||||
for x in l:
|
||||
s *= (x - 1)/x
|
||||
return s
|
||||
|
||||
def main():
|
||||
print(primeFactors(100))
|
||||
print(numberOfDivisors(100))
|
||||
print(sumOfDivisors(100))
|
||||
print(eulerPhi(100))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
|
||||
18
maths/FibonacciSequenceRecursion.py
Normal file
18
maths/FibonacciSequenceRecursion.py
Normal file
@@ -0,0 +1,18 @@
|
||||
# Fibonacci Sequence Using Recursion
|
||||
|
||||
def recur_fibo(n):
|
||||
return n if n <= 1 else (recur_fibo(n-1) + recur_fibo(n-2))
|
||||
|
||||
def isPositiveInteger(limit):
|
||||
return limit >= 0
|
||||
|
||||
def main():
|
||||
limit = int(input("How many terms to include in fibonacci series: "))
|
||||
if isPositiveInteger(limit):
|
||||
print("The first {limit} terms of the fibonacci series are as follows:")
|
||||
print([recur_fibo(n) for n in range(limit)])
|
||||
else:
|
||||
print("Please enter a positive integer: ")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
15
maths/GreaterCommonDivisor.py
Normal file
15
maths/GreaterCommonDivisor.py
Normal file
@@ -0,0 +1,15 @@
|
||||
# Greater Common Divisor - https://en.wikipedia.org/wiki/Greatest_common_divisor
|
||||
def gcd(a, b):
|
||||
return b if a == 0 else gcd(b % a, a)
|
||||
|
||||
def main():
|
||||
try:
|
||||
nums = input("Enter two Integers separated by comma (,): ").split(',')
|
||||
num1 = int(nums[0]); num2 = int(nums[1])
|
||||
except (IndexError, UnboundLocalError, ValueError):
|
||||
print("Wrong Input")
|
||||
print(f"gcd({num1}, {num2}) = {gcd(num1, num2)}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
20
maths/ModularExponential.py
Normal file
20
maths/ModularExponential.py
Normal file
@@ -0,0 +1,20 @@
|
||||
def modularExponential(base, power, mod):
|
||||
if power < 0:
|
||||
return -1
|
||||
base %= mod
|
||||
result = 1
|
||||
|
||||
while power > 0:
|
||||
if power & 1:
|
||||
result = (result * base) % mod
|
||||
power = power >> 1
|
||||
base = (base * base) % mod
|
||||
return result
|
||||
|
||||
|
||||
def main():
|
||||
print(modularExponential(3, 200, 13))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
46
maths/SegmentedSieve.py
Normal file
46
maths/SegmentedSieve.py
Normal file
@@ -0,0 +1,46 @@
|
||||
import math
|
||||
|
||||
def sieve(n):
|
||||
in_prime = []
|
||||
start = 2
|
||||
end = int(math.sqrt(n)) # Size of every segment
|
||||
temp = [True] * (end + 1)
|
||||
prime = []
|
||||
|
||||
while(start <= end):
|
||||
if temp[start] == True:
|
||||
in_prime.append(start)
|
||||
for i in range(start*start, end+1, start):
|
||||
if temp[i] == True:
|
||||
temp[i] = False
|
||||
start += 1
|
||||
prime += in_prime
|
||||
|
||||
low = end + 1
|
||||
high = low + end - 1
|
||||
if high > n:
|
||||
high = n
|
||||
|
||||
while(low <= n):
|
||||
temp = [True] * (high-low+1)
|
||||
for each in in_prime:
|
||||
|
||||
t = math.floor(low / each) * each
|
||||
if t < low:
|
||||
t += each
|
||||
|
||||
for j in range(t, high+1, each):
|
||||
temp[j - low] = False
|
||||
|
||||
for j in range(len(temp)):
|
||||
if temp[j] == True:
|
||||
prime.append(j+low)
|
||||
|
||||
low = high + 1
|
||||
high = low + end - 1
|
||||
if high > n:
|
||||
high = n
|
||||
|
||||
return prime
|
||||
|
||||
print(sieve(10**6))
|
||||
24
maths/SieveOfEratosthenes.py
Normal file
24
maths/SieveOfEratosthenes.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import math
|
||||
n = int(raw_input("Enter n: "))
|
||||
|
||||
def sieve(n):
|
||||
l = [True] * (n+1)
|
||||
prime = []
|
||||
start = 2
|
||||
end = int(math.sqrt(n))
|
||||
while(start <= end):
|
||||
if l[start] == True:
|
||||
prime.append(start)
|
||||
for i in range(start*start, n+1, start):
|
||||
if l[i] == True:
|
||||
l[i] = False
|
||||
start += 1
|
||||
|
||||
for j in range(end+1,n+1):
|
||||
if l[j] == True:
|
||||
prime.append(j)
|
||||
|
||||
return prime
|
||||
|
||||
print(sieve(n))
|
||||
|
||||
49
maths/SimpsonRule.py
Normal file
49
maths/SimpsonRule.py
Normal file
@@ -0,0 +1,49 @@
|
||||
|
||||
'''
|
||||
Numerical integration or quadrature for a smooth function f with known values at x_i
|
||||
|
||||
This method is the classical approch of suming 'Equally Spaced Abscissas'
|
||||
|
||||
method 2:
|
||||
"Simpson Rule"
|
||||
|
||||
'''
|
||||
from __future__ import print_function
|
||||
|
||||
|
||||
def method_2(boundary, steps):
|
||||
# "Simpson Rule"
|
||||
# int(f) = delta_x/2 * (b-a)/3*(f1 + 4f2 + 2f_3 + ... + fn)
|
||||
h = (boundary[1] - boundary[0]) / steps
|
||||
a = boundary[0]
|
||||
b = boundary[1]
|
||||
x_i = makePoints(a,b,h)
|
||||
y = 0.0
|
||||
y += (h/3.0)*f(a)
|
||||
cnt = 2
|
||||
for i in x_i:
|
||||
y += (h/3)*(4-2*(cnt%2))*f(i)
|
||||
cnt += 1
|
||||
y += (h/3.0)*f(b)
|
||||
return y
|
||||
|
||||
def makePoints(a,b,h):
|
||||
x = a + h
|
||||
while x < (b-h):
|
||||
yield x
|
||||
x = x + h
|
||||
|
||||
def f(x): #enter your function here
|
||||
y = (x-0)*(x-0)
|
||||
return y
|
||||
|
||||
def main():
|
||||
a = 0.0 #Lower bound of integration
|
||||
b = 1.0 #Upper bound of integration
|
||||
steps = 10.0 #define number of steps or resolution
|
||||
boundary = [a, b] #define boundary of integration
|
||||
y = method_2(boundary, steps)
|
||||
print('y = {0}'.format(y))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
46
maths/TrapezoidalRule.py
Normal file
46
maths/TrapezoidalRule.py
Normal file
@@ -0,0 +1,46 @@
|
||||
'''
|
||||
Numerical integration or quadrature for a smooth function f with known values at x_i
|
||||
|
||||
This method is the classical approch of suming 'Equally Spaced Abscissas'
|
||||
|
||||
method 1:
|
||||
"extended trapezoidal rule"
|
||||
|
||||
'''
|
||||
from __future__ import print_function
|
||||
|
||||
def method_1(boundary, steps):
|
||||
# "extended trapezoidal rule"
|
||||
# int(f) = dx/2 * (f1 + 2f2 + ... + fn)
|
||||
h = (boundary[1] - boundary[0]) / steps
|
||||
a = boundary[0]
|
||||
b = boundary[1]
|
||||
x_i = makePoints(a,b,h)
|
||||
y = 0.0
|
||||
y += (h/2.0)*f(a)
|
||||
for i in x_i:
|
||||
#print(i)
|
||||
y += h*f(i)
|
||||
y += (h/2.0)*f(b)
|
||||
return y
|
||||
|
||||
def makePoints(a,b,h):
|
||||
x = a + h
|
||||
while x < (b-h):
|
||||
yield x
|
||||
x = x + h
|
||||
|
||||
def f(x): #enter your function here
|
||||
y = (x-0)*(x-0)
|
||||
return y
|
||||
|
||||
def main():
|
||||
a = 0.0 #Lower bound of integration
|
||||
b = 1.0 #Upper bound of integration
|
||||
steps = 10.0 #define number of steps or resolution
|
||||
boundary = [a, b] #define boundary of integration
|
||||
y = method_1(boundary, steps)
|
||||
print('y = {0}'.format(y))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user