content top

Project Euler – Problem 8

Problem description:

http://projecteuler.net/index.php?section=problems&id=8

Find the greatest product of five consecutive digits in the 1000-digit number.

731671765313…0752963450 (Check problem page for full length number)


My solution:


The logic behind:

Simply put: ..

Solution (PHP):
<?php
$string = '7316717...2963450';

for ($i = 0; $i < 1000; $i++)
{
$nyverdi = $string[$i]*$string[$i+1]*$string[$i+2]*$string[$i+3]*$string[$i+4];

if ($nyverdi > $verdi) {$verdi = $nyverdi;}
}
echo $verdi;
?>
Answer
40824

An irrational decimal fraction is created by concatenating the positive integers:

0.123456789101112131415161718192021…

It can be seen that the 12^(th) digit of the fractional part is 1.

If d_(n) represents the n^(th) digit of the fractional part, find the value of the following expression.

d_(1) × d_(10) × d_(100) × d_(1000) × d_(10000) × d_(100000) × d_(1000000

Project Euler – Problem 9

Problem description:

http://projecteuler.net/index.php?section=problems&id=9

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,

a2 + b2 = c2

For example, 32 + 42 = 9 + 16 = 25 = 52.

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.


My solution:


The logic behind:

My initial approach was to simplify the equation by using the given information and some algebra, resulting in the following equation:

b^2 = c2 – a2
b = 1000 – a – c

(1000 – a – c)2 = c2 – a2
10002 – 1000a – 1000c – 1000a – a2 – ac – 1000c – ac – c2 = c2 – a2
2000a + 2000c + 2ac = 10002
1000a + 1000c + ac = 500000

Eventually i reconsidered my approach.
As i later was to discover, this pitch wasn’t a bad idea after all, but i eventually went for a good ol’ brute force.
The equation could have been used to shorten the time required to solve the problem by brute force a lot, but my solution spits out the correct answer within 5 secs so what the heck.

Solution (PHP):
 <?php

for ($a = 1; $a < 800; $a++)
{

for ($b = $a; $b < 1000; $b++)
{
$c = 1000-$a-$b;

if (bcpow($a,2)+bcpow($b,2))==bcpow($c,2) //GREAT SUCCESS!
{
echo $a*$b*$c;
}
}
}
?>
Answer
31875000

Project Euler – Problem 40

Problem description:

http://projecteuler.net/index.php?section=problems&id=40

An irrational decimal fraction is created by concatenating the positive integers:

0.123456789101112131415161718192021…

It can be seen that the 12^(th) digit of the fractional part is 1.

If d_(n) represents the n^(th) digit of the fractional part, find the value of the following expression.

d_(1) × d_(10) × d_(100) × d_(1000) × d_(10000) × d_(100000) × d_(1000000


My solution:


The logic behind:

Simply put: Keep increasing n, and add n to the result until the length of the result string reaches a given number.

Solution (PHP):
<?php
function d($ønsketplass)
{

while ($loop < $ønsketplass)
{
$loop = $loop+strlen($i);
$streng .= $i;
$i++;
}

return (substr($streng,($ønsketplass-1),1));
}
echo d(1)*d(10)*d(100)*d(1000)*d(10000)*d(100000)*d(1000000);
?>
Answer
210

An irrational decimal fraction is created by concatenating the positive integers:

0.123456789101112131415161718192021…

It can be seen that the 12^(th) digit of the fractional part is 1.

If d_(n) represents the n^(th) digit of the fractional part, find the value of the following expression.

d_(1) × d_(10) × d_(100) × d_(1000) × d_(10000) × d_(100000) × d_(1000000

Project Euler – Problem 48

Problem description:

http://projecteuler.net/index.php?section=problems&id=48

The series, 11 + 22 + 33 + … + 1010 = 10405071317.

Find the last ten digits of the series, 11 + 22 + 33 + … + 10001000.


My solution:


The logic behind:

Simply put: Iterate over all numbers between 1 and 1000. For each iteration n, add nn.
Code uses bcmath functions, as the PHP pow function can’t handle numbers of this scale.

Solution (PHP):
<?php

for ($x = 1; $x < 1000; $x++)
{$sum = bcadd($sum, bcpow($x,$x));}
echo substr($sum,strlen($sum)-10,10);
?>
Answer
9110846700

Project Euler – Problem 1

Problem description:

http://projecteuler.net/index.php?section=problems&id=1

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.



My solution:


The logic behind:

Simply put: Iterate over all numbers between 0 and 1000, and check if the given number is a multiple of  3 or 5. If the number does satisfy this requirement, add it to the stack.

Solution (PHP):
<?php

Echo "To be continued:)";
?>
Answer
233168
content top