Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3397968
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:35:33+00:00 2026-05-18T04:35:33+00:00

I decided to try project euler problem 17 today, and I quickly wrote a

  • 0

I decided to try project euler problem 17 today, and I quickly wrote a pretty fast code in C++ to solve it. However, for some reason, the result is wrong.
The question is:

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.

I seriously don’t know why, as I have checked every part of my program thoroughly and I can’t find anything wrong. The only thing bad I could find is when checking for 1000, my while loop doesn’t check correctly. I fixed that by lowering the limit of my while loop to <1000 instead of <1001 and just added 11(onethousand = 11) manually to the sum. And yet, it doesn’t work. I would really appreciate it if you could tell me what’s wrong. I’m sure my code is pretty bad, but it’s something done in a few minutes. So here it is:

int getDigit (int x, int y)
{
 return (x / (int)pow(10.0, y)) % 10;
}

int main()
{
 string dictionary[10] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
 string dictionary2[18] = { "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
 string dictionary3[10] = { "onehundred", "twohundred", "threehundred", "fourhundred", "fivehundred", "sixhundred", "sevenhundred", "eighthundred", "ninehundred", "onethousand" };

 int i = 1;
 int last;
 int first;
 int middle;

 _int64 sumofletters = 0;

 while (i < 10)     //OK
 {
  sumofletters += dictionary[i].length();

  i++;
 }

 cout << sumofletters << endl;

 while (i < 20)     //OK
 {
  last = i % 10;

  sumofletters += dictionary2[last].length();

  i++;
 }

 while (i < 100)     //OK 
 {
  first = (i / 10) + 8;
  last = i % 10;

  if (last != 0)
  {
   sumofletters += dictionary2[first].length() + dictionary[last].length();
  }

  else
   sumofletters += dictionary2[first].length();

  i++;
 }

 cout << sumofletters << endl;

 while (i < 1000)       //OK
 {
  last = i % 10;
  first = (i / 100) - 1;
  middle = (getDigit(i, 1)) + 8;

  if (middle != 0 && last != 0)   //OK
  {
   if (middle == 1)
    sumofletters += dictionary3[first].length() + dictionary2[last].length() + 3;
   else
    sumofletters += dictionary3[first].length() + dictionary2[middle].length() + dictionary[last].length() + 3;
  }

  else if (last == 0 && middle != 0)  //OK
  {
   if (middle == 1)
    sumofletters += dictionary3[first].length() + 6;
   else
    sumofletters += dictionary3[first].length() + dictionary2[middle].length() + 3;
  }

  else if (middle == 0 && last != 0)   //OK
   sumofletters += dictionary3[first].length() + dictionary[last].length() + 3;

  else
   sumofletters += dictionary3[first].length();

  i++;
 }

 sumofletters += 11;

 cout << sumofletters << endl;

 return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T04:35:34+00:00Added an answer on May 18, 2026 at 4:35 am

    The problem appears to be with this line:

    middle = (getDigit(i, 1)) + 8; 
    

    You add 8 to this number – presumably to act as an offset into your dictionary2 – but in the following if statements, you have cases where it needs to be 0. Those can never be satisfied unless getDigit would return -8.

    Instead of adding your offset there, add it when you need it – or better still, don’t store those things in the same dictionary.

    Even better would be a completely different structure: write a function which generates the string for a number, and then take the length of that string for your counting. This will also make it much easier to debug problems like this, because you can see the actual string you’re taking the length of.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I've decided to try to solve my physics homework by writing some python
I decided to try http://www.screwturn.eu/ wiki as a code snippet storage utility. So far
In trying to solve a particular Project Euler question, I ran into difficulties with
I'm starting a new project and have decided to try to incorporate DDD patterns
After working for some time with SVN I decided to try git with my
Following the advice of wcoenen, I've decided to try using registration-free COM. This works
I decided to use log4net as a logger for a new webservice project. Everything
In my project I need to use third party code, stored in several Git
I just need some quick help on syntax. I'm doing a WPF project and
I've run into a problem with WPF when I try to use DataBinding with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.