def solution(n: int) -> int:
sol_list = []
def num_generator(start_index, end_index):
yield [i for i in range(1, n ** 4, 2)][start_index: end_index]
index = 0
if n == 1:
sol_list = [1]
else:
for i in range(1, n+1):
sol_list = num_generator(index, index + i).__next__()
index = index + i
return sum(sol_list)
assert solution(1) == 1
assert solution(2) == 8
assert solution(13) == 2197
assert solution(19) == 6859
def solution(n: int) -> int:
num = 0
res = 0
# 计算一个有多少个元素
for i in range(1, n+1):
num += i
# 每个元素放入列表中
list1 = [(2*j+1) for j in range(num)]
# 确定第 n 行开始元素的 index 值
start = num - n
# 取出来进行相加
for t in range(start, num):
res += list1[t]
return res
assert solution(1) == 1
assert solution(2) == 8
assert solution(13) == 2197
assert solution(19) == 6859
public int solution3(int n) {
if (n == 1) {
return 1;
}
int res = 0;
int init = 1;
int m = n * (n - 1) / 2 + n;
for (int i = 1; i < m; i++) {
init += 2;
if (m - n <= i) {
res += init;
}
}
return res;
}
def solution(n: int) -> int:
# 计算n行元素总数
count = sum([i for i in range(1, n + 1)])
# 第n行索引 count - n, count
result = sum([2 * i + 1 for i in range(count - n, count)])
return result
assert solution(1) == 1
assert solution(2) == 8
assert solution(13) == 2197
assert solution(19) == 6859