Sunday 12 April 2009

PROJECT EULER #40

Link to Project Euler problem 40

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 expression.

d1 x d10 x d100 x d1000 x d10000 x d100000 x d1000000


using System;
using System.Text;

namespace project_euler
{
class Program
{
static void Main()
{
//Problem 40
DateTime start = DateTime.Now;
StringBuilder sb = new StringBuilder();
for (int i = 1; i < 200000; i++)
sb.Append(i.ToString());
string s = sb.ToString();
long sum = int.Parse(s[0].ToString())*int.Parse(s[9].ToString())*int.Parse(s[99].ToString())*int.Parse(s[999].ToString())*int.Parse(s[9999].ToString())*int.Parse(s[99999].ToString())*int.Parse(s[999999].ToString());
Console.WriteLine(sum);
TimeSpan time = DateTime.Now - start;
Console.WriteLine("This took {0}", time);
Console.ReadKey();
}
}
}

No comments: