Search This Blog

Tuesday, December 14, 2010

Euler Problem 17

How many letters are used in counting from 1 to 1000 inclusive?

Inc the ands, but not spaces or hyphens!

Full details here: http://projecteuler.net/index.php?section=problems&id=17

the approach here is to work out how many times each sequence occurs.. so there are 900 occurrences of the word “hundred” for example…

In the end the answer is 21,124 characters!

_____

 

static void Main(string[] args)
        {

            string a1 = "onetwothreefourfivesixseveneightnine";
            string b1="teneleventwelvethirteenfourteenfifteensixteenseventeeneighteenninetine";
            string b2 = "twentythirtyfortyfiftysixtyseventyeightyninety";
            string c1 = "hundred";
            string c2 ="and";
            string c3 = "onethousand";


        int ans = (90*a1.Length) + (10*b1.Length) + (100*b2.Length)
                +(c1.Length*900) + (a1.Length*100) + (c3.Length) + (c2.Length*99*9);

        Console.WriteLine(ans);
        Console.ReadLine();

        }

No comments: