Project Euler

Project Euler Problem 45

steloflute 2012. 6. 9. 00:11

Problem 45

06 June 2003

Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:

Triangle   Tn=n(n+1)/2   1, 3, 6, 10, 15, ...
Pentagonal   Pn=n(3n1)/2   1, 5, 12, 22, 35, ...
Hexagonal   Hn=n(2n1)   1, 6, 15, 28, 45, ...

It can be verified that T285 = P165 = H143 = 40755.

Find the next triangle number that is also pentagonal and hexagonal.


Answer:
1533776805

 

C#

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Numerics;

namespace Euler {
    class Program {
        static bool isPentagonal(long value) {
            var index = (Math.Sqrt(24 * value + 1) + 1) / 6;
            return index == Math.Floor(index);
        }

        static void Main(string[] args) {
            for (var i = 144;;i++) {
                var h = i * (2 * i - 1);
                // Every hexagonal number is a triangular number.
                if (isPentagonal(h)) {
                    Console.WriteLine(h);
                    break;
                }
            }
        }
    }
}



'Project Euler' 카테고리의 다른 글

Project Euler Problem 47  (0) 2012.06.09
Project Euler Problem 46  (0) 2012.06.09
Project Euler Problem 44  (0) 2012.06.09
Project Euler Problem 43  (0) 2012.06.09
Project Euler Problem 42  (0) 2012.06.09