fork download
  1. #include<iostream>
  2. #include<math.h>
  3.  
  4. using namespace std;
  5.  
  6. long int generateTriangleNum(int);
  7. long int factorsTriangleNum(int);
  8.  
  9. int main(){
  10. int factors;
  11. // cout<<"Enter no. of factors"<<endl;
  12. // cin>>factors;
  13. cout<<"The Triangle number is :"<<factorsTriangleNum(500)<<endl;
  14. return 0;
  15. }
  16.  
  17. long int generateTriangleNum(int n){
  18. long int i, sum;
  19. sum=0;
  20.  
  21. for(i=1;i<=n;i++)
  22. {
  23. sum+=i;
  24. }
  25.  
  26. return sum;
  27. }
  28.  
  29. long int factorsTriangleNum(int factors){
  30. long int i, n, count, triangleNum, sqRoot;
  31. int condition;
  32. condition = 1;
  33. n = 0;
  34.  
  35. while(condition){
  36. n++;
  37. triangleNum = generateTriangleNum(n);
  38. count = 0;
  39. sqRoot = sqrt(triangleNum);
  40. for(i=1; i<=sqRoot; i++){
  41. if(triangleNum%i == 0){
  42. count++;
  43. if(triangleNum/i > sqRoot){
  44. count++;
  45. }
  46. }
  47. }
  48. if(count>factors){
  49. condition = 0;
  50. }
  51. }
  52. return triangleNum;
  53. }
Success #stdin #stdout 0.5s 5280KB
stdin
Standard input is empty
stdout
The Triangle number is :76576500