Restructuring
This commit is contained in:
15
solutions/ProjectEuler/042/desc.yml
Normal file
15
solutions/ProjectEuler/042/desc.yml
Normal file
@ -0,0 +1,15 @@
|
||||
title: Coded triangle numbers
|
||||
url: http://projecteuler.net/problem=42
|
||||
|
||||
desc: |
|
||||
The nth term of the sequence of triangle numbers is given by, tn = 0.5n(n+1); so the first ten triangle numbers are:
|
||||
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
|
||||
By converting each letter in a word to a number corresponding to its alphabetical position and adding these values we form a word value. For example, the word value for SKY is 19 + 11 + 25 = 55 = t10. If the word value is a triangle number then we shall call the word a triangle word.
|
||||
Using words.txt (right click and 'Save Link/Target As...'), a 16K text file containing nearly two-thousand common English words, how many are triangle words?
|
||||
solution: Bruteforce
|
||||
|
||||
solutions:
|
||||
solve.php:
|
||||
desc: Expects data on STDIN
|
||||
language: php
|
||||
parameters: < ProjectEuler\042\words.txt
|
18
solutions/ProjectEuler/042/solve.php
Normal file
18
solutions/ProjectEuler/042/solve.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
$words = explode('","',substr(file_get_contents('php://STDIN'),1,-1));
|
||||
|
||||
//Find triangles
|
||||
for($t=1;$t<40;$t++) {
|
||||
$triangle_numbers[] = 0.5*$t*($t+1);
|
||||
}
|
||||
|
||||
foreach($words as $word) {
|
||||
for($c =0; $c < strlen($word); $c++) {
|
||||
$char_sum += ord($word[$c]) - 64;
|
||||
}
|
||||
if(in_array($char_sum,$triangle_numbers)) {
|
||||
$result++;
|
||||
}
|
||||
$char_sum = 0;
|
||||
}
|
||||
echo $result;
|
1
solutions/ProjectEuler/042/words.txt
Normal file
1
solutions/ProjectEuler/042/words.txt
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user