Fastest method to define whether a number is a triangular number

后端 未结 8 963
失恋的感觉
失恋的感觉 2020-12-14 12:18

A triangular number is the sum of the n natural numbers from 1 to n. What is the fastest method to find whether a given positive integer number is a triangular one?

He

8条回答
  •  一向
    一向 (楼主)
    2020-12-14 12:46

    Home work ?

    Sum of numbers from 1 to N

    1 + 2 + 3 + 4 + ... n-1 + n

    if you add the first plus last, and then the second plus the second from last, then ...

    = (1+n) + (2+n-1) + (3+n-2) + (4+n-3) + ... (n/2 + n/2+1)

    = (n=1) + (n+1) + (n+1) + ... (n+1); .... n/2 times

    = n(n+1)/2

    This should get you started ...

提交回复
热议问题