Problem link : https://projecteuler.net/problem=12
Highly divisible triangular number
Problem 12
The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:
What is the value of the first triangle number to have over five hundred divisors?
Solution :
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
Let us list the factors of the first seven triangle numbers:1: 1We can see that 28 is the first triangle number to have over five divisors.
3: 1,3
6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28
What is the value of the first triangle number to have over five hundred divisors?
Solution :
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
int main() | |
{ | |
int sum=0,counter=1,i,num; | |
while(1){ | |
sum=sum+counter; | |
counter++; // generate triangle number | |
num=0; | |
i=1; | |
while(i*i<=sum){ | |
if(sum%i==0){ | |
num=num+2; | |
} | |
i++; | |
} | |
if(num>=500) | |
break; | |
} | |
printf("%d",sum); | |
} // end main | |