【每日一题1117】三角边求和

:woman_mage: 已知一个三角形结构的数字结构,如下图:

[01]
02 [03]
04 05 [06]
07 08 09 [10]
11 12 13 14 [15]
16 17 18 19 20 [21]

三角形斜边上的带中括号的数字,即是我们需要关注的核心数字。请编写一个函数,接收指定的层数,请计算截至该层的所有斜边数字的总和。

示例:
输入:4,输出20。因为:1+3+6+10=20

题目难度:简单
题目来源:codewars: Sum of Triangular Numbers

def solution(n: int) -> int:
    # your code

assert solution(4) == 20
assert solution(6) == 56
assert solution(943) == 140205240

找规律

def solution(n:int):
    res=0
    for i in range(n):
        res=res+1/2*(i+1)*(i+2)
    return int(res)
1 个赞

def solution_1117(n:int)->int:
num = 0
sum_num = 0
for i in range(0, n):
num = num + (i + 1)
sum_num = sum_num + num
return sum_num

pass

def test_solution_1117():
assert solution_1117(4) == 20
assert solution_1117(0) == 0
assert solution_1117(1) == 1
assert solution_1117(943) == 140205240
pass

def solution(n: int) -> int:
    layer_num = 0
    num_list = []
    len_num = 0
    count = 0
    i = 1
    while True:
        num_list.append(i)
        if len(num_list) == len_num + 1:
            count += num_list[-1]
            len_num = len(num_list)
            num_list = []
            layer_num += 1
        if layer_num == n:
            break
        i += 1
    return count


assert solution(4) == 20
assert solution(6) == 56
assert solution(943) == 140205240
def solution(n: int):
    return sum([sum([i for i in range(1,num+1)]) for num in range(1,n+1)])

assert solution(4) == 20
assert solution(6) == 56
assert solution(943) == 140205240
   public int solution(int n){
        int res =0;
        for (int i = 0; i < n; i++) {
            res += 0.5*(i+1)*(i+2);
        }
        return res;
    }
def solution(n: int) -> int:
    s = 0
    s1 = 0
    list1 = []
    for i in range(1, n + 1):
        s += 1
        s1 += s
        list1.append(s1)
    return sum(list1)


assert solution(4) == 20
assert solution(6) == 56
assert solution(943) == 140205240
def solution2(n:int)->int:
    return sum(sum([i for i in range(1, i + 1)]) for i in range(1,n+1))

assert solution(4) == 20
assert solution(6) == 56
assert solution(943) == 140205240

1 个赞
def solution(n: int) -> int:
    result = 0
    while n > 0:
        result += feb(n)
        n -=1
    return result

def feb(n):
    if n ==1 : return 1
    elif n < 1: return 0
    else : return feb(n-1)+n
  def sum_tri(n: int) -> int:
        if n == 1:
            return 1
        for i in range(2,n+1):
           sum1 = sum_tri(i-1)+i
        return sum1
def solution(n: int) -> int:
    # your code
    num0 = []
    for i in range(1, n+1):
        num0.append(sum(j for j in range(1, i+1)))
    return sum(num0)


assert solution(4) == 20
assert solution(6) == 56
assert solution(943) == 140205240

def solution(n: int) -> int:
    result = 0
    for i in range(1, n + 1):
        result += sum([j for j in range(1, i + 1)])
    return result


assert solution(4) == 20
assert solution(6) == 56
assert solution(943) == 140205240
def solution(n: int) -> int:
    return sum(sum(range(1, i + 2)) for i in range(n)) #1 1+2 1+2+3 1+2+3+4


assert solution(4) == 20
assert solution(6) == 56
assert solution(943) == 140205240