2022年9月27日 星期二

Python b558: 求數列第 n 項

b558: 求數列第 n 項


有一個數列,第一項的值為 1,第二項的值為第一項加 1,第三項的值為第二項加 2,第四項的值為第三項加 3 ... 第 k 項的值為第 k-1 項的值加上 k-1。

給一個數字 n,請印出這個數列的第 n 項。

 


#---------------------------------------------

print('-------b558: 求數列第 n 項-------')

n=0

b=[1]

for a in range(501):

    b.append(b[a]+a+1)

print (b)

while True:

    try:

        n=int(input('請輸入數字 n ,1<=n<=500 -->'))

        

        print("數列的第",n,"項的數值 = ",b[n-1]);

    except:

        break




>>> %Run -c $EDITOR_CONTENT

-------b558: 求數列第 n 項-------

[1, 2, 4, 7, 11, 16, 22, 29, 37, 46, 56, 67, 79, 92, 106, 121, 137, 154, 172, 191, 211, 232, 254, 277, 301, 326, 352, 379, 407, 436, 466, 497, 529, 562, 596, 631, 667, 704, 742, 781, 821, 862, 904, 947, 991, 1036, 1082, 1129, 1177, 1226, 1276, 1327, 1379, 1432, 1486, 1541, 1597, 1654, 1712, 1771, 1831, 1892, 1954, 2017, 2081, 2146, 2212, 2279, 2347, 2416, 2486, 2557, 2629, 2702, 2776, 2851, 2927, 3004, 3082, 3161, 3241, 3322, 3404, 3487, 3571, 3656, 3742, 3829, 3917, 4006, 4096, 4187, 4279, 4372, 4466, 4561, 4657, 4754, 4852, 4951, 5051, 5152, 5254, 5357, 5461, 5566, 5672, 5779, 5887, 5996, 6106, 6217, 6329, 6442, 6556, 6671, 6787, 6904, 7022, 7141, 7261, 7382, 7504, 7627, 7751, 7876, 8002, 8129, 8257, 8386, 8516, 8647, 8779, 8912, 9046, 9181, 9317, 9454, 9592, 9731, 9871, 10012, 10154, 10297, 10441, 10586, 10732, 10879, 11027, 11176, 11326, 11477, 11629, 11782, 11936, 12091, 12247, 12404, 12562, 12721, 12881, 13042, 13204, 13367, 13531, 13696, 13862, 14029, 14197, 14366, 14536, 14707, 14879, 15052, 15226, 15401, 15577, 15754, 15932, 16111, 16291, 16472, 16654, 16837, 17021, 17206, 17392, 17579, 17767, 17956, 18146, 18337, 18529, 18722, 18916, 19111, 19307, 19504, 19702, 19901, 20101, 20302, 20504, 20707, 20911, 21116, 21322, 21529, 21737, 21946, 22156, 22367, 22579, 22792, 23006, 23221, 23437, 23654, 23872, 24091, 24311, 24532, 24754, 24977, 25201, 25426, 25652, 25879, 26107, 26336, 26566, 26797, 27029, 27262, 27496, 27731, 27967, 28204, 28442, 28681, 28921, 29162, 29404, 29647, 29891, 30136, 30382, 30629, 30877, 31126, 31376, 31627, 31879, 32132, 32386, 32641, 32897, 33154, 33412, 33671, 33931, 34192, 34454, 34717, 34981, 35246, 35512, 35779, 36047, 36316, 36586, 36857, 37129, 37402, 37676, 37951, 38227, 38504, 38782, 39061, 39341, 39622, 39904, 40187, 40471, 40756, 41042, 41329, 41617, 41906, 42196, 42487, 42779, 43072, 43366, 43661, 43957, 44254, 44552, 44851, 45151, 45452, 45754, 46057, 46361, 46666, 46972, 47279, 47587, 47896, 48206, 48517, 48829, 49142, 49456, 49771, 50087, 50404, 50722, 51041, 51361, 51682, 52004, 52327, 52651, 52976, 53302, 53629, 53957, 54286, 54616, 54947, 55279, 55612, 55946, 56281, 56617, 56954, 57292, 57631, 57971, 58312, 58654, 58997, 59341, 59686, 60032, 60379, 60727, 61076, 61426, 61777, 62129, 62482, 62836, 63191, 63547, 63904, 64262, 64621, 64981, 65342, 65704, 66067, 66431, 66796, 67162, 67529, 67897, 68266, 68636, 69007, 69379, 69752, 70126, 70501, 70877, 71254, 71632, 72011, 72391, 72772, 73154, 73537, 73921, 74306, 74692, 75079, 75467, 75856, 76246, 76637, 77029, 77422, 77816, 78211, 78607, 79004, 79402, 79801, 80201, 80602, 81004, 81407, 81811, 82216, 82622, 83029, 83437, 83846, 84256, 84667, 85079, 85492, 85906, 86321, 86737, 87154, 87572, 87991, 88411, 88832, 89254, 89677, 90101, 90526, 90952, 91379, 91807, 92236, 92666, 93097, 93529, 93962, 94396, 94831, 95267, 95704, 96142, 96581, 97021, 97462, 97904, 98347, 98791, 99236, 99682, 100129, 100577, 101026, 101476, 101927, 102379, 102832, 103286, 103741, 104197, 104654, 105112, 105571, 106031, 106492, 106954, 107417, 107881, 108346, 108812, 109279, 109747, 110216, 110686, 111157, 111629, 112102, 112576, 113051, 113527, 114004, 114482, 114961, 115441, 115922, 116404, 116887, 117371, 117856, 118342, 118829, 119317, 119806, 120296, 120787, 121279, 121772, 122266, 122761, 123257, 123754, 124252, 124751, 125251, 125752]



請輸入數字 n ,1<=n<=500 -->10

數列的第 10 項的數值 =  46

請輸入數字 n ,1<=n<=500 -->4

數列的第 4 項的數值 =  7

請輸入數字 n ,1<=n<=500 -->1

數列的第 1 項的數值 =  1

請輸入數字 n ,1<=n<=500 -->12

數列的第 12 項的數值 =  67

請輸入數字 n ,1<=n<=500 -->

>>> 

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...