示例#1
0
<?php

$namesList = implode('', file(__DIR__ . '/data/p022_names.txt'));
$names = explode(',', $namesList);
foreach ($names as $k => $v) {
    $names[$k] = trim($v, '"');
}
sort($names);
$n = count($names);
$totalScore = 0;
for ($i = 0; $i < $n; ++$i) {
    $thisNameScore = nameScore($names[$i]) * ($i + 1);
    $totalScore += $thisNameScore;
}
echo $totalScore . "\n";
function nameScore($name)
{
    $score = 0;
    $n = strlen($name);
    for ($i = 0; $i < $n; ++$i) {
        $score += ord(substr($name, $i, 1)) - ord('A') + 1;
    }
    return $score;
}
?>

示例#2
0
 *
 * For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284.
 * The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.
 *
 * Evaluate the sum of all the amicable numbers under 10000.
 *
 * Solution: For each number 2 to 10000, check if it is amicable with the sum of its factors.  Too brute force.
 * Must refactor.
 */
include "helper.php";
function nameScore($name, $position)
{
    global $letters;
    $nameChars = str_split(strtolower($name));
    $nameScore = 0;
    foreach ($nameChars as $char) {
        $nameScore += $letters[$char];
    }
    return $nameScore * $position;
}
$letters = array('0' => 0, 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 6, 'g' => 7, 'h' => 8, 'i' => 9, 'j' => 10, 'k' => 11, 'l' => 12, 'm' => 13, 'n' => 14, 'o' => 15, 'p' => 16, 'q' => 17, 'r' => 18, 's' => 19, 't' => 20, 'u' => 21, 'v' => 22, 'w' => 23, 'x' => 24, 'y' => 25, 'z' => 26);
$total = 0;
$x = 1;
$namesFile = fopen('./assets/022-names.txt', 'r');
$names = explode(",", fread($namesFile, 99000));
sort($names);
foreach ($names as $name) {
    $total += nameScore(str_replace('"', '', $name), $x);
    $x++;
}
result(871198282, $total);