943,972 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 2343
  • PHP RSS
Oct 20th, 2009
1

Project Euler Problem 17

Expand Post »
Hi everyone,

I have a problem with one of the problems (17) that i try to solve on the Project Euler site.

The problem is as follows:

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.

The code that i have written is as follows:
PHP Syntax (Toggle Plain Text)
  1. <?php
  2. function decimal($i)
  3. {
  4. if ($i == 1)
  5. {
  6. return 3;
  7. }
  8. elseif ($i == 5 || $i == 6 || $i == 4)
  9. {
  10. return 5;
  11. }
  12. elseif ($i == 7)
  13. {
  14. return 7;
  15. }
  16. else
  17. {
  18. return 6;
  19. }
  20. }
  21. function onedigit($i)
  22. {
  23. if ($i == 1 || $i == 2 || $i == 6)
  24. {
  25. return 3;
  26. }
  27. elseif ($i == 4 || $i == 5 || $i == 9)
  28. {
  29. return 4;
  30. }
  31. elseif ($i == 0)
  32. {
  33. return 0;
  34. }
  35. else
  36. {
  37. return 5;
  38. }
  39. }
  40. function twodigit($i)
  41. {
  42. if ($i == 14 || $i == 16 || $i == 17 || $i == 19)
  43. {
  44. if ($i == 16)
  45. {
  46. return 7;
  47. }
  48. elseif ($i == 17)
  49. {
  50. return 9;
  51. }
  52. else
  53. {
  54. return 8;
  55. }
  56. }
  57. else
  58. {
  59. return onedigit(substr($i,1,1))+decimal(substr($i,0,1));
  60. }
  61. }
  62. function threedigit($i)
  63. {
  64. if (substr($i,1,1) != 0 && substr($i,2,1) != 0)
  65. return onedigit(substr($i,0,1))+10+twodigit(substr($i,1,2));
  66. elseif (substr($i,1,1) == 0 && substr($i,2,1) != 0)
  67. return onedigit(substr($i,0,1))+10+onedigit(substr($i,2,1));
  68. else
  69. return onedigit(substr($i,0,1))+7;
  70. }
  71.  
  72. $length=0;
  73. $limit=999;
  74.  
  75. for ($i=1; $i<$limit+1; $i++)
  76. {
  77. if (strlen($i) == 1)
  78. $length += onedigit($i);
  79. elseif (strlen($i) == 2)
  80. $length += twodigit($i);
  81. else
  82. $length += threedigit($i);
  83. }
  84. echo $length+11;//Here i add "One thousand"
  85. ?>

I really don't know where i have made a mistake but i know from a friend that the good result should be: 21124

If someone can find my mistake, it would be usefull for me.

Thanks in advance,

Olivier
Similar Threads
Reputation Points: 16
Solved Threads: 0
Newbie Poster
O71v13r is offline Offline
5 posts
since May 2009
Oct 21st, 2009
0
Re: Project Euler Problem 17
Click to Expand / Collapse  Quote originally posted by O71v13r ...

I really don't know where i have made a mistake but i know from a friend that the good result should be: 21124

If someone can find my mistake, it would be usefull for me.

Thanks in advance,

Olivier
Ok i solved it myself. In the function "threedigit" i forgot do define what the function should do if the third digit is 0. So i've fixed the problem. For those who are interested, you should change the function "threedigit" with this:
PHP Syntax (Toggle Plain Text)
  1. function threedigit($i)
  2. {
  3. if (substr($i,1,1) != 0 && substr($i,2,1) != 0)
  4. return onedigit(substr($i,0,1))+10+twodigit(substr($i,1,2));
  5. elseif (substr($i,1,1) == 0 && substr($i,2,1) != 0)
  6. return onedigit(substr($i,0,1))+10+onedigit(substr($i,2,1));
  7. elseif (substr($i,1,1) != 0 && substr($i,2,1) == 0)
  8. return onedigit(substr($i,0,1))+10+twodigit(substr($i,1,2));
  9. else
  10. return onedigit(substr($i,0,1))+7;
  11. }

When you guys have a more efficient code to calculate the number of letters that are used, you can still tell me...
Reputation Points: 16
Solved Threads: 0
Newbie Poster
O71v13r is offline Offline
5 posts
since May 2009
Oct 21st, 2009
2
Re: Project Euler Problem 17
I got bored and wrote code to echo out the actual string. Only took 10 minutes.

I ended up getting 21224 not 21124. Maybe I overlooked something.

PHP Syntax (Toggle Plain Text)
  1. $ones = array(
  2. 1 => 'One',
  3. 2 => 'Two',
  4. 3 => 'Three',
  5. 4 => 'Four',
  6. 5 => 'Five',
  7. 6 => 'Six',
  8. 7 => 'Seven',
  9. 8 => 'Eight',
  10. 9 => 'Nine'
  11. );
  12. $teens = array(
  13. 1 => 'Eleven',
  14. 2 => 'Twelve',
  15. 3 => 'Thirteen',
  16. 4 => 'Fourteen',
  17. 5 => 'Fifteen',
  18. 6 => 'Sixteen',
  19. 7 => 'Seventeen',
  20. 8 => 'Eighteen',
  21. 9 => 'Nineteen'
  22. );
  23. $tens = array(
  24. 1 => 'Ten',
  25. 2 => 'Twenty',
  26. 3 => 'Thirty',
  27. 4 => 'Fourty',
  28. 5 => 'Fifty',
  29. 6 => 'Sixty',
  30. 7 => 'Seventy',
  31. 8 => 'Eighty',
  32. 9 => 'Ninety'
  33. );
  34.  
  35. function number( $i ) {
  36. global $ones,$teens,$tens;
  37. $n = str_split( $i );
  38. switch(count($n)) {
  39. case 1:
  40. $str = $ones[$n[0]];
  41. break;
  42. case 2:
  43. if ( $n[1] == 0 ) {
  44. $str = $tens[$n[0]];
  45. }
  46. elseif ( $i > 10 && $i < 20 ) {
  47. $str = $teens[$n[1]];
  48. }
  49. else {
  50. $str = $tens[$n[0]] . ' ' . $ones[$n[1]];
  51. }
  52. break;
  53. case 3:
  54. $str = "{$ones[$n[0]]} Hundred";
  55. if ( intval($n[1].$n[2]) !== 0 ) {
  56. $str .= ' and ' . number( intval($n[1].$n[2]) );
  57. }
  58. break;
  59. case 4:
  60. $str = "{$ones[$n[0]]} Thousand";
  61. break;
  62. }
  63. return $str;
  64. }
  65.  
  66. $total = 0;
  67. $i = 1;
  68. while( $i <= 1000 ) {
  69. $str = number( $i );
  70. $total += strlen( str_replace(' ','',$str) );
  71. echo $str . '<br />';
  72. $i++;
  73. }
  74.  
  75. echo $total;
Last edited by kkeith29; Oct 21st, 2009 at 1:58 pm.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007
Oct 21st, 2009
1
Re: Project Euler Problem 17
Quote ...
Maybe I overlooked something.
I don't know whether it's the only thing, but look very closely at this:
PHP Syntax (Toggle Plain Text)
  1. $tens = array(
  2. 1 => 'Ten',
  3. 2 => 'Twenty',
  4. 3 => 'Thirty',
  5. 4 => 'Fourty',
  6. 5 => 'Fifty',
  7. 6 => 'Sixty',
  8. 7 => 'Seventy',
  9. 8 => 'Eighty',
  10. 9 => 'Ninety'
  11. );
Soon you'll probably notice that it isn't fourty, but forty .
Reputation Points: 2125
Solved Threads: 243
Postaholic
tux4life is offline Offline
2,105 posts
since Feb 2009
Oct 21st, 2009
-1
Re: Project Euler Problem 17
That'll do it - 1 char X 100 instances (10 lots of forty-something for every set of 100 numbers). Nice spot tux, but I have to say KK - your solution is pretty.
Sponsor
Featured Poster
Reputation Points: 1048
Solved Threads: 949
Sarcastic Poster
ardav is offline Offline
6,699 posts
since Oct 2006
Oct 21st, 2009
0
Re: Project Euler Problem 17
Thanks kkeith29,
It's true, your code looks more pretty than mine. I'm still learning PHP and as you can see, I don't know a lot of functions of PHP yet.

Quote ...
Soon you'll probably notice that it isn't fourty, but forty .
It's quite funny that you've made the same mistake as I did in the beginning. I wrote also fourty instead of forty :d
Reputation Points: 16
Solved Threads: 0
Newbie Poster
O71v13r is offline Offline
5 posts
since May 2009
Oct 21st, 2009
0
Re: Project Euler Problem 17
Wow, I can't believe I missed that.
Reputation Points: 235
Solved Threads: 193
Nearly a Posting Virtuoso
kkeith29 is offline Offline
1,315 posts
since Jun 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: [Drupal] Dynamic Combo Box in a form
Next Thread in PHP Forum Timeline: PHP & MySQL Error. Please help.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC