sympy library can be used to handle mathematical formula. mathemathica and matlab are the famous language in this genre, but they are not open source and expensive software.
The advantage of sympy is that
In [13]:
import sympy as smp
x = smp.Rational(1,3)
y = smp.Rational(1,2)
print(x, y, x+y)
print(x*3, (x+y)*6)
In [14]:
root2 = smp.sqrt(2)
print('Root 2:', root2.evalf(100))
print('Pi: ', smp.pi.evalf(100))
print('e: ', smp.E.evalf(100))
In [15]:
x = smp.Symbol('x') # Define 1 symbol
y, z = smp.symbols('y z') # Define 2 or more symbols
f = x**2 - 2*x + 1
print('Factor', smp.factor(f))
print('Solve', smp.solve(f, x))
In [16]:
f = x + y -7 # x+y = 7
g = x * y -10 # x*y = 10
smp.solve([f,g])
Out[16]:
In [17]:
from sympy import symbols, solve, factor
f = x**7 - 1
print(factor(f))
print(solve(f))
In [18]:
from sympy import factorint
years = [2017,2018,2019]
for y in years:
print(factorint(y))
In [19]:
# print years that are product of 3 different prims
for year in range(1900, 2019):
primes = factorint(year)
if sum(primes.values()) == 3 and len(primes) == 3:
print('Year:', year, 'Prime', primes)
In [20]:
factorint(1234567890123456789012345678913)
Out[20]:
In [ ]: