Check if a Number is a Tetracontagon Number

Updated

Given a positive integer N, find out whether N is a tetracontagonal number (also called a tetracontagon number): one of the numbers produced by 40-sided polygons.

What is a tetracontagonal number?

Polygonal (figurate) numbers count the dots needed to draw a growing family of regular polygons that share one corner, where each new polygon has one more dot on every side. A tetracontagon has 40 sides, so the tetracontagonal numbers are the polygonal numbers with s = 40.

For a polygon with s sides, the k-th polygonal number is

P(s, k) = ((s − 2)k² − (s − 4)k) / 2

With s = 40 this becomes

T(k) = (38k² − 36k) / 2 = 19k² − 18k

so the sequence starts 1, 40, 117, 232, 385, …

Examples:

Input: N = 40
Output: Yes
Explanation: T(2) = 19 × 4 − 18 × 2 = 40.

Input: N = 35
Output: No
Explanation: T(1) = 1 and T(2) = 40, and no term lies between them.

Approach

N is tetracontagonal exactly when 19k² − 18k = N has a positive whole-number solution k. Solving this quadratic for k gives

k = (18 + √(76N + 324)) / 38

So two checks are enough, both with whole numbers only:

  1. 76N + 324 is a perfect square, say d².
  2. 18 + d is divisible by 38. Then k = (18 + d) / 38.

Working with integers avoids the rounding errors that a floating-point square root can cause for large N.

// Check whether N is a tetracontagonal (40-gonal) number.
#include <cmath>
#include <iostream>
using namespace std;

bool isTetracontagonal(long long n)
{
    if (n < 1) return false;
    long long disc = 76 * n + 324;          // must be a perfect square
    long long d = llround(sqrt((long double)disc));
    while (d * d > disc) --d;               // correct any rounding
    while ((d + 1) * (d + 1) <= disc) ++d;
    return d * d == disc && (18 + d) % 38 == 0;
}

int main()
{
    for (long long n : {40LL, 35LL, 117LL, 385LL}) {
        cout << n << (isTetracontagonal(n) ? " is" : " is not")
             << " a tetracontagonal number\n";
    }
    return 0;
}

Output:

40 is a tetracontagonal number
35 is not a tetracontagonal number
117 is a tetracontagonal number
385 is a tetracontagonal number

The same check in Python, where math.isqrt gives an exact integer square root:

import math

def is_tetracontagonal(n: int) -> bool:
    if n < 1:
        return False
    disc = 76 * n + 324
    d = math.isqrt(disc)
    return d * d == disc and (18 + d) % 38 == 0

for n in (40, 35, 117, 385):
    print(n, "is" if is_tetracontagonal(n) else "is not", "a tetracontagonal number")

Complexity: O(1) arithmetic per number (one integer square root) and O(1) extra space.

Comment
Next tutorial
Mithlesh Upadhyay Published 1 Dec 2024 · 3 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Related tutorials