1

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
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

Votes + Comments
Kudos for using code tags on your first post :P
4
Contributors
6
Replies
8
Views
8 Years
Discussion Span
Last Post by somedude3488
0


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:

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

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 . '<br />';
	$i++;
}

echo $total;

Edited by somedude3488: n/a

Votes + Comments
Nice piece of code :)
Thanks for the second and more pretty solution for my problem :)
1

Maybe I overlooked something.

I don't know whether it's the only thing, but look very closely at this:

$tens = array(
	1 => 'Ten',
	2 => 'Twenty',
	3 => 'Thirty',
	4 => 'Fourty',
	5 => 'Fifty',
	6 => 'Sixty',
	7 => 'Seventy',
	8 => 'Eighty',
	9 => 'Ninety'
);

Soon you'll probably notice that it isn't fourty, but forty :).

Votes + Comments
Solved a second problem in the thread
0

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.

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.