Added a few generic problems\nSolved Euler027

This commit is contained in:
FuryFire
2016-02-23 15:55:26 +01:00
parent a6355b2629
commit 2a2623a652
19 changed files with 352 additions and 0 deletions

23
ProjectEuler/026/desc.yml Normal file
View File

@ -0,0 +1,23 @@
title: Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.
url: http://projecteuler.net/problem=26
desc: |
A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given:
1/2 = 0.5
1/3 = 0.(3)
1/4 = 0.25
1/5 = 0.2
1/6 = 0.1(6)
1/7 = 0.(142857)
1/8 = 0.125
1/9 = 0.(1)
1/10 = 0.1
Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. It can be seen that 1/7 has a 6-digit recurring cycle.
Find the value of d < 1000 for which 1/d contains the longest recurring cycle in its decimal fraction part.
solution: Bruteforce
solutions:
solve.php:
desc: Basic Solution - needs BCMath
language: php

View File

@ -0,0 +1,20 @@
<?php
define('MAX',1000);
$max_len = 0;
for($div = MAX; $div > 2; $div--) {
$result = bcdiv('1',(string)$div,2000);
for($len = 2; $len < 1000; $len++) {
if(substr($result,10,$len) === substr($result,10+$len,$len)) {
if($len > $max_len) {
echo "Div: ".$div. " Len: ".$len."\n";
$max_len = $len;
$max_div = $div;
}
break;
}
}
}
echo $max_div;

13
ProjectEuler/026/solve.rb Normal file
View File

@ -0,0 +1,13 @@
DIGITS = 1000
max = 10 ** (DIGITS-1)
fcurrent = 1
fprev = 1
term = 2
while fcurrent < max do
term = term + 1
fnext = fcurrent + fprev
fprev = fcurrent;
fcurrent = fnext;
end
puts term

20
ProjectEuler/027/desc.yml Normal file
View File

@ -0,0 +1,20 @@
title: Quadratic primes
url: http://projecteuler.net/problem=27
desc: |
Euler discovered the remarkable quadratic formula:
n<> + n + 41
It turns out that the formula will produce 40 primes for the consecutive values n = 0 to 39. However, when n = 40, 402 + 40 + 41 = 40(40 + 1) + 41 is divisible by 41, and certainly when n = 41, 41<34> + 41 + 41 is clearly divisible by 41.
The incredible formula n<> - 79n + 1601 was discovered, which produces 80 primes for the consecutive values n = 0 to 79. The product of the coefficients, -79 and 1601, is -126479.
Considering quadratics of the form:
n<> + an + b, where |a| < 1000 and |b| < 1000
where |n| is the modulus/absolute value of n
e.g. |11| = 11 and |-4| = 4
Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n = 0
solution: Bruteforce
solutions:
solve.php:
desc: Basic Solution - needs BCMath
language: php

View File

@ -0,0 +1,32 @@
<?php
function is_prime($prime) {
if($prime == 1)
return true;
if($prime == 2)
return true;
$sqrt = sqrt($prime);
for ($i = 3; $i <= $sqrt; $i+=2){
if ($prime%$i == 0) return false;
}
return true;
}
$high = 0;
$max_primes = 0;
for($a=-999; $a<1000; $a++) {
for($b=-999; $b<1000; $b=$b+2) {
$n=0;
while(true) {
echo $n*$n + $a * $n + $b . "\n";
if(!is_prime($n*$n + $a * $n + $b)) {
if($n > $max_primes) {
$max_primes = $n;
echo "max: $n a: $a b: $b\n";
}
break;
}
$n++;
}
}
}

13
ProjectEuler/027/solve.rb Normal file
View File

@ -0,0 +1,13 @@
DIGITS = 1000
max = 10 ** (DIGITS-1)
fcurrent = 1
fprev = 1
term = 2
while fcurrent < max do
term = term + 1
fnext = fcurrent + fprev
fprev = fcurrent;
fcurrent = fnext;
end
puts term