Pythonで算数・数学の問題を解く

Modified: | Tags: Python, 算数・数学, まとめ

Pythonを使うと、小学校・中学校・高校レベルの算数や数学の問題を簡単に解ける。

mathfractionなどの標準ライブラリのモジュールだけでも様々なことができるし、SymPyをインストールすると因数分解や微分積分、NumPyをインストールすると行列の演算が可能。

四則演算: +, -, *, /演算子

四則演算には +, -, *, /演算子を使う。そのほか、整数除算の//演算子、剰余(あまり, mod)の%演算子、べき乗の**演算子などがある。

print(10 + 3)
# 13

print(10 - 3)
# 7

print(10 * 3)
# 30

print(10 / 3)
# 3.3333333333333335

分数: fractionsモジュール

標準ライブラリのfractionsモジュールを使うと、数値を分数として扱うことができる。

from fractions import Fraction

print(Fraction(1, 3))
# 1/3

print(Fraction(1, 2) + Fraction(1, 3))
# 5/6

print(Fraction(1, 2) / Fraction(1, 3))
# 3/2

print(Fraction(1, 6) ** 2 + Fraction(1, 2) * Fraction(1, 3))
# 7/36

最大公約数・最小公倍数: mathモジュール

標準ライブラリのmathモジュールに、最大公約数を算出するgcd()関数と最小公倍数を算出するlcm()関数がある。

import math

print(math.gcd(6, 4))
# 2

print(math.lcm(6, 4))
# 12
source: gcd_lcm.py

三角関数: mathモジュール

標準ライブラリのmathモジュールを使うと、三角関数(sin, cos, tan)、逆三角関数(arcsin, arccos, arctan)を計算できる。角度変換(ラジアンと度)も可能。

import math

print(math.pi)
# 3.141592653589793

print(math.degrees(math.pi))
# 180.0

print(math.radians(180))
# 3.141592653589793

sin30 = math.sin(math.radians(30))
print(sin30)
# 0.49999999999999994

print(round(sin30, 1))
# 0.5

指数関数・対数関数: mathモジュール

標準ライブラリのmathモジュールを使うと、指数関数および対数関数(自然対数、常用対数、二進対数)を計算できる。

import math

print(math.log(25, 5))
# 2.0

print(math.log(math.e))
# 1.0

print(math.log10(100000))
# 5.0

print(math.log2(1024))
# 10.0

階乗、順列・組み合わせ: math, itertoolsモジュール

標準ライブラリのmathモジュールに階乗を計算する関数、itertoolsモジュールにリストなどから順列・組み合わせを生成する関数がある。

import math

print(math.factorial(5))
# 120

print(math.factorial(0))
# 1
import itertools

l = ['a', 'b', 'c', 'd']

p = itertools.permutations(l, 2)
print(type(p))
# <class 'itertools.permutations'>

for v in itertools.permutations(l, 2):
    print(v)
# ('a', 'b')
# ('a', 'c')
# ('a', 'd')
# ('b', 'a')
# ('b', 'c')
# ('b', 'd')
# ('c', 'a')
# ('c', 'b')
# ('c', 'd')
# ('d', 'a')
# ('d', 'b')
# ('d', 'c')

集合: set型

組み込みのデータ型として集合を扱うset型が用意されている。

set型は重複しない要素(同じ値ではない要素、ユニークな要素)のコレクションで、和集合、積集合、差集合などの集合演算を行うことができる。

s = {3, 1, 2, 2, 3, 1, 4}
print(s)
# {1, 2, 3, 4}

print(type(s))
# <class 'set'>
source: set.py
s1 = {0, 1, 2}
s2 = {1, 2, 3}

print(s1 | s2)
# {0, 1, 2, 3}
source: set.py

複素数: complex型

組み込みのデータ型として複素数を扱うcomplex型もある。

複素数の演算や極座標変換などができる。

c1 = 3 + 4j
c2 = 2 - 1j

print(c1 + c2)
# (5+3j)

print(c1 - c2)
# (1+5j)

print(c1 * c2)
# (10+5j)

print(c1 / c2)
# (0.4+2.2j)
source: complex.py

方程式、式の展開、因数分解、微分積分: SymPy

代数計算(数式処理)ライブラリのSymPyをインストールすると、方程式を解いたり、式の展開、因数分解、微分積分ができる。

import sympy

x = sympy.Symbol('x')
y = sympy.Symbol('y')

print(type(x))
# <class 'sympy.core.symbol.Symbol'>
print(sympy.factor(x**3 - x**2 - 3 * x + 3))
# (x - 1)*(x**2 - 3)

print(sympy.factor(x * y + x + y + 1))
# (x + 1)*(y + 1)

行列: NumPy

NumPyをインストールすると行列の演算が可能。

arr1 = np.arange(4).reshape((2, 2))

print(arr1)
# [[0 1]
#  [2 3]]

arr2 = np.arange(6).reshape((2, 3))

print(arr2)
# [[0 1 2]
#  [3 4 5]]

arr_mul_matrix = np.dot(arr1, arr2)

print(arr_mul_matrix)
# [[ 3  4  5]
#  [ 9 14 19]]

関連カテゴリー

関連記事