Restructuring
This commit is contained in:
26
solutions/ProjectEuler/001/desc.yml
Normal file
26
solutions/ProjectEuler/001/desc.yml
Normal file
@ -0,0 +1,26 @@
|
||||
title: Add all the natural numbers below one thousand that are multiples of 3 or 5.
|
||||
url: http://projecteuler.net/problem=1
|
||||
|
||||
desc: |
|
||||
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
|
||||
Find the sum of all the multiples of 3 or 5 below 1000.
|
||||
|
||||
solution: |
|
||||
Loop numbers from 1-1000 - Use mod to find the multiples
|
||||
|
||||
solutions:
|
||||
solve.php:
|
||||
desc: Basic solution
|
||||
language: php
|
||||
solve.rb:
|
||||
desc: Basic solution in Ruby
|
||||
language: ruby
|
||||
solve.c:
|
||||
desc: ANSI C solution (Tested with TCC)
|
||||
language: c
|
||||
solve.js:
|
||||
desc: NodeJS solution
|
||||
language: javascript
|
||||
solve.lua:
|
||||
desc: Basic solution
|
||||
language: lua
|
15
solutions/ProjectEuler/001/solve.c
Normal file
15
solutions/ProjectEuler/001/solve.c
Normal file
@ -0,0 +1,15 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main( )
|
||||
{
|
||||
int sum = 0;
|
||||
int i = 0;
|
||||
for(i = 1; i < 1000; i++)
|
||||
{
|
||||
if(i % 3 == 0 || i % 5 == 0)
|
||||
{
|
||||
sum += i;
|
||||
}
|
||||
}
|
||||
printf( "%i", sum );
|
||||
}
|
9
solutions/ProjectEuler/001/solve.js
Normal file
9
solutions/ProjectEuler/001/solve.js
Normal file
@ -0,0 +1,9 @@
|
||||
sum = 0;
|
||||
i = 0;
|
||||
for(i=1; i<1000;i++) {
|
||||
if(i % 3 == 0 || i % 5 == 0) {
|
||||
sum += i;
|
||||
}
|
||||
}
|
||||
|
||||
console.log(sum);
|
7
solutions/ProjectEuler/001/solve.lua
Normal file
7
solutions/ProjectEuler/001/solve.lua
Normal file
@ -0,0 +1,7 @@
|
||||
sum = 0
|
||||
for i=1,999 do
|
||||
if i % 3 == 0 or i % 5 == 0 then
|
||||
sum = sum + i
|
||||
end
|
||||
end
|
||||
print(sum)
|
10
solutions/ProjectEuler/001/solve.php
Normal file
10
solutions/ProjectEuler/001/solve.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
$sum = 0;
|
||||
$i = 0;
|
||||
for($i=1; $i<1000;$i++) {
|
||||
if($i % 3 == 0 OR $i % 5 == 0) {
|
||||
$sum += $i;
|
||||
}
|
||||
}
|
||||
|
||||
echo $sum;
|
7
solutions/ProjectEuler/001/solve.rb
Normal file
7
solutions/ProjectEuler/001/solve.rb
Normal file
@ -0,0 +1,7 @@
|
||||
sum = 0
|
||||
(1..1000).each do |i|
|
||||
if(i % 3 == 0 or i % 5 == 0)
|
||||
sum += i
|
||||
end
|
||||
end
|
||||
puts sum
|
27
solutions/ProjectEuler/002/desc.yml
Normal file
27
solutions/ProjectEuler/002/desc.yml
Normal file
@ -0,0 +1,27 @@
|
||||
title: By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
|
||||
url: http://projecteuler.net/problem=2
|
||||
|
||||
desc: |
|
||||
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
|
||||
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
|
||||
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
|
||||
|
||||
solution: |
|
||||
Step through the fibonacci sequence while you sum each even entry. Quit as you reach 4million
|
||||
|
||||
solutions:
|
||||
solve.php:
|
||||
desc: Basic solution
|
||||
language: php
|
||||
solve.rb:
|
||||
desc: Basic solution in Ruby
|
||||
language: ruby
|
||||
solve.c:
|
||||
desc: ANSI C solution (Tested with TCC)
|
||||
language: c
|
||||
solve.js:
|
||||
desc: NodeJS solution
|
||||
language: javascript
|
||||
solve.lua:
|
||||
desc: Basic solution
|
||||
language: lua
|
18
solutions/ProjectEuler/002/solve.c
Normal file
18
solutions/ProjectEuler/002/solve.c
Normal file
@ -0,0 +1,18 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main( )
|
||||
{
|
||||
int sum = 2;
|
||||
int fib[3] = { 1, 2, 3 };
|
||||
|
||||
while(fib[2] < 4000000)
|
||||
{
|
||||
fib[2] = fib[0] + fib[1];
|
||||
if(fib[2] % 2 == 0)
|
||||
sum += fib[2];
|
||||
|
||||
fib[0] = fib[1];
|
||||
fib[1] = fib[2];
|
||||
}
|
||||
printf( "%i", sum );
|
||||
}
|
11
solutions/ProjectEuler/002/solve.js
Normal file
11
solutions/ProjectEuler/002/solve.js
Normal file
@ -0,0 +1,11 @@
|
||||
sum = 2;
|
||||
fib = new Array( 1, 2, 3 );
|
||||
while(fib[2] < 4000000)
|
||||
{
|
||||
fib[2] = fib[0] + fib[1];
|
||||
if(fib[2] % 2 == 0)
|
||||
sum += fib[2];
|
||||
fib[0] = fib[1];
|
||||
fib[1] = fib[2];
|
||||
}
|
||||
console.log(sum );
|
11
solutions/ProjectEuler/002/solve.lua
Normal file
11
solutions/ProjectEuler/002/solve.lua
Normal file
@ -0,0 +1,11 @@
|
||||
sum = 2
|
||||
fib = {1, 2, 3}
|
||||
while fib[2] < 4000000 do
|
||||
fib[3] = fib[1] + fib[2]
|
||||
if(fib[3] % 2 == 0) then
|
||||
sum = sum + fib[3]
|
||||
end
|
||||
fib[1] = fib[2]
|
||||
fib[2] = fib[3]
|
||||
end
|
||||
print (sum )
|
12
solutions/ProjectEuler/002/solve.php
Normal file
12
solutions/ProjectEuler/002/solve.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
$fib = array(1,2,3);
|
||||
$sum = 2;
|
||||
while($fib[2] < 4000000) {
|
||||
$fib[2] = $fib[0] + $fib[1];
|
||||
if($fib[2] % 2 == 0) {
|
||||
$sum += $fib[2];
|
||||
}
|
||||
$fib[0] = $fib[1];
|
||||
$fib[1] = $fib[2];
|
||||
}
|
||||
echo $sum;
|
11
solutions/ProjectEuler/002/solve.rb
Normal file
11
solutions/ProjectEuler/002/solve.rb
Normal file
@ -0,0 +1,11 @@
|
||||
fib = [1, 2, 3];
|
||||
sum = 2;
|
||||
while(fib[2] < 4000000)
|
||||
fib[2] = fib[0] + fib[1];
|
||||
if(fib[2] % 2 == 0)
|
||||
sum += fib[2];
|
||||
end
|
||||
fib[0] = fib[1];
|
||||
fib[1] = fib[2];
|
||||
end
|
||||
puts sum;
|
15
solutions/ProjectEuler/003/desc.yml
Normal file
15
solutions/ProjectEuler/003/desc.yml
Normal file
@ -0,0 +1,15 @@
|
||||
title: Find the largest prime factor of a composite number.
|
||||
url: http://projecteuler.net/problem=3
|
||||
|
||||
desc: |
|
||||
The prime factors of 13195 are 5, 7, 13 and 29.
|
||||
What is the largest prime factor of the number 600851475143 ?
|
||||
|
||||
solution: |
|
||||
Use factorization to find the largest prime factor
|
||||
|
||||
solutions:
|
||||
solve.php:
|
||||
desc: Basic solution
|
||||
language: php
|
||||
|
33
solutions/ProjectEuler/003/solve.php
Normal file
33
solutions/ProjectEuler/003/solve.php
Normal file
@ -0,0 +1,33 @@
|
||||
<?php
|
||||
$number = 600851475143;
|
||||
|
||||
echo max(factorize($number));
|
||||
|
||||
/**
|
||||
* Returns a sorted array of the prime factorization of $num
|
||||
* @staticvar array $aFactors
|
||||
* @param type int Number to factorize
|
||||
* @return type array Prime factors
|
||||
*/
|
||||
function factorize($num) {
|
||||
// Returns a sorted array of the prime factorization of $num
|
||||
// Caches prior results. Returns empty array for |$num|<2
|
||||
// eg. factorize(360) => [5, 3, 3, 2, 2, 2]
|
||||
static $aFactors = array();
|
||||
if (2 > $num = abs($num))
|
||||
return array(); // negatives, 1, 0
|
||||
|
||||
if ($aFactors[$key = "x$num"]) { // handles doubles
|
||||
// Been there, done that
|
||||
if (($factor = $aFactors[$key]) == $num)
|
||||
return array($num);
|
||||
return array_merge(factorize($num / $factor), array($factor));
|
||||
}
|
||||
|
||||
// Find a smallest factor
|
||||
for ($sqrt = sqrt($num), $factor = 2; $factor <= $sqrt; ++$factor)
|
||||
if (floor($num / $factor) == $num / $factor)
|
||||
return array_merge(factorize($num / $factor), array($aFactors[$key] = $factor));
|
||||
|
||||
return (array($aFactors[$key] = $num));
|
||||
}
|
17
solutions/ProjectEuler/004/desc.yml
Normal file
17
solutions/ProjectEuler/004/desc.yml
Normal file
@ -0,0 +1,17 @@
|
||||
title: Find the largest palindrome made from the product of two 3-digit numbers.
|
||||
url: http://projecteuler.net/problem=4
|
||||
|
||||
desc: |
|
||||
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.
|
||||
Find the largest palindrome made from the product of two 3-digit numbers.
|
||||
|
||||
solution: |
|
||||
See code
|
||||
|
||||
solutions:
|
||||
solve.php:
|
||||
desc: Basic solution
|
||||
language: php
|
||||
solve.rb:
|
||||
desc: Basic solution in Ruby
|
||||
language: ruby
|
13
solutions/ProjectEuler/004/solve.php
Normal file
13
solutions/ProjectEuler/004/solve.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
$max = 0;
|
||||
for($num1 = 1000; $num1>100; $num1--) {
|
||||
for($num2 = 1000; $num2>100; $num2--) {
|
||||
$sum = $num1 * $num2;
|
||||
|
||||
//Check if palindrome
|
||||
if($sum > $max AND strrev($sum) == $sum)
|
||||
$max = $sum;
|
||||
|
||||
}
|
||||
}
|
||||
echo $max;
|
10
solutions/ProjectEuler/004/solve.rb
Normal file
10
solutions/ProjectEuler/004/solve.rb
Normal file
@ -0,0 +1,10 @@
|
||||
max = 0;
|
||||
(100..1000).each do |num1|
|
||||
(100..1000).each do |num2|
|
||||
sum = num1 * num2
|
||||
if( sum > max and sum.to_s.reverse == sum.to_s)
|
||||
max = sum
|
||||
end
|
||||
end
|
||||
end
|
||||
print max
|
20
solutions/ProjectEuler/005/desc.yml
Normal file
20
solutions/ProjectEuler/005/desc.yml
Normal file
@ -0,0 +1,20 @@
|
||||
title: What is the smallest number divisible by each of the numbers 1 to 20?
|
||||
url: http://projecteuler.net/problem=5
|
||||
|
||||
desc: |
|
||||
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
|
||||
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
|
||||
|
||||
solution: |
|
||||
See code
|
||||
|
||||
solutions:
|
||||
solve.php:
|
||||
desc: Basic solution
|
||||
language: php
|
||||
solve.rb:
|
||||
desc: Basic solution in Ruby
|
||||
language: ruby
|
||||
solve.js:
|
||||
desc: Basic solution for NodeJS
|
||||
language: js
|
11
solutions/ProjectEuler/005/solve.js
Normal file
11
solutions/ProjectEuler/005/solve.js
Normal file
@ -0,0 +1,11 @@
|
||||
for(i=20;true;i+=20) {
|
||||
div = 19;
|
||||
|
||||
while(!(i % div)) {
|
||||
div--;
|
||||
if(div == 0) {
|
||||
console.log( i );
|
||||
process.exit(0);
|
||||
}
|
||||
}
|
||||
}
|
12
solutions/ProjectEuler/005/solve.lua
Normal file
12
solutions/ProjectEuler/005/solve.lua
Normal file
@ -0,0 +1,12 @@
|
||||
i=0
|
||||
while(true) do
|
||||
i = i + 20
|
||||
div = 19
|
||||
while((i % div) == 0) do
|
||||
div = div - 1
|
||||
if(div == 0) then
|
||||
print(i)
|
||||
os.exit()
|
||||
end
|
||||
end
|
||||
end
|
12
solutions/ProjectEuler/005/solve.php
Normal file
12
solutions/ProjectEuler/005/solve.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
for($i=20;true;$i+=20) {
|
||||
$div = 19;
|
||||
|
||||
while(!($i % $div)) {
|
||||
$div--;
|
||||
if($div == 0) {
|
||||
echo $i;
|
||||
die;
|
||||
}
|
||||
}
|
||||
}
|
13
solutions/ProjectEuler/005/solve.rb
Normal file
13
solutions/ProjectEuler/005/solve.rb
Normal file
@ -0,0 +1,13 @@
|
||||
i = 0
|
||||
while (true)
|
||||
i = i + 20
|
||||
div = 19
|
||||
|
||||
while((i % div) == 0)
|
||||
div = div - 1
|
||||
if(div == 0) then
|
||||
puts i
|
||||
exit
|
||||
end
|
||||
end
|
||||
end
|
27
solutions/ProjectEuler/006/desc.yml
Normal file
27
solutions/ProjectEuler/006/desc.yml
Normal file
@ -0,0 +1,27 @@
|
||||
title: What is the smallest number divisible by each of the numbers 1 to 20?
|
||||
url: http://projecteuler.net/problem=6
|
||||
|
||||
desc: |
|
||||
The sum of the squares of the first ten natural numbers is,
|
||||
1^2 + 2^2 + ... + 10^2 = 385
|
||||
The square of the sum of the first ten natural numbers is,
|
||||
(1 + 2 + ... + 10)^2 = 55^2 = 3025
|
||||
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 385 = 2640.
|
||||
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
|
||||
|
||||
solution: |
|
||||
See code
|
||||
|
||||
solutions:
|
||||
solve.php:
|
||||
desc: Basic solution
|
||||
language: php
|
||||
solve.rb:
|
||||
desc: Basic Ruby solution
|
||||
language: ruby
|
||||
solve.c:
|
||||
desc: ANSI C solution (Tested with TCC)
|
||||
language: c
|
||||
solve.js:
|
||||
desc: Javascript solution for NodeJS
|
||||
language: javascript
|
19
solutions/ProjectEuler/006/solve.c
Normal file
19
solutions/ProjectEuler/006/solve.c
Normal file
@ -0,0 +1,19 @@
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
int main( )
|
||||
{
|
||||
int num;
|
||||
int result;
|
||||
|
||||
int square = 0;
|
||||
int sum = 0;
|
||||
|
||||
for(num=1; num<=100; num++) {
|
||||
square += pow(num,2);
|
||||
sum += num;
|
||||
|
||||
}
|
||||
result = pow(sum,2) - square;
|
||||
printf("%d", result);
|
||||
}
|
9
solutions/ProjectEuler/006/solve.js
Normal file
9
solutions/ProjectEuler/006/solve.js
Normal file
@ -0,0 +1,9 @@
|
||||
square = 0;
|
||||
sum = 0;
|
||||
for(num=1;num<101;num++) {
|
||||
square += Math.pow(num,2);
|
||||
sum += num;
|
||||
|
||||
}
|
||||
console.log( Math.pow(sum,2) - square);
|
||||
|
9
solutions/ProjectEuler/006/solve.php
Normal file
9
solutions/ProjectEuler/006/solve.php
Normal file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
$square =0;
|
||||
$sum =0;
|
||||
for($num=1;$num<101;$num++) {
|
||||
$square += pow($num,2);
|
||||
$sum += $num;
|
||||
|
||||
}
|
||||
echo pow($sum,2) - $square;
|
7
solutions/ProjectEuler/006/solve.rb
Normal file
7
solutions/ProjectEuler/006/solve.rb
Normal file
@ -0,0 +1,7 @@
|
||||
square = 0
|
||||
sum = 0
|
||||
(1..100).each do |num|
|
||||
square += num**2;
|
||||
sum += num;
|
||||
end
|
||||
puts sum**2 - square;
|
20
solutions/ProjectEuler/007/desc.yml
Normal file
20
solutions/ProjectEuler/007/desc.yml
Normal file
@ -0,0 +1,20 @@
|
||||
title: Find the 10001st prime.
|
||||
url: http://projecteuler.net/problem=7
|
||||
|
||||
desc: |
|
||||
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
|
||||
What is the 10 001st prime number?
|
||||
|
||||
solution: |
|
||||
Move up the among the prime numbers till you reach 10001
|
||||
|
||||
solutions:
|
||||
solve.php:
|
||||
desc: Basic solution
|
||||
language: php
|
||||
solve.rb:
|
||||
desc: Basic solution
|
||||
language: ruby
|
||||
solve.c:
|
||||
desc: ANSI C (Compiled with TCC) solution
|
||||
language: c
|
30
solutions/ProjectEuler/007/solve.c
Normal file
30
solutions/ProjectEuler/007/solve.c
Normal file
@ -0,0 +1,30 @@
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
int is_prime( int prime) {
|
||||
|
||||
int max_test = sqrt( prime );
|
||||
|
||||
int i;
|
||||
for (i = 3; i <= max_test; i+=2){
|
||||
if (prime % i == 0){
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main( )
|
||||
{
|
||||
//Use start val
|
||||
int i=13;
|
||||
int primes = 6;
|
||||
do{
|
||||
i+= 2;
|
||||
if(is_prime(i)){
|
||||
primes++;
|
||||
}
|
||||
|
||||
} while(primes < 10001);
|
||||
printf("%d", i);
|
||||
}
|
18
solutions/ProjectEuler/007/solve.php
Normal file
18
solutions/ProjectEuler/007/solve.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
function is_prime($prime) {
|
||||
$sqrt = sqrt($prime);
|
||||
for ($i = 3; $i <= $sqrt; $i+=2){
|
||||
if ($prime%$i == 0) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
$i=13;
|
||||
$primes = 6;
|
||||
do{
|
||||
$i+= 2;
|
||||
if(is_prime($i)){
|
||||
$primes++;
|
||||
}
|
||||
}while($primes < 10001);
|
||||
echo $i;
|
21
solutions/ProjectEuler/007/solve.rb
Normal file
21
solutions/ProjectEuler/007/solve.rb
Normal file
@ -0,0 +1,21 @@
|
||||
def is_prime(prime)
|
||||
sqrt = Math.sqrt(prime)
|
||||
i=3
|
||||
while(i <= sqrt)
|
||||
if (prime % i == 0)
|
||||
return false
|
||||
end
|
||||
i+= 2
|
||||
end
|
||||
return true;
|
||||
end
|
||||
|
||||
i=13
|
||||
primes = 6
|
||||
until (primes == 10001)
|
||||
i+= 2
|
||||
if(is_prime(i))
|
||||
primes+=1
|
||||
end
|
||||
end
|
||||
puts i
|
18
solutions/ProjectEuler/008/desc.yml
Normal file
18
solutions/ProjectEuler/008/desc.yml
Normal file
@ -0,0 +1,18 @@
|
||||
title: Discover the largest product of five consecutive digits in the 1000-digit number.
|
||||
url: http://projecteuler.net/problem=8
|
||||
|
||||
desc: |
|
||||
Find the greatest product of five consecutive digits in the 1000-digit number.
|
||||
|
||||
solution: |
|
||||
Bruteforce (Unless someone found a smarter way)
|
||||
|
||||
solutions:
|
||||
solve.php:
|
||||
desc: Expects the haystack as stdin with optional newlines
|
||||
language: php
|
||||
parameters: < ProjectEuler\008\input
|
||||
solve.rb:
|
||||
desc: Expects the haystack as stdin with optional newlines
|
||||
language: ruby
|
||||
parameters: < ProjectEuler\008\input
|
20
solutions/ProjectEuler/008/input
Normal file
20
solutions/ProjectEuler/008/input
Normal file
@ -0,0 +1,20 @@
|
||||
73167176531330624919225119674426574742355349194934
|
||||
96983520312774506326239578318016984801869478851843
|
||||
85861560789112949495459501737958331952853208805511
|
||||
12540698747158523863050715693290963295227443043557
|
||||
66896648950445244523161731856403098711121722383113
|
||||
62229893423380308135336276614282806444486645238749
|
||||
30358907296290491560440772390713810515859307960866
|
||||
70172427121883998797908792274921901699720888093776
|
||||
65727333001053367881220235421809751254540594752243
|
||||
52584907711670556013604839586446706324415722155397
|
||||
53697817977846174064955149290862569321978468622482
|
||||
83972241375657056057490261407972968652414535100474
|
||||
82166370484403199890008895243450658541227588666881
|
||||
16427171479924442928230863465674813919123162824586
|
||||
17866458359124566529476545682848912883142607690042
|
||||
24219022671055626321111109370544217506941658960408
|
||||
07198403850962455444362981230987879927244284909188
|
||||
84580156166097919133875499200524063689912560717606
|
||||
05886116467109405077541002256983155200055935729725
|
||||
71636269561882670428252483600823257530420752963450
|
8
solutions/ProjectEuler/008/solve.php
Normal file
8
solutions/ProjectEuler/008/solve.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
$haystack = str_replace(array('\n','\r\n'),'',file_get_contents('php://stdin'));
|
||||
for($i=0;$i<1000-4;$i++) {
|
||||
$cmp = $haystack[$i] * $haystack[$i+1] * $haystack[$i+2] * $haystack[$i+3] * $haystack[$i+4];
|
||||
$max = ($cmp > $max) ? $cmp : $max;
|
||||
}
|
||||
|
||||
echo $max;
|
13
solutions/ProjectEuler/008/solve.rb
Normal file
13
solutions/ProjectEuler/008/solve.rb
Normal file
@ -0,0 +1,13 @@
|
||||
haystack = ''
|
||||
ARGF.lines("\n") do |line|
|
||||
haystack << line.strip
|
||||
end
|
||||
|
||||
max = 0
|
||||
(1..(1000-4)).each do |index|
|
||||
compare = haystack[index].to_i * haystack[index+1].to_i * haystack[index+2].to_i * haystack[index+3].to_i * haystack[index+4].to_i
|
||||
if(compare > max)
|
||||
max = compare
|
||||
end
|
||||
end
|
||||
puts max
|
23
solutions/ProjectEuler/009/desc.yml
Normal file
23
solutions/ProjectEuler/009/desc.yml
Normal file
@ -0,0 +1,23 @@
|
||||
title: Find the only Pythagorean triplet, {a, b, c}, for which a + b + c = 1000.
|
||||
url: http://projecteuler.net/problem=9
|
||||
|
||||
desc: |
|
||||
A Pythagorean triplet is a set of three natural numbers, a b c, for which,
|
||||
a^2 + b^2 = c^2
|
||||
For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
|
||||
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
|
||||
Find the product abc.
|
||||
|
||||
solution: |
|
||||
Make a nested forloop for a and b in the range 1-1000 - Then c = 1000-a-b - Test if solution is valid.
|
||||
|
||||
solutions:
|
||||
solve.php:
|
||||
desc: Basic solution
|
||||
language: php
|
||||
solve.rb:
|
||||
desc: Basic solution
|
||||
language: ruby
|
||||
solve.c:
|
||||
desc: ANSI C solution compiled with TCC
|
||||
language: c
|
24
solutions/ProjectEuler/009/solve.c
Normal file
24
solutions/ProjectEuler/009/solve.c
Normal file
@ -0,0 +1,24 @@
|
||||
#include "stdio.h"
|
||||
#include "math.h"
|
||||
|
||||
int main( )
|
||||
{
|
||||
int a;
|
||||
int b;
|
||||
int c;
|
||||
int cmp;
|
||||
for(a = 1; a < 1000; a++)
|
||||
{
|
||||
for(b = 1; b < 1000; b++)
|
||||
{
|
||||
//Calculate the only valid value for c
|
||||
c = 1000 - a - b;
|
||||
if( pow(c,2) == (pow(a, 2 ) + pow( b, 2 )))
|
||||
{
|
||||
int result = a * b * c;
|
||||
printf("%i", result);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
11
solutions/ProjectEuler/009/solve.php
Normal file
11
solutions/ProjectEuler/009/solve.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
for ($a = 1; $a < 1000; $a++) {
|
||||
for ($b = 1; $b < 1000; $b++) {
|
||||
//Make it run reverse in order to find solution quickly
|
||||
$c = 1000 - $a - $b;
|
||||
if (pow($a, 2) + pow($b, 2) == pow($c, 2)) {
|
||||
echo $a * $b * $c;
|
||||
die;
|
||||
}
|
||||
}
|
||||
}
|
9
solutions/ProjectEuler/009/solve.rb
Normal file
9
solutions/ProjectEuler/009/solve.rb
Normal file
@ -0,0 +1,9 @@
|
||||
(1..1000).each do |a|
|
||||
(1..1000).each do |b|
|
||||
c = 1000 - a - b
|
||||
if( a ** 2 + b ** 2 == c ** 2)
|
||||
puts a * b *c
|
||||
exit
|
||||
end
|
||||
end
|
||||
end
|
20
solutions/ProjectEuler/010/desc.yml
Normal file
20
solutions/ProjectEuler/010/desc.yml
Normal file
@ -0,0 +1,20 @@
|
||||
title: Calculate the sum of all the primes below two million.
|
||||
url: http://projecteuler.net/problem=10
|
||||
|
||||
desc: |
|
||||
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
|
||||
Find the sum of all the primes below two million.
|
||||
|
||||
solution: |
|
||||
Use a primenumber tester
|
||||
|
||||
solutions:
|
||||
solve.php:
|
||||
desc: Basic solution
|
||||
language: php
|
||||
solve.rb:
|
||||
desc: Basic solution
|
||||
language: ruby
|
||||
solve.c:
|
||||
desc: C solution compiled with gcc-4.3.4 (Support for long long needed)
|
||||
language: c
|
28
solutions/ProjectEuler/010/solve.c
Normal file
28
solutions/ProjectEuler/010/solve.c
Normal file
@ -0,0 +1,28 @@
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
int is_prime( int prime) {
|
||||
|
||||
int max_test = sqrt( prime );
|
||||
|
||||
int i;
|
||||
for (i = 3; i <= max_test; i+=2){
|
||||
if (prime % i == 0){
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main( )
|
||||
{
|
||||
long long sum = 2+3;
|
||||
long number;
|
||||
for(number = 5; number < 2000000; number+=2) {
|
||||
if (is_prime(number))
|
||||
{
|
||||
sum += number;
|
||||
}
|
||||
}
|
||||
printf("%llu",sum);
|
||||
}
|
18
solutions/ProjectEuler/010/solve.php
Normal file
18
solutions/ProjectEuler/010/solve.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
function is_prime($prime) {
|
||||
$sqrt = sqrt($prime);
|
||||
for ($i = 3; $i <= $sqrt; $i+=2){
|
||||
if ($prime%$i == 0) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
$sum = 2+3;
|
||||
for($number = 5; $number < 2000000; $number+=2) {
|
||||
if (is_prime($number))
|
||||
{
|
||||
$sum += $number;
|
||||
}
|
||||
}
|
||||
echo $sum;
|
21
solutions/ProjectEuler/010/solve.rb
Normal file
21
solutions/ProjectEuler/010/solve.rb
Normal file
@ -0,0 +1,21 @@
|
||||
def is_prime(prime)
|
||||
sqrt = Math.sqrt(prime)
|
||||
i=3
|
||||
while(i <= sqrt)
|
||||
if (prime % i == 0)
|
||||
return false
|
||||
end
|
||||
i+= 2
|
||||
end
|
||||
return true
|
||||
end
|
||||
|
||||
number = 3
|
||||
sum = 5
|
||||
until(number >= 2000000)
|
||||
number += 2
|
||||
if(is_prime(number))
|
||||
sum += number
|
||||
end
|
||||
end
|
||||
puts sum;
|
20
solutions/ProjectEuler/011/desc.yml
Normal file
20
solutions/ProjectEuler/011/desc.yml
Normal file
@ -0,0 +1,20 @@
|
||||
title: What is the greatest product of four adjacent numbers on the same straight line in the 20 by 20 grid?
|
||||
url: http://projecteuler.net/problem=11
|
||||
|
||||
desc: |
|
||||
In the 2020 grid below, four numbers along a diagonal line have been marked in red.
|
||||
What is the greatest product of four adjacent numbers in any direction (up, down, left, right, or diagonally) in the 2020 grid?
|
||||
(See file input)
|
||||
|
||||
solution: |
|
||||
Bruteforce (Unless someone found a smarter way)
|
||||
|
||||
solutions:
|
||||
solve.php:
|
||||
desc: Expects the matrix as stdin
|
||||
language: php
|
||||
parameters: < ProjectEuler\011\input
|
||||
solve.rb:
|
||||
desc: Expects the matrix as stdin
|
||||
language: ruby
|
||||
parameters: < ProjectEuler\011\input
|
20
solutions/ProjectEuler/011/input
Normal file
20
solutions/ProjectEuler/011/input
Normal file
@ -0,0 +1,20 @@
|
||||
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
|
||||
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
|
||||
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
|
||||
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
|
||||
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
|
||||
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
|
||||
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
|
||||
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
|
||||
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
|
||||
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
|
||||
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
|
||||
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
|
||||
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
|
||||
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
|
||||
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
|
||||
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
|
||||
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
|
||||
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
|
||||
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
|
||||
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
|
39
solutions/ProjectEuler/011/solve.php
Normal file
39
solutions/ProjectEuler/011/solve.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
foreach(file('php://stdin') as $line) {
|
||||
$matrix[] = explode(' ', $line);
|
||||
}
|
||||
$max = 0;
|
||||
//Left and right
|
||||
for ($y = 0; $y < 19; $y++) {
|
||||
for ($x = 0; $x < 16; $x++) {
|
||||
$cmp = $matrix[$y][$x] * $matrix[$y][$x + 1] * $matrix[$y][$x + 2] * $matrix[$y][$x + 3];
|
||||
$max = ($cmp > $max) ? $cmp : $max;
|
||||
}
|
||||
}
|
||||
|
||||
//Vertical
|
||||
for($x = 0; $x < 19; $x++) {
|
||||
for ($y = 0; $y < 16; $y++) {
|
||||
$cmp = $matrix[$y][$x] * $matrix[$y + 1][$x] * $matrix[$y + 2][$x] * $matrix[$y + 3][$x];
|
||||
$max = ($cmp > $max) ? $cmp : $max;
|
||||
}
|
||||
}
|
||||
|
||||
//Diagonally left->down
|
||||
for($x = 0; $x < 16; $x++) {
|
||||
for ($y = 0; $y < 16; $y++) {
|
||||
$cmp = $matrix[$y][$x] * $matrix[$y + 1][$x + 1] * $matrix[$y + 2][$x + 2] * $matrix[$y + 3][$x + 3];
|
||||
$max = ($cmp > $max) ? $cmp : $max;
|
||||
}
|
||||
}
|
||||
|
||||
//Diagonally right->down
|
||||
for($x = 4; $x < 19; $x++) {
|
||||
for ($y = 0; $y < 16; $y++) {
|
||||
$cmp = $matrix[$y][$x] * $matrix[$y + 1][$x - 1] * $matrix[$y + 2][$x - 2] * $matrix[$y + 3][$x - 3];
|
||||
$max = ($cmp > $max) ? $cmp : $max;
|
||||
}
|
||||
}
|
||||
|
||||
echo $max;
|
53
solutions/ProjectEuler/011/solve.rb
Normal file
53
solutions/ProjectEuler/011/solve.rb
Normal file
@ -0,0 +1,53 @@
|
||||
matrix = []
|
||||
linenum = 0
|
||||
ARGF.lines("\n") do |line|
|
||||
matrix[linenum] = Array.new
|
||||
line.split.each do |str|
|
||||
matrix[linenum] << str.to_i
|
||||
end
|
||||
linenum += 1
|
||||
end
|
||||
|
||||
max = 0
|
||||
max = 0;
|
||||
#Left and right
|
||||
(0..19).each do |y|
|
||||
(0..16).each do |x|
|
||||
cmp = matrix[y][x] * matrix[y][x + 1] * matrix[y][x + 2] * matrix[y][x + 3];
|
||||
if (cmp > max)
|
||||
max = cmp
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
#Vertical
|
||||
(0..16).each do |y|
|
||||
(0..19).each do |x|
|
||||
cmp = matrix[y][x] * matrix[y + 1][x] * matrix[y + 2][x] * matrix[y + 3][x];
|
||||
if (cmp > max)
|
||||
max = cmp
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
#Diagonally left->down
|
||||
(0..16).each do |y|
|
||||
(0..16).each do |x|
|
||||
cmp = matrix[y][x] * matrix[y + 1][x + 1] * matrix[y + 2][x + 2] * matrix[y + 3][x + 3];
|
||||
if (cmp > max)
|
||||
max = cmp
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
#Diagonally right->down
|
||||
(0..16).each do |y|
|
||||
(3..19).each do |x|
|
||||
cmp = matrix[y][x] * matrix[y + 1][x - 1] * matrix[y + 2][x - 2] * matrix[y + 3][x - 3];
|
||||
if (cmp > max)
|
||||
max = cmp
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
puts max
|
30
solutions/ProjectEuler/012/desc.yml
Normal file
30
solutions/ProjectEuler/012/desc.yml
Normal file
@ -0,0 +1,30 @@
|
||||
title: What is the value of the first triangle number to have over five hundred divisors?
|
||||
url: http://projecteuler.net/problem=12
|
||||
|
||||
desc: |
|
||||
The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:
|
||||
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
|
||||
Let us list the factors of the first seven triangle numbers:
|
||||
1: 1
|
||||
3: 1,3
|
||||
6: 1,2,3,6
|
||||
10: 1,2,5,10
|
||||
15: 1,3,5,15
|
||||
21: 1,3,7,21
|
||||
28: 1,2,4,7,14,28
|
||||
We can see that 28 is the first triangle number to have over five divisors.
|
||||
What is the value of the first triangle number to have over five hundred divisors?
|
||||
|
||||
solution: |
|
||||
Bruteforce - Still kinda slow - Guessing it could be improved
|
||||
|
||||
solutions:
|
||||
solve.php:
|
||||
desc: Basic solution
|
||||
language: php
|
||||
solve.rb:
|
||||
desc: Basic solution
|
||||
language: ruby
|
||||
solve.c:
|
||||
desc: ANSI C solution compiled with TCC
|
||||
language: c
|
23
solutions/ProjectEuler/012/solve.c
Normal file
23
solutions/ProjectEuler/012/solve.c
Normal file
@ -0,0 +1,23 @@
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
int main( )
|
||||
{
|
||||
long num = 3;
|
||||
long tri_add = 2;
|
||||
long div;
|
||||
int divisors;
|
||||
int max = 0;
|
||||
do
|
||||
{
|
||||
num += ++tri_add;
|
||||
divisors = 2;
|
||||
int square_root = sqrt(num);
|
||||
for(div = 2; div <= square_root; div++)
|
||||
{
|
||||
if(num % div == 0)
|
||||
divisors+=2;
|
||||
}
|
||||
} while(divisors <= 500);
|
||||
printf( "%i", num );
|
||||
}
|
13
solutions/ProjectEuler/012/solve.php
Normal file
13
solutions/ProjectEuler/012/solve.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
$num = 3;
|
||||
$tri_add = 2;
|
||||
do {
|
||||
$num += ++$tri_add;
|
||||
$divisors = 2;
|
||||
$square_root = sqrt($num);
|
||||
for($div = 2; $div <= $square_root; $div++) {
|
||||
if($num % $div == 0)
|
||||
$divisors+= 2;
|
||||
}
|
||||
} while ($divisors <= 500);
|
||||
echo $num;
|
17
solutions/ProjectEuler/012/solve.rb
Normal file
17
solutions/ProjectEuler/012/solve.rb
Normal file
@ -0,0 +1,17 @@
|
||||
num = 3;
|
||||
tri_add = 2;
|
||||
begin
|
||||
tri_add += 1
|
||||
num += tri_add
|
||||
|
||||
divisors = 2;
|
||||
square_root = Math.sqrt(num);
|
||||
div = 2
|
||||
while(div <= square_root)
|
||||
if(num % div == 0)
|
||||
divisors += 2;
|
||||
end
|
||||
div += 1
|
||||
end
|
||||
end until (divisors > 500);
|
||||
puts num;
|
19
solutions/ProjectEuler/013/desc.yml
Normal file
19
solutions/ProjectEuler/013/desc.yml
Normal file
@ -0,0 +1,19 @@
|
||||
title: Find the first ten digits of the sum of one-hundred 50-digit numbers.
|
||||
url: http://projecteuler.net/problem=13
|
||||
|
||||
desc: |
|
||||
Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.
|
||||
(See file input)
|
||||
|
||||
solution: |
|
||||
Use a full precission math lib
|
||||
|
||||
solutions:
|
||||
solve.php:
|
||||
desc: Expects input on STDIN
|
||||
language: php
|
||||
parameters: < ProjectEuler\013\input
|
||||
solve.rb:
|
||||
desc: Expects input on STDIN
|
||||
language: ruby
|
||||
parameters: < ProjectEuler\013\input
|
100
solutions/ProjectEuler/013/input
Normal file
100
solutions/ProjectEuler/013/input
Normal file
@ -0,0 +1,100 @@
|
||||
37107287533902102798797998220837590246510135740250
|
||||
46376937677490009712648124896970078050417018260538
|
||||
74324986199524741059474233309513058123726617309629
|
||||
91942213363574161572522430563301811072406154908250
|
||||
23067588207539346171171980310421047513778063246676
|
||||
89261670696623633820136378418383684178734361726757
|
||||
28112879812849979408065481931592621691275889832738
|
||||
44274228917432520321923589422876796487670272189318
|
||||
47451445736001306439091167216856844588711603153276
|
||||
70386486105843025439939619828917593665686757934951
|
||||
62176457141856560629502157223196586755079324193331
|
||||
64906352462741904929101432445813822663347944758178
|
||||
92575867718337217661963751590579239728245598838407
|
||||
58203565325359399008402633568948830189458628227828
|
||||
80181199384826282014278194139940567587151170094390
|
||||
35398664372827112653829987240784473053190104293586
|
||||
86515506006295864861532075273371959191420517255829
|
||||
71693888707715466499115593487603532921714970056938
|
||||
54370070576826684624621495650076471787294438377604
|
||||
53282654108756828443191190634694037855217779295145
|
||||
36123272525000296071075082563815656710885258350721
|
||||
45876576172410976447339110607218265236877223636045
|
||||
17423706905851860660448207621209813287860733969412
|
||||
81142660418086830619328460811191061556940512689692
|
||||
51934325451728388641918047049293215058642563049483
|
||||
62467221648435076201727918039944693004732956340691
|
||||
15732444386908125794514089057706229429197107928209
|
||||
55037687525678773091862540744969844508330393682126
|
||||
18336384825330154686196124348767681297534375946515
|
||||
80386287592878490201521685554828717201219257766954
|
||||
78182833757993103614740356856449095527097864797581
|
||||
16726320100436897842553539920931837441497806860984
|
||||
48403098129077791799088218795327364475675590848030
|
||||
87086987551392711854517078544161852424320693150332
|
||||
59959406895756536782107074926966537676326235447210
|
||||
69793950679652694742597709739166693763042633987085
|
||||
41052684708299085211399427365734116182760315001271
|
||||
65378607361501080857009149939512557028198746004375
|
||||
35829035317434717326932123578154982629742552737307
|
||||
94953759765105305946966067683156574377167401875275
|
||||
88902802571733229619176668713819931811048770190271
|
||||
25267680276078003013678680992525463401061632866526
|
||||
36270218540497705585629946580636237993140746255962
|
||||
24074486908231174977792365466257246923322810917141
|
||||
91430288197103288597806669760892938638285025333403
|
||||
34413065578016127815921815005561868836468420090470
|
||||
23053081172816430487623791969842487255036638784583
|
||||
11487696932154902810424020138335124462181441773470
|
||||
63783299490636259666498587618221225225512486764533
|
||||
67720186971698544312419572409913959008952310058822
|
||||
95548255300263520781532296796249481641953868218774
|
||||
76085327132285723110424803456124867697064507995236
|
||||
37774242535411291684276865538926205024910326572967
|
||||
23701913275725675285653248258265463092207058596522
|
||||
29798860272258331913126375147341994889534765745501
|
||||
18495701454879288984856827726077713721403798879715
|
||||
38298203783031473527721580348144513491373226651381
|
||||
34829543829199918180278916522431027392251122869539
|
||||
40957953066405232632538044100059654939159879593635
|
||||
29746152185502371307642255121183693803580388584903
|
||||
41698116222072977186158236678424689157993532961922
|
||||
62467957194401269043877107275048102390895523597457
|
||||
23189706772547915061505504953922979530901129967519
|
||||
86188088225875314529584099251203829009407770775672
|
||||
11306739708304724483816533873502340845647058077308
|
||||
82959174767140363198008187129011875491310547126581
|
||||
97623331044818386269515456334926366572897563400500
|
||||
42846280183517070527831839425882145521227251250327
|
||||
55121603546981200581762165212827652751691296897789
|
||||
32238195734329339946437501907836945765883352399886
|
||||
75506164965184775180738168837861091527357929701337
|
||||
62177842752192623401942399639168044983993173312731
|
||||
32924185707147349566916674687634660915035914677504
|
||||
99518671430235219628894890102423325116913619626622
|
||||
73267460800591547471830798392868535206946944540724
|
||||
76841822524674417161514036427982273348055556214818
|
||||
97142617910342598647204516893989422179826088076852
|
||||
87783646182799346313767754307809363333018982642090
|
||||
10848802521674670883215120185883543223812876952786
|
||||
71329612474782464538636993009049310363619763878039
|
||||
62184073572399794223406235393808339651327408011116
|
||||
66627891981488087797941876876144230030984490851411
|
||||
60661826293682836764744779239180335110989069790714
|
||||
85786944089552990653640447425576083659976645795096
|
||||
66024396409905389607120198219976047599490197230297
|
||||
64913982680032973156037120041377903785566085089252
|
||||
16730939319872750275468906903707539413042652315011
|
||||
94809377245048795150954100921645863754710598436791
|
||||
78639167021187492431995700641917969777599028300699
|
||||
15368713711936614952811305876380278410754449733078
|
||||
40789923115535562561142322423255033685442488917353
|
||||
44889911501440648020369068063960672322193204149535
|
||||
41503128880339536053299340368006977710650566631954
|
||||
81234880673210146739058568557934581403627822703280
|
||||
82616570773948327592232845941706525094512325230608
|
||||
22918802058777319719839450180888072429661980811197
|
||||
77158542502016545090413245809786882778948721859617
|
||||
72107838435069186155435662884062257473692284509516
|
||||
20849603980134001723930671666823555245252804609722
|
||||
53503534226472524250874054075591789781264330331690
|
7
solutions/ProjectEuler/013/solve.php
Normal file
7
solutions/ProjectEuler/013/solve.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
$numbers = file('php://stdin');
|
||||
$total = 0;
|
||||
foreach($numbers as $number) {
|
||||
$total = bcadd($total,trim($number));
|
||||
}
|
||||
echo substr($total,0,10);
|
13
solutions/ProjectEuler/013/solve.rb
Normal file
13
solutions/ProjectEuler/013/solve.rb
Normal file
@ -0,0 +1,13 @@
|
||||
numbers = []
|
||||
linenum = 0
|
||||
ARGF.lines("\n") do |line|
|
||||
numbers[linenum] = Array.new
|
||||
numbers[linenum] = line.to_i
|
||||
linenum += 1
|
||||
end
|
||||
|
||||
sum=0
|
||||
numbers.each do |num|
|
||||
sum += num
|
||||
end
|
||||
puts sum.to_s[0..9]
|
27
solutions/ProjectEuler/014/desc.yml
Normal file
27
solutions/ProjectEuler/014/desc.yml
Normal file
@ -0,0 +1,27 @@
|
||||
title: Find the longest sequence using a starting number under one million.
|
||||
url: http://projecteuler.net/problem=14
|
||||
|
||||
desc: |
|
||||
The following iterative sequence is defined for the set of positive integers:
|
||||
n -> n/2 (n is even)
|
||||
n -> 3n + 1 (n is odd)
|
||||
Using the rule above and starting with 13, we generate the following sequence:
|
||||
13 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1
|
||||
It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.
|
||||
|
||||
Which starting number, under one million, produces the longest chain?
|
||||
NOTE: Once the chain starts the terms are allowed to go above one million.
|
||||
|
||||
solution: |
|
||||
Bruteforce
|
||||
|
||||
solutions:
|
||||
solve.php:
|
||||
desc: Basic solution
|
||||
language: php
|
||||
solve.rb:
|
||||
desc: Basic solution
|
||||
language: ruby
|
||||
solve.c:
|
||||
desc: ANSI C compiled with TCC
|
||||
language: c
|
34
solutions/ProjectEuler/014/solve.c
Normal file
34
solutions/ProjectEuler/014/solve.c
Normal file
@ -0,0 +1,34 @@
|
||||
#define MAX 1000000-1
|
||||
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
int main( )
|
||||
{
|
||||
unsigned int max_steps = 0;
|
||||
unsigned int max_start;
|
||||
|
||||
unsigned int test;
|
||||
|
||||
unsigned long ctest;
|
||||
unsigned int steps;
|
||||
|
||||
for(test = MAX; test > 1 ; test--)
|
||||
{
|
||||
|
||||
ctest = test;
|
||||
steps = 1;
|
||||
while(ctest != 1)
|
||||
{
|
||||
ctest = ( ctest % 2 ) ? ctest * 3 + 1 : ctest / 2;
|
||||
steps++;
|
||||
}
|
||||
if(steps > max_steps)
|
||||
{
|
||||
max_start = test;
|
||||
max_steps = steps;
|
||||
}
|
||||
|
||||
}
|
||||
printf( "%lu", max_start );
|
||||
}
|
13
solutions/ProjectEuler/014/solve.php
Normal file
13
solutions/ProjectEuler/014/solve.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
$max_steps = 0;
|
||||
for($test=999999;$test>1;$test--) {
|
||||
$ctest = $test;
|
||||
$steps = 1;
|
||||
while($ctest != 1) {
|
||||
$ctest = ($ctest % 2) ? $ctest*3+1 : $ctest/2;
|
||||
$steps++;
|
||||
}
|
||||
|
||||
if($steps > $max_steps) { $max_start = $test; $max_steps = $steps;}
|
||||
}
|
||||
echo $max_start;
|
18
solutions/ProjectEuler/014/solve.rb
Normal file
18
solutions/ProjectEuler/014/solve.rb
Normal file
@ -0,0 +1,18 @@
|
||||
max_start = 0
|
||||
max_steps = 0
|
||||
test = 1
|
||||
|
||||
while(test < 1000000)
|
||||
ctest = test
|
||||
steps = 1
|
||||
while(ctest != 1)
|
||||
ctest = ( ctest % 2 != 0) ? ctest * 3 + 1 : ctest / 2
|
||||
steps += 1
|
||||
end
|
||||
if(steps > max_steps)
|
||||
max_start = test
|
||||
max_steps = steps
|
||||
end
|
||||
test += 1
|
||||
end
|
||||
puts max_start
|
19
solutions/ProjectEuler/015/desc.yml
Normal file
19
solutions/ProjectEuler/015/desc.yml
Normal file
@ -0,0 +1,19 @@
|
||||
title: Starting in the top left corner in a 20 by 20 grid, how many routes are there to the bottom right corner?
|
||||
url: http://projecteuler.net/problem=15
|
||||
|
||||
desc: |
|
||||
Starting in the top left corner of a 22 grid, there are 6 routes (without backtracking) to the bottom right corner.
|
||||
How many routes are there through a 2020 grid?
|
||||
|
||||
solution: |
|
||||
(2n)!/n!^2 - Where n is the size of the grid -
|
||||
|
||||
todo: Find a more "programmable way" instead of this cheating mathsolution
|
||||
|
||||
solutions:
|
||||
solve.php:
|
||||
desc: Using BCMath to cope with large numbers
|
||||
language: php
|
||||
solve.rb:
|
||||
desc: Basic solution
|
||||
language: ruby
|
11
solutions/ProjectEuler/015/solve.php
Normal file
11
solutions/ProjectEuler/015/solve.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
define('GRID_SIZE',20);
|
||||
function factorial($num) {
|
||||
if($num == 0)
|
||||
return 1;
|
||||
else
|
||||
return bcmul($num,factorial(bcsub($num,1)));
|
||||
}
|
||||
|
||||
echo bcdiv(factorial(2* GRID_SIZE),bcpow(factorial( GRID_SIZE),2));
|
||||
|
11
solutions/ProjectEuler/015/solve.rb
Normal file
11
solutions/ProjectEuler/015/solve.rb
Normal file
@ -0,0 +1,11 @@
|
||||
GRID_SIZE = 20
|
||||
|
||||
def factorial(num)
|
||||
if(num == 0)
|
||||
return 1
|
||||
else
|
||||
return num * factorial(num - 1)
|
||||
end
|
||||
end
|
||||
|
||||
puts factorial(2 * GRID_SIZE) / factorial(GRID_SIZE) ** 2
|
17
solutions/ProjectEuler/016/desc.yml
Normal file
17
solutions/ProjectEuler/016/desc.yml
Normal file
@ -0,0 +1,17 @@
|
||||
title: What is the sum of the digits of the number 2^1000?
|
||||
url: http://projecteuler.net/problem=16
|
||||
|
||||
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: |
|
||||
Bruteforce
|
||||
|
||||
solutions:
|
||||
solve.php:
|
||||
desc: Using BCMath to cope with large numbers
|
||||
language: php
|
||||
solve.rb:
|
||||
desc: Basic solution
|
||||
language: ruby
|
7
solutions/ProjectEuler/016/solve.php
Normal file
7
solutions/ProjectEuler/016/solve.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
$num = bcpow(2,1000);
|
||||
$sum = 0;
|
||||
for($c =0; $c < strlen($num);$c++) {
|
||||
$sum += $num[$c];
|
||||
}
|
||||
echo $sum;
|
6
solutions/ProjectEuler/016/solve.rb
Normal file
6
solutions/ProjectEuler/016/solve.rb
Normal file
@ -0,0 +1,6 @@
|
||||
num = 2 ** 1000
|
||||
sum = 0
|
||||
num.to_s.chars do |digit|
|
||||
sum += digit.to_i
|
||||
end
|
||||
puts sum
|
15
solutions/ProjectEuler/017/desc.yml
Normal file
15
solutions/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
solutions/ProjectEuler/017/solve.php
Normal file
79
solutions/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;
|
25
solutions/ProjectEuler/018/desc.yml
Normal file
25
solutions/ProjectEuler/018/desc.yml
Normal file
@ -0,0 +1,25 @@
|
||||
title: Find the maximum sum travelling from the top of the triangle to the base.
|
||||
url: http://projecteuler.net/problem=18
|
||||
|
||||
desc: |
|
||||
By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.
|
||||
3
|
||||
7 4
|
||||
2 4 6
|
||||
8 5 9 3
|
||||
That is, 3 + 7 + 4 + 9 = 23.
|
||||
Find the maximum total from top to bottom of the triangle below
|
||||
(See file input)
|
||||
|
||||
|
||||
solution: Instead of bruteforcing from top to bottom simply add the numbers from the bottom up and let the "highest" number win in a shootout
|
||||
|
||||
solutions:
|
||||
solve.php:
|
||||
desc: Basic solution
|
||||
language: php
|
||||
parameters: < ProjectEuler\018\input
|
||||
solve.rb:
|
||||
desc: Basic solution
|
||||
language: ruby
|
||||
parameters: < ProjectEuler\018\input
|
15
solutions/ProjectEuler/018/input
Normal file
15
solutions/ProjectEuler/018/input
Normal file
@ -0,0 +1,15 @@
|
||||
75
|
||||
95 64
|
||||
17 47 82
|
||||
18 35 87 10
|
||||
20 04 82 47 65
|
||||
19 01 23 75 03 34
|
||||
88 02 77 73 07 63 67
|
||||
99 65 04 28 06 16 70 92
|
||||
41 41 26 56 83 40 80 70 33
|
||||
41 48 72 33 47 32 37 16 94 29
|
||||
53 71 44 65 25 43 91 52 97 51 14
|
||||
70 11 33 28 77 73 17 78 39 68 17 57
|
||||
91 71 52 38 17 14 91 43 58 50 27 29 48
|
||||
63 66 04 68 89 53 67 30 73 16 69 87 40 31
|
||||
04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
|
16
solutions/ProjectEuler/018/solve.php
Normal file
16
solutions/ProjectEuler/018/solve.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
foreach(file('php://stdin') as $line) {
|
||||
$tri[] = explode(' ',trim($line));
|
||||
}
|
||||
for($y=count($tri);$y>=0;$y--) {
|
||||
|
||||
for($x=0;$x<count($tri[$y]);$x++) {
|
||||
$cur = $tri[$y][$x];
|
||||
if($cur + $tri[$y+1][$x] > $cur + $tri[$y+1][$x+1]) {
|
||||
$tri[$y][$x] = $cur + $tri[$y+1][$x];
|
||||
} else {
|
||||
$tri[$y][$x] = $cur + $tri[$y+1][$x+1];
|
||||
}
|
||||
}
|
||||
}
|
||||
echo $tri[0][0];
|
20
solutions/ProjectEuler/018/solve.rb
Normal file
20
solutions/ProjectEuler/018/solve.rb
Normal file
@ -0,0 +1,20 @@
|
||||
tri = []
|
||||
linenum = 0
|
||||
ARGF.lines("\n") do |line|
|
||||
tri[linenum] = Array.new
|
||||
line.split.each do |str|
|
||||
tri[linenum] << str.to_i
|
||||
end
|
||||
linenum += 1
|
||||
end
|
||||
|
||||
(0..13).reverse_each do |y|
|
||||
0.upto(tri[y].size()-1) do |x|
|
||||
if( tri[y+1][x] > tri[y+1][x+1])
|
||||
tri[y][x] = tri[y][x] + tri[y+1][x]
|
||||
else
|
||||
tri[y][x] = tri[y][x] + tri[y+1][x+1]
|
||||
end
|
||||
end
|
||||
end
|
||||
puts tri[0][0]
|
25
solutions/ProjectEuler/019/desc.yml
Normal file
25
solutions/ProjectEuler/019/desc.yml
Normal file
@ -0,0 +1,25 @@
|
||||
title: How many Sundays fell on the first of the month during the twentieth century?
|
||||
url: http://projecteuler.net/problem=19
|
||||
|
||||
desc: |
|
||||
You are given the following information, but you may prefer to do some research for yourself.
|
||||
1 Jan 1900 was a Monday.
|
||||
Thirty days has September,
|
||||
April, June and November.
|
||||
All the rest have thirty-one,
|
||||
Saving February alone,
|
||||
Which has twenty-eight, rain or shine.
|
||||
And on leap years, twenty-nine.
|
||||
A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.
|
||||
How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
|
||||
|
||||
|
||||
solution: Instead of using buildtin DateTime language features this use a homemade algorithm
|
||||
|
||||
solutions:
|
||||
solve.php:
|
||||
desc: Basic solution
|
||||
language: php
|
||||
solve.rb:
|
||||
desc: Basic solution
|
||||
language: ruby
|
38
solutions/ProjectEuler/019/solve.php
Normal file
38
solutions/ProjectEuler/019/solve.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
$days_in_month = array(
|
||||
1 => 31,
|
||||
2 => 28,
|
||||
3 => 31,
|
||||
4 => 30,
|
||||
5 => 31,
|
||||
6 => 30,
|
||||
7 => 31,
|
||||
8 => 31,
|
||||
9 => 30,
|
||||
10 => 31,
|
||||
11 => 30,
|
||||
12 => 31
|
||||
);
|
||||
$day_cycle = 0;
|
||||
for($year=1900; $year <= 2000; $year++) {
|
||||
for($month=1; $month <= 12; $month++) {
|
||||
|
||||
$cdates = $days_in_month[$month];
|
||||
if($month == 2) {
|
||||
if($year % 4 == 0) {
|
||||
$cdates = 29;
|
||||
}
|
||||
if($years % 100 == 0) {
|
||||
$cdates = 28;
|
||||
}
|
||||
if($years % 400 == 0) {
|
||||
$cdates = 29;
|
||||
}
|
||||
}
|
||||
$day_cycle += $cdates;
|
||||
if($day_cycle % 7 == 0) {
|
||||
if($year >= 1901) {$sum++; }
|
||||
}
|
||||
}
|
||||
}
|
||||
echo $sum;
|
9
solutions/ProjectEuler/019/solve.rb
Normal file
9
solutions/ProjectEuler/019/solve.rb
Normal file
@ -0,0 +1,9 @@
|
||||
require "date"
|
||||
sundays = 0
|
||||
(1901..2000).each do |year|
|
||||
(1..12).each do |month|
|
||||
dt = Date.new(year,month,1)
|
||||
sundays += (dt.wday == 0) ? 1 : 0
|
||||
end
|
||||
end
|
||||
puts sundays
|
18
solutions/ProjectEuler/020/desc.yml
Normal file
18
solutions/ProjectEuler/020/desc.yml
Normal file
@ -0,0 +1,18 @@
|
||||
title: Find the sum of digits in 100!
|
||||
url: http://projecteuler.net/problem=20
|
||||
|
||||
desc: |
|
||||
n! means n * (n - 1) * ... 3 * 2 * 1
|
||||
For example, 10! = 10 * 9 * ... * 3 * 2 * 1 = 3628800,
|
||||
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.
|
||||
Find the sum of the digits in the number 100!
|
||||
|
||||
solution: Bruteforce
|
||||
|
||||
solutions:
|
||||
solve.php:
|
||||
desc: Basic solution
|
||||
language: php
|
||||
solve.rb:
|
||||
desc: Basic solution
|
||||
language: ruby
|
15
solutions/ProjectEuler/020/solve.php
Normal file
15
solutions/ProjectEuler/020/solve.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
define('FACTORIAL',100);
|
||||
|
||||
function factorial($num) {
|
||||
if($num == 0)
|
||||
return 1;
|
||||
else
|
||||
return bcmul($num,factorial(bcsub($num,1)));
|
||||
}
|
||||
$sum = 0;
|
||||
$fac = factorial(FACTORIAL);
|
||||
for($c = 0; $c < strlen($fac); $c++) {
|
||||
$sum += $fac[$c];
|
||||
}
|
||||
echo $sum;
|
16
solutions/ProjectEuler/020/solve.rb
Normal file
16
solutions/ProjectEuler/020/solve.rb
Normal file
@ -0,0 +1,16 @@
|
||||
FACTORIAL = 100
|
||||
def factorial(num)
|
||||
if(num == 0)
|
||||
return 1
|
||||
else
|
||||
return num * factorial(num - 1)
|
||||
end
|
||||
end
|
||||
|
||||
number = factorial(FACTORIAL)
|
||||
sum = 0
|
||||
|
||||
number.to_s.each_char do |c|
|
||||
sum += c.to_i
|
||||
end
|
||||
puts sum
|
23
solutions/ProjectEuler/021/desc.yml
Normal file
23
solutions/ProjectEuler/021/desc.yml
Normal file
@ -0,0 +1,23 @@
|
||||
title: Evaluate the sum of all amicable pairs under 10000.
|
||||
url: http://projecteuler.net/problem=21
|
||||
|
||||
desc: |
|
||||
Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).
|
||||
If d(a) = b and d(b) = a, where a b, then a and b are an amicable pair and each of a and b are called amicable numbers.
|
||||
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: Bruteforce
|
||||
|
||||
todo: Implement caching in other solutions
|
||||
|
||||
solutions:
|
||||
solve.php:
|
||||
desc: (Uses a cache to find allready calculated values for d()
|
||||
language: php
|
||||
solve.rb:
|
||||
desc: Basic solution
|
||||
language: ruby
|
||||
solve.c:
|
||||
desc: ANSI C solution (Compiled with TCC)
|
||||
language: c
|
28
solutions/ProjectEuler/021/solve.c
Normal file
28
solutions/ProjectEuler/021/solve.c
Normal file
@ -0,0 +1,28 @@
|
||||
#define MAX 10000
|
||||
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
int d( int input) {
|
||||
int sum = 0;
|
||||
int n;
|
||||
for(n=1;n<input;n++) {
|
||||
if(input % n == 0)
|
||||
sum += n;
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
int main( )
|
||||
{
|
||||
int result = 0;
|
||||
int number;
|
||||
int d_sum;
|
||||
for(number = 1; number < MAX; number++) {
|
||||
d_sum = d(number);
|
||||
if(number != d_sum && number == d(d_sum)) {
|
||||
result += number;
|
||||
}
|
||||
}
|
||||
printf("%d", result);
|
||||
}
|
23
solutions/ProjectEuler/021/solve.php
Normal file
23
solutions/ProjectEuler/021/solve.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
define('MAX',10000);
|
||||
|
||||
function d($int) {
|
||||
static $cache;
|
||||
if(isset($cache[$int])) {return $cache[$int]; }
|
||||
$sum = 0;
|
||||
for($n=1;$n<$int;$n++) {
|
||||
if($int % $n == 0)
|
||||
$sum += $n;
|
||||
}
|
||||
$cache[$int] = $sum;
|
||||
return $sum;
|
||||
}
|
||||
|
||||
$result = 0;
|
||||
for($number = 1; $number < MAX; $number++) {
|
||||
$d_sum = d($number);
|
||||
if($number != $d_sum AND $number == d($d_sum)) {
|
||||
$result += $number;
|
||||
}
|
||||
}
|
||||
echo $result;
|
20
solutions/ProjectEuler/021/solve.rb
Normal file
20
solutions/ProjectEuler/021/solve.rb
Normal file
@ -0,0 +1,20 @@
|
||||
MAX = 10000
|
||||
|
||||
def d(num)
|
||||
sum = 0
|
||||
1.upto(num-1) do |d|
|
||||
if(num % d == 0)
|
||||
sum += d
|
||||
end
|
||||
end
|
||||
return sum
|
||||
end
|
||||
|
||||
result = 0
|
||||
Range.new(0,MAX,true).each do |number|
|
||||
d_sum = d(number)
|
||||
if(number != d_sum && number == d(d_sum))
|
||||
result += number
|
||||
end
|
||||
end
|
||||
puts result
|
15
solutions/ProjectEuler/022/desc.yml
Normal file
15
solutions/ProjectEuler/022/desc.yml
Normal file
@ -0,0 +1,15 @@
|
||||
title: What is the total of all the name scores in the file of first names?
|
||||
url: http://projecteuler.net/problem=22
|
||||
|
||||
desc: |
|
||||
Using input, a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.
|
||||
For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 53 = 49714.
|
||||
What is the total of all the name scores in the file?
|
||||
(See file input)
|
||||
solution: Bruteforce
|
||||
|
||||
solutions:
|
||||
solve.php:
|
||||
desc: Expects data on STDIN
|
||||
language: php
|
||||
parameters: < ProjectEuler\022\input
|
1
solutions/ProjectEuler/022/input
Normal file
1
solutions/ProjectEuler/022/input
Normal file
File diff suppressed because one or more lines are too long
12
solutions/ProjectEuler/022/solve.php
Normal file
12
solutions/ProjectEuler/022/solve.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
$names = explode('","',substr(file_get_contents('php://STDIN'),1,-1));
|
||||
sort($names);
|
||||
|
||||
foreach($names as $line=>$name) {
|
||||
for($c =0; $c < strlen($name); $c++) {
|
||||
$char_sum += ord($name[$c]) - 64;
|
||||
}
|
||||
$result += $char_sum * ($line+1);
|
||||
$char_sum = 0;
|
||||
}
|
||||
echo $result;
|
15
solutions/ProjectEuler/023/desc.yml
Normal file
15
solutions/ProjectEuler/023/desc.yml
Normal file
@ -0,0 +1,15 @@
|
||||
title: Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.
|
||||
url: http://projecteuler.net/problem=23
|
||||
|
||||
desc: |
|
||||
A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.
|
||||
A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.
|
||||
As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.
|
||||
Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.
|
||||
todo: Improve algorithm
|
||||
solution: From we http://mathworld.wolfram.com/AbundantNumber.html we cheat and limit our search to 20161
|
||||
|
||||
solutions:
|
||||
solve.php:
|
||||
desc: Very slow - takes around 1 minut on my Core 2 Duo
|
||||
language: php
|
27
solutions/ProjectEuler/023/solve.php
Normal file
27
solutions/ProjectEuler/023/solve.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
function abundant($input) {
|
||||
$max = floor(sqrt($input));
|
||||
$sum = 1;
|
||||
for($div=2;$div<=$max ;$div++) {
|
||||
if($input % $div == 0) {
|
||||
$sum += ($input/$div != $div) ? $input/$div + $div : $div;
|
||||
if($sum > $input)
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
//Find all abundant numbers
|
||||
for($number = 12; $number <= 28123 ; $number++) {
|
||||
if(abundant($number)) { $adjnum[] = $number; }
|
||||
}
|
||||
|
||||
$sum = array_sum(range(1,23));
|
||||
for($test = 24; $test <= 20162; $test++) {
|
||||
$nadundant = true;
|
||||
for($index = 0; $adjnum[$index] < $test; $index++) {
|
||||
if(abundant($test - $adjnum[$index])) {$nadundant = false; break; }
|
||||
}
|
||||
if($nadundant) {$sum += $test; }
|
||||
}
|
||||
echo $sum;
|
13
solutions/ProjectEuler/024/desc.yml
Normal file
13
solutions/ProjectEuler/024/desc.yml
Normal file
@ -0,0 +1,13 @@
|
||||
title: What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
|
||||
url: http://projecteuler.net/problem=24
|
||||
|
||||
desc: |
|
||||
A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are:
|
||||
012 021 102 120 201 210
|
||||
What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
|
||||
|
||||
solution: Use factoring
|
||||
solutions:
|
||||
solve.php:
|
||||
desc: Basic Solution
|
||||
language: php
|
26
solutions/ProjectEuler/024/solve.php
Normal file
26
solutions/ProjectEuler/024/solve.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
define('NUM_PERMUTATION',1000000);
|
||||
define('DIGITS',9);
|
||||
$target = NUM_PERMUTATION - 1;
|
||||
|
||||
$digits = range(0, 9);
|
||||
$permus = array(1 => 1);
|
||||
for ($i = 2; $i <= 9; $i++) {
|
||||
$permus[$i] = $permus[$i - 1] * $i;
|
||||
}
|
||||
$permus = array_reverse($permus);
|
||||
$values = array();
|
||||
|
||||
foreach ($permus as $n) {
|
||||
$values[] = floor($target / $n);
|
||||
$target = $target%$n;
|
||||
}
|
||||
|
||||
$result = "";
|
||||
foreach ($values as $val) {
|
||||
$result .= $digits[$val];
|
||||
unset($digits[$val]);
|
||||
sort($digits);
|
||||
}
|
||||
$result .= $digits[0];
|
||||
echo $result;
|
30
solutions/ProjectEuler/025/desc.yml
Normal file
30
solutions/ProjectEuler/025/desc.yml
Normal file
@ -0,0 +1,30 @@
|
||||
title: What is the first term in the Fibonacci sequence to contain 1000 digits?
|
||||
url: http://projecteuler.net/problem=25
|
||||
|
||||
desc: |
|
||||
The Fibonacci sequence is defined by the recurrence relation:
|
||||
Fn = Fn1 + Fn2, where F1 = 1 and F2 = 1.
|
||||
Hence the first 12 terms will be:
|
||||
F1 = 1
|
||||
F2 = 1
|
||||
F3 = 2
|
||||
F4 = 3
|
||||
F5 = 5
|
||||
F6 = 8
|
||||
F7 = 13
|
||||
F8 = 21
|
||||
F9 = 34
|
||||
F10 = 55
|
||||
F11 = 89
|
||||
F12 = 144
|
||||
The 12th term, F12, is the first term to contain three digits.
|
||||
What is the first term in the Fibonacci sequence to contain 1000 digits?
|
||||
solution: Bruteforce
|
||||
|
||||
solutions:
|
||||
solve.php:
|
||||
desc: Basic Solution - needs BCMath
|
||||
language: php
|
||||
solve.rb:
|
||||
desc: Basic solution
|
||||
language: ruby
|
13
solutions/ProjectEuler/025/solve.php
Normal file
13
solutions/ProjectEuler/025/solve.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
define('DIGITS',1000);
|
||||
|
||||
$current = 1;
|
||||
$prev = 1;
|
||||
$term = 2;
|
||||
while(strlen($current) < DIGITS) {
|
||||
$term++;
|
||||
$next = bcadd($current,$prev);
|
||||
$prev = $current;
|
||||
$current = $next;
|
||||
}
|
||||
echo $term;
|
13
solutions/ProjectEuler/025/solve.rb
Normal file
13
solutions/ProjectEuler/025/solve.rb
Normal file
@ -0,0 +1,13 @@
|
||||
DIGITS = 1000
|
||||
|
||||
max = 10 ** (DIGITS-1)
|
||||
fcurrent = 1
|
||||
fprev = 1
|
||||
term = 2
|
||||
while fcurrent < max do
|
||||
term = term + 1
|
||||
fnext = fcurrent + fprev
|
||||
fprev = fcurrent;
|
||||
fcurrent = fnext;
|
||||
end
|
||||
puts term
|
23
solutions/ProjectEuler/026/desc.yml
Normal file
23
solutions/ProjectEuler/026/desc.yml
Normal file
@ -0,0 +1,23 @@
|
||||
title: Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.
|
||||
url: http://projecteuler.net/problem=26
|
||||
|
||||
desc: |
|
||||
A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given:
|
||||
1/2 = 0.5
|
||||
1/3 = 0.(3)
|
||||
1/4 = 0.25
|
||||
1/5 = 0.2
|
||||
1/6 = 0.1(6)
|
||||
1/7 = 0.(142857)
|
||||
1/8 = 0.125
|
||||
1/9 = 0.(1)
|
||||
1/10 = 0.1
|
||||
Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. It can be seen that 1/7 has a 6-digit recurring cycle.
|
||||
Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.
|
||||
|
||||
solution: Bruteforce
|
||||
|
||||
solutions:
|
||||
solve.php:
|
||||
desc: Basic Solution - needs BCMath
|
||||
language: php
|
20
solutions/ProjectEuler/026/solve.php
Normal file
20
solutions/ProjectEuler/026/solve.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
define('MAX',1000);
|
||||
|
||||
$max_len = 0;
|
||||
for($div = MAX; $div > 2; $div--) {
|
||||
$result = bcdiv('1',(string)$div,2000);
|
||||
|
||||
for($len = 2; $len < 1000; $len++) {
|
||||
if(substr($result,10,$len) === substr($result,10+$len,$len)) {
|
||||
if($len > $max_len) {
|
||||
echo "Div: ".$div. " Len: ".$len."\n";
|
||||
$max_len = $len;
|
||||
$max_div = $div;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
echo $max_div;
|
13
solutions/ProjectEuler/026/solve.rb
Normal file
13
solutions/ProjectEuler/026/solve.rb
Normal file
@ -0,0 +1,13 @@
|
||||
DIGITS = 1000
|
||||
|
||||
max = 10 ** (DIGITS-1)
|
||||
fcurrent = 1
|
||||
fprev = 1
|
||||
term = 2
|
||||
while fcurrent < max do
|
||||
term = term + 1
|
||||
fnext = fcurrent + fprev
|
||||
fprev = fcurrent;
|
||||
fcurrent = fnext;
|
||||
end
|
||||
puts term
|
20
solutions/ProjectEuler/027/desc.yml
Normal file
20
solutions/ProjectEuler/027/desc.yml
Normal file
@ -0,0 +1,20 @@
|
||||
title: Quadratic primes
|
||||
url: http://projecteuler.net/problem=27
|
||||
|
||||
desc: |
|
||||
Euler discovered the remarkable quadratic formula:
|
||||
n<> + n + 41
|
||||
It turns out that the formula will produce 40 primes for the consecutive values n = 0 to 39. However, when n = 40, 402 + 40 + 41 = 40(40 + 1) + 41 is divisible by 41, and certainly when n = 41, 41<34> + 41 + 41 is clearly divisible by 41.
|
||||
The incredible formula n<> - 79n + 1601 was discovered, which produces 80 primes for the consecutive values n = 0 to 79. The product of the coefficients, -79 and 1601, is -126479.
|
||||
Considering quadratics of the form:
|
||||
n<> + an + b, where |a| < 1000 and |b| < 1000
|
||||
where |n| is the modulus/absolute value of n
|
||||
e.g. |11| = 11 and |-4| = 4
|
||||
Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n = 0
|
||||
|
||||
solution: Bruteforce
|
||||
|
||||
solutions:
|
||||
solve.php:
|
||||
desc: Basic Solution - needs BCMath
|
||||
language: php
|
30
solutions/ProjectEuler/027/problem27.php
Normal file
30
solutions/ProjectEuler/027/problem27.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
function is_prime($prime) {
|
||||
if($prime < 1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if($prime == 1)
|
||||
return true;
|
||||
if($prime == 2)
|
||||
return true;
|
||||
$sqrt = sqrt($prime);
|
||||
for ($i = 3; $i <= $sqrt; $i+=2){
|
||||
if ($prime%$i == 0) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
$high = 0;
|
||||
$max_primes = 0;
|
||||
for($a=-999; $a<1000; $a++) {
|
||||
for($b=-999; $b<1000; $b=$b+2) {
|
||||
$n=0;
|
||||
while(is_prime($n*$n + $a * $n + $b)) {
|
||||
$n++;
|
||||
if($n > $max_primes) {
|
||||
$max_primes = $n;
|
||||
echo "max: $n a: $a b: $b\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
32
solutions/ProjectEuler/027/solve.php
Normal file
32
solutions/ProjectEuler/027/solve.php
Normal file
@ -0,0 +1,32 @@
|
||||
<?php
|
||||
function is_prime($prime) {
|
||||
if($prime == 1)
|
||||
return true;
|
||||
if($prime == 2)
|
||||
return true;
|
||||
$sqrt = sqrt($prime);
|
||||
for ($i = 3; $i <= $sqrt; $i+=2){
|
||||
if ($prime%$i == 0) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
$high = 0;
|
||||
$max_primes = 0;
|
||||
for($a=-999; $a<1000; $a++) {
|
||||
for($b=-999; $b<1000; $b=$b+2) {
|
||||
|
||||
$n=0;
|
||||
while(true) {
|
||||
echo $n*$n + $a * $n + $b . "\n";
|
||||
if(!is_prime($n*$n + $a * $n + $b)) {
|
||||
if($n > $max_primes) {
|
||||
$max_primes = $n;
|
||||
echo "max: $n a: $a b: $b\n";
|
||||
}
|
||||
break;
|
||||
}
|
||||
$n++;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
13
solutions/ProjectEuler/027/solve.rb
Normal file
13
solutions/ProjectEuler/027/solve.rb
Normal file
@ -0,0 +1,13 @@
|
||||
DIGITS = 1000
|
||||
|
||||
max = 10 ** (DIGITS-1)
|
||||
fcurrent = 1
|
||||
fprev = 1
|
||||
term = 2
|
||||
while fcurrent < max do
|
||||
term = term + 1
|
||||
fnext = fcurrent + fprev
|
||||
fprev = fcurrent;
|
||||
fcurrent = fnext;
|
||||
end
|
||||
puts term
|
32
solutions/ProjectEuler/028/desc.yml
Normal file
32
solutions/ProjectEuler/028/desc.yml
Normal file
@ -0,0 +1,32 @@
|
||||
title: What is the sum of both diagonals in a 1001 by 1001 spiral?
|
||||
url: http://projecteuler.net/problem=28
|
||||
|
||||
desc: |
|
||||
Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:
|
||||
21 22 23 24 25
|
||||
20 7 8 9 10
|
||||
19 6 1 2 11
|
||||
18 5 4 3 12
|
||||
17 16 15 14 13
|
||||
It can be verified that the sum of the numbers on the diagonals is 101.
|
||||
What is the sum of the numbers on the diagonals in a 1001 by 1001 spiral formed in the same way?
|
||||
|
||||
solution: |
|
||||
Step around in the spiral (add 2 - 3 times, then add 4 - 3times, then 6 - times etc)
|
||||
|
||||
solutions:
|
||||
solve.php:
|
||||
desc: Basic solution
|
||||
language: php
|
||||
solve.rb:
|
||||
desc: Basic solution
|
||||
language: ruby
|
||||
solve.c:
|
||||
desc: ANSI C solution (Tested with TCC)
|
||||
language: c
|
||||
solve.js:
|
||||
desc: NodeJS solution
|
||||
language: javascript
|
||||
solve.lua:
|
||||
desc: Basic solution
|
||||
language: lua
|
19
solutions/ProjectEuler/028/solve.c
Normal file
19
solutions/ProjectEuler/028/solve.c
Normal file
@ -0,0 +1,19 @@
|
||||
#include <stdio.h>
|
||||
#define SIZE 1001
|
||||
#define SIDES 4
|
||||
|
||||
int main( )
|
||||
{
|
||||
|
||||
int sum = 1;
|
||||
int result=1;
|
||||
int addition;
|
||||
for(addition = 2; addition <= SIZE; addition+=2) {
|
||||
int cside;
|
||||
for(cside = 0; cside < SIDES; cside++) {
|
||||
sum += addition;
|
||||
result += sum;
|
||||
}
|
||||
}
|
||||
printf( "%i", result );
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user