Project Euler

Project Euler Problem 40

steloflute 2012. 6. 9. 00:09

Problem 40

28 March 2003

An irrational decimal fraction is created by concatenating the positive integers:

0.123456789101112131415161718192021...

It can be seen that the 12th digit of the fractional part is 1.

If dn represents the nth digit of the fractional part, find the value of the following e-pression.

d1 d10 d100 d1000 d10000 d100000 d1000000


Answer:
210

 

 

C#

 

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

namespace Euler {
    class Program {
        static void Main(string[] args) {
            var sb = new StringBuilder(10000000);
            for (var i = 0; ; i++) {
                sb.Append(i);
                if (sb.Length > 1000000) break;
            }
            var indexes = new[] { 1, 10, 100, 1000, 10000, 100000, 1000000 };
            Console.WriteLine(indexes.Select(x => sb[x] - '0').Aggregate((x, y) => x * y));            
        }
    }
}



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

Project Euler Problem 42  (0) 2012.06.09
Project Euler Problem 41  (0) 2012.06.09
Project Euler Problem 39  (0) 2012.06.09
Project Euler Problem 38  (0) 2012.06.09
Project Euler Problem 37  (0) 2012.06.09