Solved ProjectEuler/028,048

This commit is contained in:
FuryFire 2011-04-24 16:22:52 +02:00
parent 80dbb818a8
commit 6cc3b0ad82
10 changed files with 142 additions and 0 deletions

@ -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

32
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
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 );
}

13
ProjectEuler/028/solve.js Normal file

@ -0,0 +1,13 @@
SIZE = 1001;
SIDES = 4
sum = 1;
result=1;
for(addition = 2; addition <= SIZE; addition+=2) {
for(cside = 0; cside < SIDES; cside++) {
sum += addition;
result += sum;
}
}
console.log(result);

@ -0,0 +1,12 @@
SIZE = 1001;
SIDES = 4;
sum = 1;
result=1;
for addition = 2,SIZE,2 do
for cside=0,(SIDES-1) do
sum = sum + addition;
result = result + sum;
end
end
print(result)

@ -0,0 +1,12 @@
<?php
define('SIZE',1001);
define('SIDES',4);
$sum = 1;
$result=1;
for($addition = 2; $addition <= SIZE; $addition+=2) {
for($cside = 0; $cside < SIDES; $cside++) {
$sum += $addition;
$result += $sum;
}
}
echo $result;

12
ProjectEuler/028/solve.rb Normal file

@ -0,0 +1,12 @@
SIZE = 1001
SIDES = 4
sum = 1;
result=1;
(2...SIZE).step(2) { |add|
SIDES.downto(1) {
sum += add;
result += sum;
}
}
puts result

17
ProjectEuler/048/desc.yml Normal file

@ -0,0 +1,17 @@
title: Find the last ten digits of 11 + 22 + ... + 10001000.
url: http://projecteuler.net/problem=48
desc: |
The series, 1^1 + 2^2 + 3^3 + ... + 10^10 = 10405071317.
Find the last ten digits of the series, 1^1 + 2^2 + 3^3 + ... + 1000^1000.
solution: |
Bruteforce
solutions:
solve.php:
desc: Using BCMath to cope with large numbers
language: php
solve.rb:
desc: Basic solution
language: ruby

@ -0,0 +1,7 @@
<?php
define('MAX',1000);
$sum = 0;
for($c =1; $c <= MAX;$c++) {
$sum = bcadd($sum,bcpow($c,$c));
}
echo substr($sum,-10,10);

@ -0,0 +1,6 @@
MAX = 1000
sum = 0
(1..MAX).each do |digit|
sum += digit**digit
end
puts sum.to_s[-10..-1]