Project Euler Problem 17

Thread Solved

Join Date: May 2009
Posts: 5
Reputation: O71v13r is an unknown quantity at this point 
Solved Threads: 0
O71v13r O71v13r is offline Offline
Newbie Poster

Project Euler Problem 17

 
1
  #1
Oct 20th, 2009
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:
  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
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 5
Reputation: O71v13r is an unknown quantity at this point 
Solved Threads: 0
O71v13r O71v13r is offline Offline
Newbie Poster
 
0
  #2
34 Days Ago
Originally Posted by O71v13r View Post

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:
  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...
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso
 
2
  #3
34 Days Ago
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.

  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; 34 Days Ago at 1:58 pm.
Google is your friend.

Use [code] tags.

If you have found a solution to your problem, please mark the thread as SOLVED.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso
 
1
  #4
34 Days Ago
Maybe I overlooked something.
I don't know whether it's the only thing, but look very closely at this:
  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 .
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 950
Reputation: ardav will become famous soon enough ardav will become famous soon enough 
Solved Threads: 125
ardav's Avatar
ardav ardav is offline Offline
Posting Shark
 
0
  #5
34 Days Ago
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.
"...the woods would be a very silent place if no birds sang except for the best"
All opinions count.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 5
Reputation: O71v13r is an unknown quantity at this point 
Solved Threads: 0
O71v13r O71v13r is offline Offline
Newbie Poster
 
0
  #6
34 Days Ago
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.

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
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso
 
0
  #7
33 Days Ago
Wow, I can't believe I missed that.
Google is your friend.

Use [code] tags.

If you have found a solution to your problem, please mark the thread as SOLVED.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC