Created
May 2, 2014 19:22
-
-
Save kashwaa/3dee820575d7bc15d201 to your computer and use it in GitHub Desktop.
This file contains 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Diagnostics; | |
namespace euler12 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Stopwatch sw = new Stopwatch(); | |
sw.Start(); | |
Console.WriteLine(calc(500)); | |
sw.Stop(); | |
Console.WriteLine("calculated in {0}",sw.Elapsed.TotalSeconds); | |
} | |
public static UInt64 GetNthTriangularNum(UInt64 n) | |
{ | |
return n*(n+1)/2; | |
} | |
public static int GetNumberOfDivisors(UInt64 n) | |
{ | |
int result = 2; | |
UInt64 start = 2; | |
UInt64 end = n; | |
for (UInt64 i = start; i < end; i+=1) | |
{ | |
if (n%i==0) | |
{ | |
start = i; | |
end = n / i; | |
result+=start==end?1:2; | |
} | |
} | |
return result; | |
} | |
public static UInt64 calc(int max) | |
{ | |
UInt64 i = 1; | |
while (GetNumberOfDivisors( GetNthTriangularNum(i))<max) | |
{ | |
i++; | |
} | |
return GetNthTriangularNum(i); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment