“Numer trójkątny Python” Kod odpowiedzi

Numer trójkątny Python

def triangular_number(n):
    return n * (n + 1) // 2  # Convert to int for Python 2
Travinth Dayalaeaswaran

Numer trójkątny Python

polygonal(n, sides=3)
Travinth Dayalaeaswaran

Numer trójkątny Python

def triangular_number(n):
    i = n
    while True:
        if i == 1:
            return n
        i -= 1
        n += i
Travinth Dayalaeaswaran

Numer trójkątny Python

def triangular_number(n):
    return sum(range(n + 1))
Travinth Dayalaeaswaran

Numer trójkątny Python

def polygonal(n, sides):
    return (n**2*(sides-2) - n*(sides-4)) // 2

def triangular(n):
    return polygonal(n, 3)
Travinth Dayalaeaswaran

Numer trójkątny Python

def triangular_number(n):
    for i in range(n):  # range(3) is a generator for [0, 1, 2]
        n += i
    return n
Travinth Dayalaeaswaran

Odpowiedzi podobne do “Numer trójkątny Python”

Pytania podobne do “Numer trójkątny Python”

Więcej pokrewnych odpowiedzi na “Numer trójkątny Python” w Python

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu