Euler Problem 17

If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.

If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?

NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of “and” when writing out numbers is in compliance with British usage.

Make sure you check your spelling, otherwise its a long day of counting.

public class Euler17
{
    public static void main(String[] args)
    {
        System.out.print(“Problem 17:\n”);
        Euler17 e = new Euler17();
        System.out.print(“Answer = ” + e.Problem()+ “\n”);
    }
    public String Problem ()
    {
        String list = “”, number = “”;
        int hun, ten, one;
        for (int i = 1; i <= 999; i++)
        {
            number = “”;
            hun = (int)Math.floor(i/100)*100;
            ten = (int)Math.floor((i-hun)/10)*10;
            one = (int)Math.floor(i-hun-ten);
            if (hun != 0)
            {
                number += Hundreds(hun);
                if (i % 100 != 0)
                    number += “and”;
            }
            if (ten <= 19)
            {
                number += Teens(ten+one);
            }
            else if (ten >= 20)
            {
                number += Tens(ten);
            }
            if (one != 0 && ten >= 20)
            {
                number += Ones(one);
            }
            System.out.print(i+”:”+number+”:”+number.length()+”\n”);
            list += number;
        }
        list += “OneThousand”;
        System.out.print(1000+”:”+”OneThousand:11\n”);
        return String.valueOf(list.length());
    }
    public String Ones (int num)
    {
        String answer = “”;
        if (num == 1)
            answer = “One”;
        else if (num == 2)
            answer = “Two”;
        else if (num == 3)
            answer = “Three”;
        else if (num == 4)
            answer = “Four”;
        else if (num == 5)
            answer = “Five”;
        else if (num == 6)
            answer = “Six”;
        else if (num == 7)
            answer = “Seven”;
        else if (num == 8)
            answer = “Eight”;
        else if (num == 9)
            answer = “Nine”;
        return answer;
    }
    public String Teens (int num)
    {
        String answer = “”;
        if (num < 10)
            answer = Ones(num);
        else if (num == 10)
            answer = “Ten”;
        else if (num == 11)
            answer = “Eleven”;
        else if (num == 12)
            answer = “Twelve”;
        else if (num == 13)
            answer = “Thirteen”;
        else if (num == 14)
            answer = “Fourteen”;
        else if (num == 15)
            answer = “Fifteen”;
        else if (num == 16)
            answer = “Sixteen”;
        else if (num == 17)
            answer = “Seventeen”;
        else if (num == 18)
            answer = “Eighteen”;
        else if (num == 19)
            answer = “Nineteen”;
        return answer;
    }
    public String Tens (int num)
    {
        String answer = “”;
        if (num == 10)
            answer = “Ten”;
        else if (num == 20)
            answer = “Twenty”;
        else if (num == 30)
            answer = “Thirty”;
        else if (num == 40)
            answer = “Forty”;
        else if (num == 50)
            answer = “Fifty”;
        else if (num == 60)
            answer = “Sixty”;
        el
se if (num == 70)
            answer = “Seventy”;
        else if (num == 80)
            answer = “Eighty”;
        else if (num == 90)
            answer = “Ninety”;
        return answer;
    }
    public String Hundreds (int num)
    {
        return Ones((int)Math.floor(num/100))+”Hundred”;
    }

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s