#include<iostream>
#include<math.h>
using namespace std;
long int generateTriangleNum(int);
long int factorsTriangleNum(int);
int main(){
int factors;
// cout<<"Enter no. of factors"<<endl;
// cin>>factors;
cout<<"The Triangle number is :"<<factorsTriangleNum(500)<<endl;
return 0;
}
long int generateTriangleNum(int n){
long int i, sum;
sum=0;
for(i=1;i<=n;i++)
{
sum+=i;
}
return sum;
}
long int factorsTriangleNum(int factors){
long int i, n, count, triangleNum, sqRoot;
int condition;
condition = 1;
n = 0;
while(condition){
n++;
triangleNum = generateTriangleNum(n);
count = 0;
sqRoot = sqrt(triangleNum);
for(i=1; i<=sqRoot; i++){
if(triangleNum%i == 0){
count++;
if(triangleNum/i > sqRoot){
count++;
}
}
}
if(count>factors){
condition = 0;
}
}
return triangleNum;
}
I2luY2x1ZGU8aW9zdHJlYW0+CiNpbmNsdWRlPG1hdGguaD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgpsb25nIGludCBnZW5lcmF0ZVRyaWFuZ2xlTnVtKGludCk7CmxvbmcgaW50IGZhY3RvcnNUcmlhbmdsZU51bShpbnQpOwoKaW50IG1haW4oKXsKICAgIGludCBmYWN0b3JzOwogICAgLy8gY291dDw8IkVudGVyIG5vLiBvZiBmYWN0b3JzIjw8ZW5kbDsKICAgIC8vIGNpbj4+ZmFjdG9yczsKICAgIGNvdXQ8PCJUaGUgVHJpYW5nbGUgbnVtYmVyIGlzIDoiPDxmYWN0b3JzVHJpYW5nbGVOdW0oNTAwKTw8ZW5kbDsKICAgIHJldHVybiAwOwp9Cgpsb25nIGludCBnZW5lcmF0ZVRyaWFuZ2xlTnVtKGludCBuKXsKICAgIGxvbmcgaW50IGksIHN1bTsKICAgIHN1bT0wOwogICAgCiAgICBmb3IoaT0xO2k8PW47aSsrKQogICAgewogICAgICAgIHN1bSs9aTsKICAgICAgICB9CgogICAgcmV0dXJuIHN1bTsKfQoKbG9uZyBpbnQgZmFjdG9yc1RyaWFuZ2xlTnVtKGludCBmYWN0b3JzKXsKICAgIGxvbmcgaW50IGksIG4sIGNvdW50LCB0cmlhbmdsZU51bSwgc3FSb290OwogICAgaW50IGNvbmRpdGlvbjsKICAgIGNvbmRpdGlvbiA9IDE7CiAgICBuID0gMDsKCiAgICB3aGlsZShjb25kaXRpb24pewogICAgICAgIG4rKzsKICAgICAgICB0cmlhbmdsZU51bSA9IGdlbmVyYXRlVHJpYW5nbGVOdW0obik7CiAgICAgICAgY291bnQgPSAwOwogICAgICAgIHNxUm9vdCA9IHNxcnQodHJpYW5nbGVOdW0pOwogICAgICAgIGZvcihpPTE7IGk8PXNxUm9vdDsgaSsrKXsKICAgICAgICAgICAgaWYodHJpYW5nbGVOdW0laSA9PSAwKXsKICAgICAgICAgICAgICAgIGNvdW50Kys7CiAgICAgICAgICAgICAgICBpZih0cmlhbmdsZU51bS9pID4gc3FSb290KXsKICAgICAgICAgICAgICAgICAgICBjb3VudCsrOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9ICAgIAogICAgICAgIH0KICAgICAgICBpZihjb3VudD5mYWN0b3JzKXsKICAgICAgICAgICAgY29uZGl0aW9uID0gMDsKICAgICAgICB9CiAgICB9CiAgICByZXR1cm4gdHJpYW5nbGVOdW07Cn0=