Solved ProjectEuler/017: php
This commit is contained in:
15
ProjectEuler/017/desc.yml
Normal file
15
ProjectEuler/017/desc.yml
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
title: How many letters would be needed to write all the numbers in words from 1 to 1000?
|
||||||
|
url: http://projecteuler.net/problem=17
|
||||||
|
|
||||||
|
desc: |
|
||||||
|
2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
|
||||||
|
What is the sum of the digits of the number 2^1000?
|
||||||
|
|
||||||
|
solution: Just a matter of having a correct number-to-word function
|
||||||
|
|
||||||
|
todo: Make a clean implementation
|
||||||
|
|
||||||
|
solutions:
|
||||||
|
solve.php:
|
||||||
|
desc: Urggh - Solve using a lib from bas@startpunt.cc and corrected a small error so 210 whould include an "and" correctly
|
||||||
|
language: php
|
79
ProjectEuler/017/solve.php
Normal file
79
ProjectEuler/017/solve.php
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
function n2w_hunderds($number)
|
||||||
|
{
|
||||||
|
|
||||||
|
$test=$number*1;
|
||||||
|
if (empty($test))return;
|
||||||
|
$lasts=array('one','two','three','four','five','six','seven','eight','nine');
|
||||||
|
$teens=array('eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen');
|
||||||
|
$teen=array('ten','twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety');
|
||||||
|
|
||||||
|
/* written by bas@startpunt.cc */
|
||||||
|
|
||||||
|
$string='';
|
||||||
|
$j=strlen($number);
|
||||||
|
$done=false;
|
||||||
|
for($i=0; $i<strlen($number); $i++)
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
if($j==2)
|
||||||
|
{
|
||||||
|
if(strlen($number)>2)
|
||||||
|
{
|
||||||
|
if($number[0]!=0)$string.= ' hundred ';
|
||||||
|
if($number % 100 != 0)$string.= 'and ';
|
||||||
|
}
|
||||||
|
if ($number[$i]==1)
|
||||||
|
{
|
||||||
|
if($number[$i+1]==0) $string.=$teen[$number[$i]-1];
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$string.=$teens[$number[$i+1]-1];
|
||||||
|
$done=true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if(!empty($teen[$number[$i]-1]))$string.=$teen[$number[$i]-1].' ';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
elseif($number[$i]!=0 && !$done) $string.=$lasts[$number[$i]-1];
|
||||||
|
|
||||||
|
$j--;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $string;
|
||||||
|
}
|
||||||
|
|
||||||
|
function n2w($number,$uk=0)
|
||||||
|
{
|
||||||
|
if(!is_string($number))$number.="";
|
||||||
|
if(!$uk)$many=array('', ' thousand ',' million ',' billion ',' trillion ');
|
||||||
|
else $many=array('', ' thousand ',' million ',' milliard ',' billion ');
|
||||||
|
$string='';
|
||||||
|
if(strlen($number)%3!=0)
|
||||||
|
{
|
||||||
|
$string.=n2w_hunderds(substr($number,0, strlen($number)%3 ));
|
||||||
|
$string.=$many[floor(strlen($number)/3)];
|
||||||
|
|
||||||
|
}
|
||||||
|
for($i=0; $i<floor(strlen($number)/3); $i++)
|
||||||
|
{
|
||||||
|
|
||||||
|
$string.=n2w_hunderds(substr($number,strlen($number)%3+($i*3),3));
|
||||||
|
if($number[strlen($number)%3+($i*3)]!=0)$string.=$many[floor(strlen($number)/3)-1-$i];
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return $string;
|
||||||
|
}
|
||||||
|
|
||||||
|
$len = 0;
|
||||||
|
for($i = 1; $i <= 1000; $i++) {
|
||||||
|
$len += strlen(str_replace(' ','',n2w($i)));
|
||||||
|
}
|
||||||
|
|
||||||
|
echo $len;
|
Reference in New Issue
Block a user