Your task is to return the sum of Triangular Numbers up-to-and-including the `nth` Triangular Number.

Triangular Number: "any of the series of numbers (1, 3, 6, 10, 15, etc.) obtained by continued summation of the natural numbers 1, 2, 3, 4, 5, etc."

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

e.g. If `4` is given: `1 + 3 + 6 + 10 = 20`.

Triangular Numbers cannot be negative so return 0 if a negative number is given.

Source: codewars | Difficulty: EASY
Tags: Fundamentals

Mathematics

Starter code: def sum_triangular_numbers(n):
	

Solutions:
def sum_triangular_numbers(n):
	return n * (n + 1) * (n + 2) / 6 if n > 0 else 0


def sum_triangular_numbers(n):
	return sum((sum(range(x + 1)) for x in range(n + 1)))


def sum_triangular_numbers(n):
	(total, a) = (0, 0)
	for i in range(n):
		a += i + 1
		total += a
	return total


def sum_triangular_numbers(n):
	if n <= 0:
		return 0
	(x, out) = (1, [1])
	while n - 1 >= x:
		x += 1
		out.append(out[-1] + x)
	return sum(out)


def sum_triangular_numbers(n):
	return n > 0 and (n ** 3 + 3 * n ** 2 + 2 * n) / 6


def sum_triangular_numbers(n):
	return n > 0 and (n + 1) * (n + 2) * n / 6


def sum_triangular_numbers(n):
	return sum((i * 0.5 * (i + 1) for i in range(n + 1)))


from itertools import accumulate
sum_triangular_numbers = lambda _: sum(accumulate(range(0, _ + 1)))


def sum_triangular_numbers(n):
	step = 0
	triangulars = [0]
	while n > len(triangulars) - 1:
		step += 1
		triangulars.append(triangulars[-1] + step)
	return sum(triangulars)


def sum_triangular_numbers(n):
	if n > 0:
		return sum([x * (x + 1) / 2 for x in range(n + 1)])
	else:
		return 0


sum_triangular_numbers = lambda n: n * (n + 1) * (n + 2) // 6 if n > 0 else 0


def sum_triangular_numbers(n):
	total = 0
	x = 0
	for i in range(1, n + 1):
		x += i
		total += x
	return total


def sum_triangular_numbers(n):
	return sum([(n * n + n) / 2 for n in range(1, n + 1)])


def sum_triangular_numbers(n):
	x = 1
	y = 2
	z = []
	while len(z) < n:
		z.append(x)
		x += y
		y += 1
	return sum(z)


def sum_triangular_numbers(n):
	return sum((i * (i + 1) // 2 for i in range(n + 1)))

def sum_triangular_numbers(n):
	return sum((i * (n - i + 1) for i in range(n + 1)))

def sum_triangular_numbers(n):
	return max(0, n * (n + 1) * (n + 2) // 6)

def sum_triangular_numbers(n):
	return 0 if n < 1 else n * (n + 1) * (n + 2) // 6


def sum_triangular_numbers(n):
	return sum((sum(range(i + 1)) for i in range(n + 1)))


sum_triangular_numbers = lambda n: n > 0 and -~n * n * (n + 2) / 6


def sum_triangular_numbers(n):
	return [0, (n ** 3 - n) / 6 + 0.5 * n * (n + 1)][n > 0]


def sum_triangular_numbers(n):
	return 0 if n <= 0 else sum([i for i in range(1, n + 1)]) + sum_triangular_numbers(n - 1)


def sum_triangular_numbers(n):
	sum = 0
	for i in range(n):
		sum += (i + 1) * (i + 2) / 2
	return sum


def sum_triangular_numbers(n):
	if n <= 0:
		return 0
	return round(1 / 6 * n ** 3 + 1 / 2 * n ** 2 + 1 / 3 * n, 0)


def sum_triangular_numbers(n):
	sum_ = 0
	for i in range(1, n + 1):
		sum_ += sum(range(1, i + 1))
	return sum_


def sum_triangular_numbers(n):
	sum_ = 0
	for i in range(1, n + 1):
		sum_ += (i ** 2 + i) // 2
	return sum_


def sum_triangular_numbers(n):
	increment = 1
	sum_ = 0
	number = 0
	while increment <= n:
		number += increment
		sum_ += number
		increment += 1
	return sum_


def sum_triangular_numbers(n, c=1, v=2):
	return c + sum_triangular_numbers(n - 1, c + v, v + 1) if n > 0 else 0


def sum_triangular_numbers(n):
	new_num = 0
	new_num_list = []
	for num in range(1, n + 1):
		new_num += num
		new_num_list.append(new_num)
		print(new_num)
	return sum(new_num_list)
print(sum_triangular_numbers(6))


def sum_triangular_numbers(n):
	return max(n * (n + 1) * (n + 2) // 6, 0)


def sum_triangular_numbers(n):
	if n <= 0:
		return 0
	total = 0
	for num in range(n + 1):
		triangle_num = num * (num + 1) / 2
		total = total + triangle_num
	return total


def sum_triangular_numbers(n):
	if n < 1:
		return 0
	else:
		triangular = []
		for i in range(1, n + 1):
			triangular.append(i * (i + 1) / 2)
		return int(sum(triangular))


def sum_triangular_numbers(n):
	(c, p, g) = (0, 0, 1)
	for loop in range(n + 1):
		c += p
		p += g
		g += 1
	return c


def sum_triangular_numbers(n):
	if n < 0:
		return 0
	elif n == 0:
		return 1
	else:
		a = [1]
		for i in range(1, n):
			a.append(a[i - 1] + i + 1)
		return sum(a)


def sum_triangular_numbers(n):
	if n > 0:
		listado_numeros = []
		while len(listado_numeros) < n:
			for i in range(1, n + 1):
				listado_numeros.append(i)
		for i in listado_numeros[1:]:
			listado_numeros[i - 1] = i + listado_numeros[i - 2]
		return sum(listado_numeros)
	else:
		return 0


def sum_triangular_numbers(n):
	if n <= 0:
		return 0
	if n == 1:
		return 1
	calc = 1
	inc = 2
	sm = 1
	for x in range(2, n + 1):
		calc = calc + inc
		sm = sm + calc
		inc = inc + 1
	return sm


def sum_triangular_numbers(n):
	return sum([round(a * (a + 1) / 2) for a in range(1, n + 1)])


def sum_triangular_numbers(n):
	numberz = 0
	triangular_numbers = []
	if n <= 0:
		return 0
	else:
		number = 0
		for int in range(0, n + 1):
			number = number + int
			triangular_numbers.append(number)
	for num in triangular_numbers:
		numberz = numberz + num
	return numberz


def sum_triangular_numbers(n):
	if n < 0:
		return 0
	a = list(range(1, n + 1))
	b = []
	for i in range(0, len(a)):
		d = a[i] + sum(a[0:i])
		b.append(d)
	s = sum(b)
	return s


def sum_triangular_numbers(n):
	if n <= 0:
		return 0
	s = 1
	i = 2
	t = 1
	x = 1
	while x < n:
		s += i
		i += 1
		x += 1
		t += s
	return t


def sum_triangular_numbers(n):
	sum = 0
	num = 0
	k = 1
	while n > 0:
		num += k
		sum += num
		k += 1
		n -= 1
	return sum


def sum_triangular_numbers(n):
	if n <= 0:
		return 0
	li = [1]
	for i in range(2, n + 1):
		li.append(li[-1] + i)
	return sum(li)


def sum_triangular_numbers(n):
	if n <= 0:
		return 0
	else:
		step = 1
		prvi = 0
		e = []
		while step < n + 1:
			prvi += step
			step += 1
			e.append(prvi)
		return sum(e)


def sum_triangular_numbers(n):
	(c, l) = (0, 0)
	for i in range(1, n + 1):
		l = l + i
		c += l
	return c


def sum_triangular_numbers(n):
	l = [0]
	for x in range(1, n + 1):
		l.append(x + l[-1])
	return sum(l)


def sum_triangular_numbers(n):
	t = 1
	s = 0
	for i in range(2, n + 2):
		s = s + t
		t = t + i
	return s


def sum_triangular_numbers(n):
	if n < 1:
		return 0
	if n == 1:
		return 1
	return n / 2 * (n + 1) + sum_triangular_numbers(n - 1)


def sum_triangular_numbers(n):
	if n < 1:
		return 0
	elif n == 1:
		return 1
	else:
		cont = 0
		num = 1
		max = 2
		result = 1
		while max <= n:
			num += 1
			cont += 1
			if cont == max:
				result += num
				max += 1
				cont = 0
		return result


def sum_triangular_numbers(n):
	total = 0
	for i in range(n + 1):
		total += sum(list(range(1, i + 1)))
	return total


def sum_triangular_numbers(n):
	(sum_, t_i) = (0, 0)
	for i in range(1, n + 1):
		t_i += i
		sum_ += t_i
	return sum_


def sum_triangular_numbers(n):
	d = 1
	s = 0
	res = 0
	for i in range(n):
		s += d
		res += s
		d += 1
	return res


def sum_triangular_numbers(n):
	s = 1
	x = []
	for i in range(2, n + 2):
		x.append(s)
		s += i
	return sum(x)


def sum_triangular_numbers(n):
	if n <= 0:
		return 0
	else:
		return (n ** 3 + 3 * n ** 2 + 2 * n) / 6


def sum_triangular_numbers(n):
	sum_list = [0]
	for i in range(1, n + 1):
		sum_list.append(sum_list[-1] + i)
	return sum(sum_list)


def sum_triangular_numbers(n):
	list = []
	count = 0
	for num in range(1, n + 1):
		count += num
		list.append(count)
	return sum(list)


def sum_triangular_numbers(n):
	val = 0
	return_list = []
	if n < 0:
		return 0
	for x in range(1, n + 1):
		val = val + x
		return_list.append(val)
	return sum(return_list)


def sum_triangular_numbers(n):
	num = 1
	interval = 2
	ls = list()
	while interval <= n:
		num = num + interval
		interval = interval + 1
		ls.append(num)
	if n < 0:
		return sum(ls)
	else:
		return sum(ls) + 1


def sum_triangular_numbers(n):
	if n > 0:
		z = 1
		x = []
		for i in range(2, n + 1):
			z = z + i
			x.append(z)
		return sum(x) + 1
	if n < 0:
		return 0


tri = lambda x: x * (x + 1) / 2

def sum_triangular_numbers(n):
	try:
		res = 0
		for i in range(1, n + 1):
			res += tri(i)
		return res
	except:
		return 0


def sum_triangular_numbers(n):
	if n <= 0:
		return 0
	else:
		x = 0
		for i in range(0, n + 1):
			x += i * (i + 1) / 2
		return int(x)


def sum_triangular_numbers(n):
	m = abs(n)
	if m == n:
		sum_ = 0
		while n > 0:
			sum_ += n * (n + 1) / 2
			n -= 1
	else:
		sum_ = 0
	return sum_


t = lambda x: x * (x + 1) // 2

def sum_triangular_numbers(n):
	return sum((t(x) for x in range(n + 1)))


def sum_triangular_numbers(n):
	s = a = 0
	for i in range(1, n + 1):
		a += i
		s += a
	return s


def sum_triangular_numbers(n):
	return sum([x * (x + 1) * 0.5 for x in range(1, n + 1)])


def sum_triangular_numbers(n):
	(addend, sum) = (0, 0)
	for step in range(1, n + 1):
		addend += step
		sum += addend
	return sum


def sum_triangular_numbers(n):
	if n <= 0:
		return 0
	(x, y) = (0, 0)
	sum = 0
	for i in range(n):
		x = x + 1
		y = x + y
		sum += y
	return sum


def sum_triangular_numbers(n):
	if n < 0:
		return 0
	(add, i, l) = (1, 0, [])
	while n != add - 1:
		i += add
		add += 1
		l.append(i)
	sum(l)
	return sum(l)


def sum_triangular_numbers(n):
	return sum((sum(range(i)) for i in range(n + 2)))


def sum_triangular_numbers(n):
	result = 0
	x = 0
	for y in range(n + 1):
		x += y
		result += x
	return result


def sum_triangular_numbers(n):
	count = 0
	sum = 0
	total = 0
	for index in range(n):
		count += 1
		sum += count
		total += sum
	return total


def sum_triangular_numbers(n):
	if n < 0:
		return 0
	else:
		a = 2
		lst = []
		n = n - 1
		while n > 0:
			lst.append(a)
			a += 1
			n -= 1
		print(lst)
		cnt = 1
		lst1 = [1]
		for i in range(len(lst)):
			cnt += lst[i]
			lst1.append(cnt)
		return sum(lst1)


def sum_triangular_numbers(n):
	if n <= 0:
		return 0
	result_sum = 0
	exponent = 2
	num = 1
	for _ in range(n - 1):
		num = num + exponent
		exponent += 1
		result_sum += num
	return result_sum + 1


def sum_triangular_numbers(n):
	return 0 if n < 0 else int(1 / 6 * n * (n + 1) * (n + 2) + 1e-05)