Created
February 16, 2011 03:29
-
-
Save clementi/828811 to your computer and use it in GitHub Desktop.
Project Euler Problem #45 Solution
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
public static void Main(string[] args) | |
{ | |
for (var n = 286L; ; n++) | |
{ | |
var triangleNumber = TriangleNumber(n); | |
if (IsPentagonal(triangleNumber) && IsHexagonal(triangleNumber)) | |
{ | |
Console.WriteLine(triangleNumber); | |
break; | |
} | |
} | |
Console.ReadKey(); | |
} | |
public static bool IsPentagonal(long triangleNumber) | |
{ | |
var pentagonal = 0L; | |
for (var n = 1L; pentagonal <= triangleNumber; n++) | |
{ | |
pentagonal = n * (3 * n - 1) / 2; | |
if (pentagonal == triangleNumber) | |
return true; | |
} | |
return false; | |
} | |
public static bool IsHexagonal(long triangleNumber) | |
{ | |
var hexagonal = 0L; | |
for (var n = 1L; hexagonal <= triangleNumber; n++) | |
{ | |
hexagonal = n * (2 * n - 1); | |
if (hexagonal == triangleNumber) | |
return true; | |
} | |
return false; | |
} | |
private static long TriangleNumber(long n) | |
{ | |
return n * (n + 1) / 2; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment