4. 課題解答例¶

課題1¶

なし

課題2¶

In [1]:
def compare(m, n):
    "mとnを比較する"
    if m == n:
        print(m, 'is equal to', n)
    elif m > n:
        print(m, 'is larger than', n)
    elif m < n:
        print(m, 'is smaller than', n)
In [2]:
compare(2, 1)
compare(1, 2)
compare(3, 3)
2 is larger than 1
1 is smaller than 2
3 is equal to 3

課題3¶

In [3]:
def is_leapyear(year):
    "うるう年の判定"
    if year%400 == 0:
        return True
    elif year%100 == 0:
        return False
    elif year%4 == 0:
        return True
In [4]:
is_leapyear(2000)
Out[4]:
True

課題4¶

In [5]:
import math

def trigonometric(n):
    "θ, sinθ, cosθの値を出力する"
    for i in range(n+1):
        x = 180.0 * i / n
        y1 = math.sin(x * math.pi/180.0)
        y2 = math.cos(x * math.pi/180.0)
        print('{:+12.3e} {:+12.3e} {:+12.3e}'.format(x, y1, y2))
In [6]:
trigonometric(18)
  +0.000e+00   +0.000e+00   +1.000e+00
  +1.000e+01   +1.736e-01   +9.848e-01
  +2.000e+01   +3.420e-01   +9.397e-01
  +3.000e+01   +5.000e-01   +8.660e-01
  +4.000e+01   +6.428e-01   +7.660e-01
  +5.000e+01   +7.660e-01   +6.428e-01
  +6.000e+01   +8.660e-01   +5.000e-01
  +7.000e+01   +9.397e-01   +3.420e-01
  +8.000e+01   +9.848e-01   +1.736e-01
  +9.000e+01   +1.000e+00   +6.123e-17
  +1.000e+02   +9.848e-01   -1.736e-01
  +1.100e+02   +9.397e-01   -3.420e-01
  +1.200e+02   +8.660e-01   -5.000e-01
  +1.300e+02   +7.660e-01   -6.428e-01
  +1.400e+02   +6.428e-01   -7.660e-01
  +1.500e+02   +5.000e-01   -8.660e-01
  +1.600e+02   +3.420e-01   -9.397e-01
  +1.700e+02   +1.736e-01   -9.848e-01
  +1.800e+02   +1.225e-16   -1.000e+00

課題5¶

In [7]:
def gcd(m, n):
    "mとnの最大公倍数を求める"
    if m < n:
        r = n
        n = m
        m = r    
    r = m%n
    while r != 0:
        m = n
        n = r
        r = m%n
    return n
In [8]:
gcd(12, 20)
Out[8]:
4

課題6¶

In [9]:
def approx_e(n, epsilon):
    "自然対数の底を級数展開を用いて求める"
    if n <= 1:
        print('Error : N > 1 is not satisfied')
        return None, None, None
    if epsilon < 0 or epsilon > 1:
        print('Error : 0 < epsilon < 1 is not satisfied')
        return None, None, None

    iteration = n
    status = False
    c = 1.0
    e = 1.0
    for i in range(n):
        c /= (i+1)
        e += c
        if abs(e - math.e)/math.e < epsilon:
            status = True
            iteration = i
            break
    return status, iteration, e
In [10]:
status, iteration, e = approx_e(10, 1.0e-8)

if status:
    print('Converged !')
else:
    print('Did not converge !')

print('{:20} : {:>20}'.format('N', iteration))
print('{:20} : {:>20.14e}'.format('Approximated', e))
print('{:20} : {:>20.14e}'.format('Exact', math.e))
print('{:20} : {:>20.14e}'.format('Error', abs(e-math.e)/math.e))
Did not converge !
N                    :                   10
Approximated         : 2.71828180114638e+00
Exact                : 2.71828182845905e+00
Error                : 1.00477663102111e-08

課題7¶

In [11]:
def approx_sqrt(a, epsilon):
    "aの平方根の近似値を求める"
    x = a
    err = 1.0
    while err > epsilon:
        dx = 0.5*(a/x - x)
        err = abs(dx/x)
        x += dx
    return x
In [12]:
sqrta1 = approx_sqrt(2.0, 1.0e-5)
sqrta2 = math.sqrt(2.0)

print('{:20} : {:>20.14e}'.format('Approximated', sqrta1))
print('{:20} : {:>20.14e}'.format('Exact', sqrta2))
Approximated         : 1.41421356237469e+00
Exact                : 1.41421356237310e+00
In [ ]: