Differences between revisions 5 and 10 (spanning 5 versions)
Revision 5 as of 2018-10-29 05:26:35
Size: 904
Comment:
Revision 10 as of 2019-10-16 14:41:11
Size: 955
Comment:
Deletions are marked like this. Additions are marked like this.
Line 10: Line 10:
  * 始点と終点を引数にとる.終点を含む
Line 14: Line 15:
    for i in range( start, end ):     for i in range( start, end + 1 ):
Line 27: Line 28:
    print( sum_of_range( 1, x + 1 ) )     print( sum_of_range( 1, x ) )
Line 33: Line 34:
    print( sum_of_range( 1, (10 ** i) + 1 ) )     print( sum_of_range( 1, 10 ** i ) )

[ edit ]

  • 解答は基本的に,下記 5 行の関数です.
    • 始点と終点を引数にとる.終点を含む

def sum_of_range( start, end ):
    sum = 0
    for i in range( start, end + 1 ):
        sum += i
    return( sum )
  • 【別解】
    • 以下の部分は,当然ですが,べき乗で表現することもできます.

# ----- 計算と結果の表示 ----
x = 1
for i in range( 5 ):
    x *= 10
    print( sum_of_range( 1, x ) )

# ----- 計算と結果の表示 ----
for i in range( 1, 5 ):
    print( sum_of_range( 1, 10 ** i ) )

SoftEng: Python/Prog/Practice/Extended/02/EP_001a/Digest (last edited 2019-10-16 14:41:11 by TadashiIijima)