find the nth triangular number in python

You can find the nth triangular number by using the formula n * (n + 1) / 2. Here's how you can implement this in Python:

main.py
def nth_triangular_number(n):
    return n * (n + 1) // 2

# Example usage
print(nth_triangular_number(5))  # Output: 15
121 chars
6 lines

In this code, // is used instead of / to ensure that the result of the division is an integer, as triangular numbers are usually defined as integers.

gistlibby LogSnag