browse archive
6 January 2010
Project Euler puzzle 17, How many letters would be needed to write all the numbers in words from 1 to 1000?
How many letters would be needed to write all the numbers in words from 1 to 1000?
WARNING: The code must be American because of the spelling, it is Forty not Fourty etc.
I’d like to credit this site for the numbers to words script, I changes the spellings though.
function convert_number($number)
{
if (($number < 0) || ($number > 999999999))
{
throw new Exception("Number is out of range");
}
$Gn = floor($number / 1000000); /* Millions (giga) */
$number -= $Gn * 1000000;
$kn = floor($number / 1000); /* Thousands (kilo) */
$number -= $kn * 1000;
$Hn = floor($number / 100); /* Hundreds (hecto) */
$number -= $Hn * 100;
$Dn = floor($number / 10); /* Tens (deca) */
$n = $number % 10; /* Ones */
$res = "";
if ($Gn)
{
$res .= convert_number($Gn) . " Million";
}
if ($kn)
{
$res .= (empty($res) ? "" : " ") .
convert_number($kn) . " Thousand";
}
if ($Hn)
{
$res .= (empty($res) ? "" : " ") .
convert_number($Hn) . " Hundred";
}
$ones = array("", "One", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen",
"Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen",
"Nineteen");
$tens = array("", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty",
"Seventy", "Eighty", "Ninety");
if ($Dn || $n)
{
if (!empty($res))
{
$res .= " and ";
}
if ($Dn < 2)
{
$res .= $ones[$Dn * 10 + $n];
}
else
{
$res .= $tens[$Dn];
if ($n)
{
$res .= "-" . $ones[$n];
}
}
}
if (empty($res))
{
$res = "zero";
}
return $res;
}
$i = 1;
$total = '';
while($i < 1001){
$total = $total . convert_number($i);
$i++;
}
// take out spaces and hyphens
$total = str_replace(' ', '', $total);
$total = str_replace('-', '', $total);
#echo $total;
echo strlen($total);
6 January 2010
Project Euler puzzle 16, What is the sum of the digits of the number 21000?
What is the sum of the digits of the number 21000?
$var = 2;
$i = 0;
while($i < 999){
$var = $var * 2;
$i++;
}
$result = number_format($var);
$result = str_replace(',', '',$result);
$array = str_split($result);
$final = 0;
foreach($array as $char){
$final = $final + $char;
}
echo $final;
<!– –> HTML Comments
HTML Comments are used throughout an HTML document to explain something. Anything between the open and closing tags are completely ignored by the browser, except in special cases. Use comments to explain what a certain piece of code does, or file information. No one can see these notes you make, unless the user views the source of the page. Most people delete all the comment they make when they have done coding, this makes the file size much smaller depending on how many comments you use.
Examples
Single line
Multi-line / Block
Sometimes it is used to talk to certain browsers, this came about because people wanted to use different style sheets for different browsers, because Internet Explorer generally mucks everything up…
Example
Comments can be placed anywhere in your HTML document.