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.
$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 . '';
$i++;
}
echo $total; kkeith29
Nearly a Posting Virtuoso
1,353 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194