Installing Linux, Apache, MySQL and PHP (LAMP) On Ubuntu

Installing the Default LAMP Package on Ubuntu

If you want to install the LAMP in the easiest way, you need to write the following codes in the terminal.

sudo apt-get update
sudo apt-get install lamp-server^

Do not forget to type the character “^” otherwise it will fail.

Installing Linux, Apache, MySQL, PHP on Ubuntu

If you want to load all separately, you are in the right place. This is a little more painful, but it gives you a better use and the ability to load whatever you want.

Installing Apache

sudo apt-get update
sudo apt-get install apache2

That’s it. Apache installed. To check, enter http: // localhost in your web browser and it is loaded without any problems if you see “It Works!”. If you are installing on your vps server, you must type in your ip address of your VPS from the browser.

Installing MySQL

sudo apt-get install mysql-server

It will ask you to enter the MySQL user’s root password.

Installing PHP

sudo apt-get install php libapache2-mod-php php-mcrypt php-mysql

That’s all.

Installing PHP Modules

Type the following code in the terminal to list the php modules.

apt-cache search php5-

If you want to install one of them, you can install it into the terminal by typing the following code.

sudo apt-get modül ismi
sudo apt-get php5-cli

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);