Solved ProjectEuler/035
This commit is contained in:
2
CT.rb
2
CT.rb
@ -30,7 +30,7 @@ case ARGV[1]
|
|||||||
|
|
||||||
require 'terminal-table'
|
require 'terminal-table'
|
||||||
files = Dir[ARGV[0]+"/**/*/desc.yml"]
|
files = Dir[ARGV[0]+"/**/*/desc.yml"]
|
||||||
files.sort
|
files.sort!
|
||||||
entry = []
|
entry = []
|
||||||
files.each do |file|
|
files.each do |file|
|
||||||
yaml = YAML::load_file(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.
|
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.
|
Note: as 1! = 1 and 2! = 2 are not sums they are not included.
|
||||||
solution: Bruteforce
|
solution: Bruteforce
|
||||||
solutons:
|
solutions:
|
||||||
solve.php:
|
solve.php:
|
||||||
desc: Basic solution
|
desc: Basic solution
|
||||||
language: php
|
language: php
|
||||||
|
11
ProjectEuler/035/desc.yml
Normal file
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:
|
||||||
|
|
||||||
|
|
40
ProjectEuler/035/solve.php
Normal file
40
ProjectEuler/035/solve.php
Normal file
@ -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
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
|
||||||
|
|
12
ProjectEuler/036/solve.php
Normal file
12
ProjectEuler/036/solve.php
Normal 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
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
|
Reference in New Issue
Block a user