Thread: Euler Project - Number Letter Counts

  1. #1
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294

    Euler Project - Number Letter Counts

    Hello!
    I am working on Euler Project exercise number 17. Here is the problem from the website.

    "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?"

    Here is my code:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    #define LENGTHOFHUNDRED         7
    #define LENGTHOFONETHOUSAND     11
    #define NUMSTART                1
    #define NUMEND                  1000
    #define ANDLENGTH               3
    
    
    static char * base_digits[] = { "one" , "two" , "three" , "four" , "five" , "six" , "seven" , "eight" , "nine" };
    static char * teens[] = { "eleven" , "twelve" , "thirteen" , "fourteen" , "fifteen" , "sixteen" , "seventeen" , "eighteen" , "nineteen" };
    static char * mults_of_ten[] = { "ten" , "twenty" , "thirty" , "forty" , "fifty" , "sixty" , "seventy" , "eighty" , "ninety" };
    
    
    unsigned int count_letters( unsigned int );
    
    
    int main(){
            unsigned int count = 0 , i;
    
    
            for( i = NUMSTART; i <= NUMEND; i++ )
                    count += count_letters( i );
            printf( "total letters-> %d\n" , count );
            return 0;
    }
    unsigned int count_letters( unsigned int num ){
            if( num < 20 && num > 10 )
                    return strlen( teens[ num - 11 ] );
            if( num == 1000 )
                    return LENGTHOFONETHOUSAND;
    
    
            unsigned int digitcount = 0 , n , mdivisor = 1;
    
    
            if( num > 100 && ( num % 100 ) )
                    digitcount += ANDLENGTH;
            do{
                    num -= ( n = num % ( mdivisor *= 10 ) );
                    if( n ){
                            if( n < 10 )
                                    digitcount += strlen( base_digits[ n - 1 ] );
                            else if( n < 100 )
                                    digitcount += strlen( mults_of_ten[ ( n / 10 - 1 ) ] );
                            else
                                    digitcount += strlen( base_digits[ ( n / 100 - 1 ) ] ) + LENGTHOFHUNDRED;
                    }
            }while( num );
            return digitcount;
    }
    Can someone show me where my logic went awry?
    "Simplicity is the ultimate sophistication." - Leonardo da Vinci

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    So through your tests, which number does it first fail on?

    Or even which number(s) in general fail.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    why not print the actual strings besides the letter count to see what the code is really generating.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    What exactly is it doing that you don't expect?

    And are you sure you're supposed to include the word "and" in the letter count? You should be able to count to 1000 without using "and".

  5. #5
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Sorry, this was at the end of the problem.

    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.

    When I run in on the numbers 1 through 5 I get what is expected, 19. And when I run it on just the number 342, I get 23. And 20 for the number 115.

    But when I try to execute it with the final parameters, starting at 1 and ending at 1000, I get "total letters-> 21088". Which is not the correct answer.
    "Simplicity is the ultimate sophistication." - Leonardo da Vinci

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    The reason for, and joy of, Project Euler is finding solutions so I'm hesitant to give you any specific advice.

    So, I'm pretty much stuck just reiterating what Salem and dmh2000 has said: use some standard debugging methods to find where you first go wrong and with that in mind you can find the flaw in your implementation.

    Soma

  7. #7
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Enough said. Thanks for all the advice. And you're right, it will be much sweeter knowing I got to the center of the lollipop by myself.
    "Simplicity is the ultimate sophistication." - Leonardo da Vinci

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by dmh2000 View Post
    why not print the actual strings besides the letter count to see what the code is really generating.
    Perfect.

  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    24
    Not the answer they are looking for by the example given but by the question of how many letters are used, I'd have to say 18.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Project Euler 22
    By deadrabbit in forum C Programming
    Replies: 1
    Last Post: 06-23-2012, 11:22 PM
  2. Project Euler Problem 8
    By deadrabbit in forum C Programming
    Replies: 2
    Last Post: 10-06-2011, 10:56 PM
  3. Replies: 9
    Last Post: 09-02-2011, 04:57 PM
  4. Project Euler
    By CodeGuru25 in forum C Programming
    Replies: 2
    Last Post: 01-13-2010, 06:25 AM
  5. Project Euler problem 3
    By SELFMADE in forum C Programming
    Replies: 7
    Last Post: 09-16-2009, 03:33 AM