Solved ProjectEuler/035

This commit is contained in:
FuryFire
2012-04-25 15:26:13 +02:00
parent 6cc3b0ad82
commit 62c2c8f47b
7 changed files with 93 additions and 2 deletions

18
ProjectEuler/036/desc.yml Normal file
View File

@ -0,0 +1,18 @@
title: Find the sum of all numbers less than one million, which are palindromic in base 10 and base 2.
url: http://projecteuler.net/problem=36
desc: |
The decimal number, 585 = 1001001001 (binary), is palindromic in both bases.
Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.
(Please note that the palindromic number, in either base, may not include leading zeros.)
solution: We can skip all even numbers since a even number would end in binary 10 - In reverse that would be 01 and we are told to skip leading 0's
solutions:
solve.php:
desc: Basic solution
language: php
solve.rb:
desc: Basic solution in Ruby
language: ruby

View File

@ -0,0 +1,12 @@
<?php
define('MAX',1000000);
$result = 0;
for($i=1;$i<MAX;$i+=2) {
if($i == strrev($i)) {
$bin = decbin($i);
if($bin == strrev($bin)) {
$result += $i;
}
}
}
echo $result;

10
ProjectEuler/036/solve.rb Normal file
View File

@ -0,0 +1,10 @@
MAX = 1000000
result = 0
(1..MAX).step(2) { |i|
if(i.to_s == i.to_s.reverse)
if(i.to_s(2) == i.to_s(2).reverse)
result += i
end
end
}
print result