计算三角形的行和
#解法1: def row_sum_numbers(n): return sum(range(n*(n-1)+1,n*(n+1),2)) #根据给定的行数,算出第一个数字3,和最后一个数字+1 6 def row_sum_numbers2(n): return n**3 # print(row_sum_numbers(3)) # print(row_sum_numbers2(3)) ''' 给定连续奇数的三角形: 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 … 从行索引(从索引1开始)计算这个三角形的行和 ''' def row_sum_numbers3(n): return sum(range(n*(n-1)+2,n*(n+1)+1,2)) # print(row_sum_numbers3(3)) def getnum(num): rlt=[] for n in range(1,num): for j in range(2,n): if n%j==0: break else: rlt.append(str(n)) print(rlt,','.join(rlt)) getnum(10)