Initial import
This commit is contained in:
21
ProjectEuler/002/desc.yml
Normal file
21
ProjectEuler/002/desc.yml
Normal file
@ -0,0 +1,21 @@
|
||||
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
|
18
ProjectEuler/002/solve.c
Normal file
18
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 );
|
||||
}
|
12
ProjectEuler/002/solve.php
Normal file
12
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
ProjectEuler/002/solve.rb
Normal file
11
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;
|
Reference in New Issue
Block a user