Latest web development tutorials

Pythonは演習76と、

Pythonの100リー Pythonの100リー

タイトル:、入力nは偶数であることを関数を書く+ 1/2 + 1/4機能評価を呼び出し、... + 1 / nの入力nが奇数の場合、関数は+ 1/1 + 1/3と呼ばれています.. 。+ 1 / n個(ポインタ関数を使用します)

プログラム解析:なし。

ソースコード:

#!/usr/bin/python
# -*- coding: UTF-8 -*-

def peven(n):
    i = 0
    s = 0.0
    for i in range(2,n + 1,2):
        s += 1.0 / i
    return s

def podd(n):
    s = 0.0
    for i in range(1, n + 1,2):
        s += 1 / i
    return s

def dcall(fp,n):
    s = fp(n)
    return s

if __name__ == '__main__':
    n = int(raw_input('input a number:\n'))
    if n % 2 == 0:
        sum = dcall(peven,n)
    else:
        sum = dcall(podd,n)
    print sum

上の例の出力は、次のとおりです。

input a number:
6
0.916666666667

Pythonの100リー Pythonの100リー