Solved ProjectEuler/039,041,042
This commit is contained in:
@ -14,3 +14,6 @@ solutions:
|
||||
solve.c:
|
||||
desc: ANSI C solution (Tested with TCC)
|
||||
language: c
|
||||
solve.js:
|
||||
desc: NodeJS solution
|
||||
language: js
|
18
ProjectEuler/039/desc.yml
Normal file
18
ProjectEuler/039/desc.yml
Normal file
@ -0,0 +1,18 @@
|
||||
title: Integer right triangles
|
||||
url: http://projecteuler.net/problem=39
|
||||
|
||||
desc: |
|
||||
If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120.
|
||||
{20,48,52}, {24,45,51}, {30,40,50}
|
||||
For which value of p <=1000, is the number of solutions maximised?
|
||||
|
||||
solution: |
|
||||
Bruteforce
|
||||
|
||||
solutions:
|
||||
solve.php:
|
||||
desc: Basic solution
|
||||
language: php
|
||||
solve.c:
|
||||
desc: Basic solution
|
||||
language: c
|
28
ProjectEuler/039/solve.c
Normal file
28
ProjectEuler/039/solve.c
Normal file
@ -0,0 +1,28 @@
|
||||
#include <stdio.h>
|
||||
#define P_VALUE 1000
|
||||
|
||||
int p, a, b, c, max, key, result = 0;
|
||||
int main( )
|
||||
{
|
||||
for(p=2;p<P_VALUE;p++)
|
||||
{
|
||||
|
||||
for(a=2;a<p;a++)
|
||||
{
|
||||
for(b=a;b<p;b++)
|
||||
{
|
||||
//Find C so it runs faster...
|
||||
c = p - a - b;
|
||||
if(a*a+b*b == c*c) {
|
||||
result++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(result > max) {
|
||||
max = result;
|
||||
key = p;
|
||||
}
|
||||
result = 0;
|
||||
}
|
||||
printf( "%i", key);
|
||||
}
|
20
ProjectEuler/039/solve.php
Normal file
20
ProjectEuler/039/solve.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
define('P_VALUE',1000);
|
||||
|
||||
for($p=2;$p<P_VALUE;$p++) {
|
||||
|
||||
for($a=2;$a<$p;$a++) {
|
||||
for($b=$a;$b<$p;$b++) {
|
||||
//Find C so it runs faster...
|
||||
$c = $p - $a - $b;
|
||||
if($a*$a+$b*$b == $c*$c) {
|
||||
$result++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if($result > $max) {
|
||||
$max = $result; $key = $p;
|
||||
}
|
||||
$result = 0;
|
||||
}
|
||||
echo $key;
|
14
ProjectEuler/041/desc.yml
Normal file
14
ProjectEuler/041/desc.yml
Normal file
@ -0,0 +1,14 @@
|
||||
title: Integer right triangles
|
||||
url: http://projecteuler.net/problem=41
|
||||
|
||||
desc: |
|
||||
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.
|
||||
What is the largest n-digit pandigital prime that exists?
|
||||
|
||||
solution: |
|
||||
Bruteforce
|
||||
|
||||
solutions:
|
||||
solve.php:
|
||||
desc: Basic solution
|
||||
language: php
|
27
ProjectEuler/041/solve.php
Normal file
27
ProjectEuler/041/solve.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
function pandigital_less($number) {
|
||||
|
||||
$array = count_chars($number,1);
|
||||
|
||||
foreach(range(1,count($array)) as $char) {
|
||||
if( $array[ord($char)]!== 1) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function is_prime($prime) {
|
||||
//if($prime%2 == 0) return false;
|
||||
|
||||
$sqrt = sqrt($prime);
|
||||
for ($i = 3; $i <= $sqrt; $i+=2){
|
||||
if ($prime%$i == 0) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
//We don't need to check uneven numbers
|
||||
//Has to start with a 7 and therefor be 7 digits long
|
||||
for($var = 7000001; $var < 8000000; $var=$var+2) {
|
||||
if(is_prime($var) AND pandigital_less($var)) { $result = $var; }
|
||||
}
|
||||
echo $result;
|
15
ProjectEuler/042/desc.yml
Normal file
15
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
ProjectEuler/042/solve.php
Normal file
18
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
ProjectEuler/042/words.txt
Normal file
1
ProjectEuler/042/words.txt
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user