-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP12.cs
53 lines (47 loc) · 1.73 KB
/
P12.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.Collections.Generic;
namespace P1 {
class Program {
// First, I need to know which numbers are the triangle numbers!
// Second, I need to know the factors, and the # of factors, for each triangle number
public static int GetPyramidNum(int limit)
{
int pyramidNum = 0;
for (int i = 1; i <= limit; i++)
{
pyramidNum += i;
}
return pyramidNum;
}
public static int GetNumOfFactors(int num) {
// Credit goes to: https://codereview.stackexchange.com/questions/237416/c-code-to-find-all-divisors-of-an-integer
List<int> divisors = new List<int>();
for (int i = 1; i <= Math.Sqrt(num); i++)
{
if (num % i == 0)
{
divisors.Add(i);
if (i != num / i)
{
divisors.Add(num / i);
}
}
}
return divisors.Count;
}
public static void Main(string[] args) {
int limitOfDivisors = 500;
int numOfDivisors = 0;
int pyramidNum = 0;
int counter = 0;
while (numOfDivisors < limitOfDivisors)
{
counter++;
pyramidNum = GetPyramidNum(counter);
numOfDivisors = GetNumOfFactors(pyramidNum);
//Console.WriteLine($"Counter: {counter}, Pyramid #: {pyramidNum}, Num of Divisors: {numOfDivisors}");
}
Console.WriteLine($"The first triangular number to have over five hundred divisors is: {pyramidNum:n0}.");
}
}
}