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

2
CT.rb

@ -30,7 +30,7 @@ case ARGV[1]
require 'terminal-table'
files = Dir[ARGV[0]+"/**/*/desc.yml"]
files.sort
files.sort!
entry = []
files.each do |file|
yaml = YAML::load_file(file)

@ -6,7 +6,7 @@ desc: |
Find the sum of all numbers which are equal to the sum of the factorial of their digits.
Note: as 1! = 1 and 2! = 2 are not sums they are not included.
solution: Bruteforce
solutons:
solutions:
solve.php:
desc: Basic solution
language: php

11
ProjectEuler/035/desc.yml Normal file

@ -0,0 +1,11 @@
title: How many circular primes are there below one million?
url: http://projecteuler.net/problem=35
desc: |
The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.
There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.
How many circular primes are there below one million?
solution: Bruteforce
solutions:

@ -0,0 +1,40 @@
<?php
define('MAX',1000000);
function is_prime($prime) {
$sqrt = sqrt($prime);
for ($i = 3; $i <= $sqrt; $i+=2){
if ($prime%$i == 0) return false;
}
return true;
}
function fact($int){
static $facts = array(1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880);
return $facts[$int];
}
$primes = array(2);
$primes_ordered = array(2);
for($i=3;$i<MAX;$i+=2) {
if(is_prime($i)) {
//$primes[] = $i;
$temp = str_split($i);
sort($temp);
$primes_ordered[] = implode($temp);
}
}
echo "Found primes below 1mil\n";
$result = 0;
foreach($primes_ordered as $p) {
$counts = fact(strlen($p));
foreach($primes_ordered as $order) {
if($p == $order) {
$counts--;
}
}
if($counts == 0) {
//echo $p."\n";
$result++;
}
}
echo $result;

18
ProjectEuler/036/desc.yml Normal 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

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

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