| | |
Project Euler Problem 17
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: May 2009
Posts: 5
Reputation:
Solved Threads: 0
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:
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
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)
<?php function decimal($i) { if ($i == 1) { return 3; } elseif ($i == 5 || $i == 6 || $i == 4) { return 5; } elseif ($i == 7) { return 7; } else { return 6; } } function onedigit($i) { if ($i == 1 || $i == 2 || $i == 6) { return 3; } elseif ($i == 4 || $i == 5 || $i == 9) { return 4; } elseif ($i == 0) { return 0; } else { return 5; } } function twodigit($i) { if ($i == 14 || $i == 16 || $i == 17 || $i == 19) { if ($i == 16) { return 7; } elseif ($i == 17) { return 9; } else { return 8; } } else { return onedigit(substr($i,1,1))+decimal(substr($i,0,1)); } } function threedigit($i) { if (substr($i,1,1) != 0 && substr($i,2,1) != 0) return onedigit(substr($i,0,1))+10+twodigit(substr($i,1,2)); elseif (substr($i,1,1) == 0 && substr($i,2,1) != 0) return onedigit(substr($i,0,1))+10+onedigit(substr($i,2,1)); else return onedigit(substr($i,0,1))+7; } $length=0; $limit=999; for ($i=1; $i<$limit+1; $i++) { if (strlen($i) == 1) $length += onedigit($i); elseif (strlen($i) == 2) $length += twodigit($i); else $length += threedigit($i); } echo $length+11;//Here i add "One thousand" ?>
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
•
•
Join Date: May 2009
Posts: 5
Reputation:
Solved Threads: 0
0
#2 34 Days Ago
•
•
•
•
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
PHP Syntax (Toggle Plain Text)
function threedigit($i) { if (substr($i,1,1) != 0 && substr($i,2,1) != 0) return onedigit(substr($i,0,1))+10+twodigit(substr($i,1,2)); elseif (substr($i,1,1) == 0 && substr($i,2,1) != 0) return onedigit(substr($i,0,1))+10+onedigit(substr($i,2,1)); elseif (substr($i,1,1) != 0 && substr($i,2,1) == 0) return onedigit(substr($i,0,1))+10+twodigit(substr($i,1,2)); else return onedigit(substr($i,0,1))+7; }
When you guys have a more efficient code to calculate the number of letters that are used, you can still tell me...
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.
I ended up getting 21224 not 21124. Maybe I overlooked something.
PHP Syntax (Toggle Plain Text)
$ones = array( 1 => 'One', 2 => 'Two', 3 => 'Three', 4 => 'Four', 5 => 'Five', 6 => 'Six', 7 => 'Seven', 8 => 'Eight', 9 => 'Nine' ); $teens = array( 1 => 'Eleven', 2 => 'Twelve', 3 => 'Thirteen', 4 => 'Fourteen', 5 => 'Fifteen', 6 => 'Sixteen', 7 => 'Seventeen', 8 => 'Eighteen', 9 => 'Nineteen' ); $tens = array( 1 => 'Ten', 2 => 'Twenty', 3 => 'Thirty', 4 => 'Fourty', 5 => 'Fifty', 6 => 'Sixty', 7 => 'Seventy', 8 => 'Eighty', 9 => 'Ninety' ); function number( $i ) { global $ones,$teens,$tens; $n = str_split( $i ); switch(count($n)) { case 1: $str = $ones[$n[0]]; break; case 2: if ( $n[1] == 0 ) { $str = $tens[$n[0]]; } elseif ( $i > 10 && $i < 20 ) { $str = $teens[$n[1]]; } else { $str = $tens[$n[0]] . ' ' . $ones[$n[1]]; } break; case 3: $str = "{$ones[$n[0]]} Hundred"; if ( intval($n[1].$n[2]) !== 0 ) { $str .= ' and ' . number( intval($n[1].$n[2]) ); } break; case 4: $str = "{$ones[$n[0]]} Thousand"; break; } return $str; } $total = 0; $i = 1; while( $i <= 1000 ) { $str = number( $i ); $total += strlen( str_replace(' ','',$str) ); echo $str . '<br />'; $i++; } 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.
Use [code] tags.
If you have found a solution to your problem, please mark the thread as SOLVED.
1
#4 34 Days Ago
•
•
•
•
Maybe I overlooked something.
PHP Syntax (Toggle Plain Text)
$tens = array( 1 => 'Ten', 2 => 'Twenty', 3 => 'Thirty', 4 => 'Fourty', 5 => 'Fifty', 6 => 'Sixty', 7 => 'Seventy', 8 => 'Eighty', 9 => 'Ninety' );
. "Never argue with idiots, they just drag you down to their level and then beat you with experience."
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.
All opinions count.
F'enw i yw Mr. Blaidd. Byddwch yn ofalus - dwi'n cnoi.
•
•
Join Date: May 2009
Posts: 5
Reputation:
Solved Threads: 0
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.
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
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 .
![]() |
Similar Threads
- Speeding up Euler Pr 12 (C)
- How many sundays fall on january in the first century (Computer Science)
- I need help!!! (C++)
- need help with math problem (C++)
- Senior Software Project Manager (Software Development Job Offers)
- Reversing A String (C)
- Project Euler problem (C++)
- C++ Homework project, need help (simple problem) (C++)
Other Threads in the PHP Forum
- Previous Thread: [Drupal] Dynamic Combo Box in a form
- Next Thread: PHP & MySQL Error. Please help.
| Thread Tools | Search this Thread |
ajax apache api array beginner binary body broken cakephp checkbox class cms code cookies cron curl database date date/time display dynamic ebooks echo email error file files folder form forms function functions google href htaccess html image include insert interactive ip javascript job joomla js limit link login mail mediawiki menu mlm mobile msqli_multi_query multiple mycodeisbad mysql navigation oop outofmemmory paging parse paypal pdf php problem procedure query radio ram random recursion regex remote script search server sessions sms source space sql stored subdomain syntax system table tutorial unicode update upload url validator variable video web webapplications websitecontactform xml youtube






