Skip to content

Instantly share code, notes, and snippets.

@kashwaa
Created May 2, 2014 19:22
Show Gist options
  • Save kashwaa/3dee820575d7bc15d201 to your computer and use it in GitHub Desktop.
Save kashwaa/3dee820575d7bc15d201 to your computer and use it in GitHub Desktop.
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