Thread: Triangular number

  1. #1
    Registered User
    Join Date
    Jun 2019
    Posts
    33

    Triangular number

    Triangular number is the amount of point that can fill equilateral triangle. For example number 6 is triangular number because all sides of triangle has the same amount of point. The problem for this task is to solve equation T(n) = n * (n + 1) / 2. t(n) is given we are searching n. My code below
    Code:
    #include <stdbool.h>
    
    
    bool is_triangular(int t) {
    unsigned long long int delta = 1+8*t;  
    double x=(-1+sqrt(delta))/2.0;
        if(floor(x)==x)
          return true;
    return false;
    }

    It gives me proer solutions except t==458000245 and for t==453351216. I don't know where is the problem.

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    int is 32 bits long (UINT_MAX = 4294967295)
    long long is 64 bits long
    double is 53. Try:

    Code:
    bool is_triangular( unsigned long long t )
    {
      unsigned long long delta = 1ULL+8ULL*t;
      long double x = (-1.0L + sqrtl(delta)) / 2.0L;
      return floorl( x ) == x;
    }

  3. #3
    Registered User
    Join Date
    Jun 2019
    Posts
    33

    Problem solved

    Thank you for answer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. while loop, triangular number
    By _jamie in forum C Programming
    Replies: 2
    Last Post: 02-08-2019, 05:01 PM
  2. heat transfer from triangular fins
    By hemanth342 in forum C Programming
    Replies: 6
    Last Post: 01-30-2018, 10:16 AM
  3. Triangular number?
    By akaile in forum C++ Programming
    Replies: 2
    Last Post: 09-21-2009, 04:10 AM
  4. triangular collision detection
    By ichijoji in forum Game Programming
    Replies: 1
    Last Post: 03-20-2003, 07:48 PM
  5. newbie- need help with triangular #
    By DirtElk in forum C Programming
    Replies: 8
    Last Post: 02-15-2002, 10:14 AM

Tags for this Thread