22
This commit is contained in:
parent
69c9bdf517
commit
b4668fe51c
23
euler22.php
Normal file
23
euler22.php
Normal file
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
/*
|
||||
* Gnieark's anwser to euler problem 22
|
||||
* https://projecteuler.net/problem=22
|
||||
*/
|
||||
|
||||
$names=explode('","',substr(file_get_contents("inc/p022_names.txt"),1,-1));
|
||||
sort($names);
|
||||
|
||||
$letters="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
for($i=0;$i<strlen($letters);$i++){
|
||||
$lettersScores[substr($letters,$i,1)]=$i+1;
|
||||
}
|
||||
|
||||
$sum=0;
|
||||
foreach($names as $order => $name){
|
||||
$nameScore=0;
|
||||
for($i=0; $i<strlen($name);$i++){
|
||||
$nameScore+=$lettersScores[substr($name,$i,1)];
|
||||
}
|
||||
$sum+=$nameScore * ($order +1);
|
||||
}
|
||||
echo $sum;
|
Loading…
Reference in New Issue
Block a user