Posts

Project Euler – Problem 6 Solution

Problem: The sum of the squares of the first ten natural numbers is,

1^2 + 2^2 + … + 10^2 = 385

The square of the sum of the first ten natural numbers is,

(1 + 2 + … + 10)^2 = 55^2 = 3025

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 – 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

*https://projecteuler.net/problem=6

PHP;

$a = 385;
$b = 55;

for ($i = 11; $i <= 100; $i++) {
    $a += $i**2;
    $b += $i;
}

echo ($b**2) - $a;

Javascript;

'use strict';

let a = 325;
let b =  55;

for (let i = 11; i <= 100; i++) {
    a += i**2;
    b += i;
}

console.log((b**2) - a);

Project Euler – Problem 5 Solution

Problem: 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
* https://projecteuler.net/problem=5

Php;

<?php

$number = 20;
$isFound = false;
$checkList = [11, 13, 14, 16, 17, 18, 19, 20];

while (!$isFound) {
    $divided = true;
    foreach ($checkList as $check) {
        if ($number % $check != 0) {
            $divided = false;
            break;
        }
    }
    if ($divided) {
        $isFound = true;
        echo "Number is $number";
        break;
    } else {
        $number+=20;
    }
}

Javascript;

'use strict';

var number = 20;
var isFound = false;
var checkList = [11, 13, 14, 16, 17, 18, 19, 20];

while (!isFound) {
    var divided = true;
    for (let check of checkList) {
        if (number % check != 0) {
            divided = false;
            break;
        }
    }
    if (divided) {
        isFound = true;
        console.log("Number is " + number);
        break;
    } else {
        number+=20;
    }
}

Project Euler – Problem 4 Solution

Problem: A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two 3-digit numbers.
* https://projecteuler.net/problem=4

Php;

<?php

/**
 * Checks number if palindrome or not!
 *
 * @param $number
 * @return bool
 */
function check_if_palindrome($number)
{
    $is_palindrome = true;
    $array  = array_map('intval', str_split($number));
    $array2  = array_map('intval', str_split($number));
    $array2_count = count($array2) - 1;
    foreach ($array as $index => $item) {
        if ($item !== $array2[$array2_count - $index]) {
            $is_palindrome = false;
        }
    }
    return $is_palindrome;
}

$value = 0;
for ($i = 100; $i < 1000; $i++) {
    for ($k = 100; $k < 1000; $k++) {
        $result = $k * $i;
        if (check_if_palindrome($result) && $result > $value) {
            $value = $result;
        }
    }
}
echo $value;

Javascript;

'use strict';

/**
 * Checks number if palindrome or not!
 * @param number
 */
function check_if_palindrome(number) {
    var is_palindrome = true,
        array = (""+number).split(""),
        array2 = (""+number).split(""),
        array2_count = array2.length - 1;

    for (var i = 0; i < array2_count; i++) {
        if (array[i] !== array2[array2_count - i]) {
            is_palindrome = false;
        }
    }

    return is_palindrome;
}

var value = 0;
for (var i = 100; i < 1000; i++) {
    for (var k = 100; k < 1000; k++) {
        var result = k * i;
        if (check_if_palindrome(result) && result > value) {
            value = result;
        }
    }
}

console.log(value);

Project Euler – Problem 3 Solution

Problem: The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ?
* https://projecteuler.net/problem=3

Php;

<?php

class ProblemSolver
{
    private $original_number;
    private $number;
    private $current_prime_number = 2;
    private $prime_numbers = [];
    private $largest_prime_number;

    /**
     * ProblemSolver constructor.
     * @param int $number
     */
    public function __construct(int $number)
    {
        $this->number = $this->original_number = $number;
    }

    /**
     * Finds the next prime number
     */
    private function find_next_prime_number()
    {
        if ($this->current_prime_number == 2) {
            $this->current_prime_number++;
        } else {
            $this->current_prime_number += 2;
        }
        $can_divide = false;
        $number = 2;
        while ($number < $this->current_prime_number) {
            if ($this->current_prime_number % $number == 0) {
                $can_divide = true;
            }
            $number++;
        }
        if ($can_divide) {
            $this->find_next_prime_number();
        }
    }

    /**
     * Finds the prime factors and largest prime factor of given number
     */
    public function find_prime_factors()
    {
        while ($this->number > 0) {
            if ($this->number == 1) {
                $this->largest_prime_number = $this->current_prime_number;
                break;
            } else {
                if ($this->number % $this->current_prime_number == 0) {
                    $this->prime_numbers[] = $this->current_prime_number;
                    $this->number /= $this->current_prime_number;
                } else {
                    $this->find_next_prime_number();
                }
            }
        }
        echo $this->largest_prime_number;
    }
}

$solver = new ProblemSolver(600851475143);
$solver->find_prime_factors();

Javascript;

'use strict';

var number = 600851475143, prime_number = 2;

function find_next_prime_number() {
    var can_divide = false;
    var n = 2;
    if (prime_number === 2) {
        prime_number++;
    } else {
        prime_number += 2;
    }
    while (n < prime_number) {
        if (prime_number % n === 0) {
            can_divide = true;
        }
        n++;
    }
    if (can_divide) {
        find_next_prime_number();
    }
}

while (number > 1) {
    if (number % prime_number === 0) {
        number /= prime_number;
    } else {
        find_next_prime_number();
    }
}
console.log(prime_number);

Project Euler – Problem 2 Solution

Problem: Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, … By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
* https://projecteuler.net/problem=2

Php;

<?php

// Parameters
$total = 2;
$array = [1, 2];

while (true) {
    $array_count = count($array);
    $sum = $array[$array_count - 1] + $array[$array_count - 2];
    if ($sum < 4000000) {
        $array[] = $sum;
        if ($sum % 2 == 0) {
            $total += $sum;
        }
    } else {
        break;
    }
}
echo $total;

Javascript;

'use strict';

// Parameters
var total = 2,
    array = [1, 2],
    sum,
    array_count;

while (true) {
    array_count = array.length;
    sum = array[array_count -1] + array[array_count - 2];
    if (sum < 4000000) {
        array.push(sum);
        if (sum % 2 === 0) {
            total += sum;
        }
    } else {
        break
    }
}

console.log(total);

Project Euler – Problem 1 Solution

Problem: 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.
* https://projecteuler.net/problem=1

Php;

<?php

// Parameters
$total = 0;

for ($i = 1; $i < 1000; $i++) {
    if ($i % 3 == 0 || $i % 5 == 0) {
        $total += $i;
    }
}
echo $total;

Javascript

'use strict';

// Parameters
var total = 0, i;

for (i = 1;i < 1000; i++) {
    if (i % 3 === 0 || i % 5 === 0) {
        total += i;
    }
}

console.log(total);