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

How to Add Social Share Buttons

In this article, I will tell you how to add social media sharing buttons. The theme you are going to add will not matter whether it’s html or WordPress. All of these codes are from the developers’ pages of the social media platforms. You can also look theirs site.

Facebook Share Button

In this code you need to change the value of the data-href = “” parameter. This should be a URL in the site. If you are using WordPress, you can type <?php the_permalink ()?>.

<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/tr_TR/sdk.js#xfbml=1&version=v2.4";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<div class="fb-share-button" data-href="http://www.yourwebsite.com" data-layout="button_count"></div>

Tweet Button

In this code you need to change the value of the data-href = “” parameter. This should be a URL in the site. If you are using WordPress, you can type <?php the_permalink ()?>. You also need to change the value of the data-text = “” parameter. This should be a page title. If you want to add a shared via, you can type in the value of the data-via = “” parameter (for example your twitter username).

<div style="display:inline-block;vertical-align:top">
<a href="https://twitter.com/share" class="twitter-share-button" data-url="http://www.sitenizinurlsi.com" data-text="Sitenizin Başlığı"> Tweet</a>
<script>!function (d, s, id) { var js, fjs = d.getElementsByTagName(s)[0], p = /^http:/.test(d.location) ? 'http' : 'https'; if (!d.getElementById(id)) { js = d.createElement(s); js.id = id; js.src = p + '://platform.twitter.com/widgets.js'; fjs.parentNode.insertBefore(js, fjs); } }(document, 'script', 'twitter-wjs');
</script>
</div>

Pinterest Pin Button

You do not need to make any changes to it. You should not only forget that the JavaScript has to be added only once on the site.

<div style="display:inline-block;vertical-align:top">
<a href="//www.pinterest.com/pin/create/button/" data-pin-do="buttonBookmark" data-pin-color="red"><img src="//assets.pinterest.com/images/pidgets/pinit_fg_en_rect_red_20.png" /></a>
<script type="text/javascript" async defer src="//assets.pinterest.com/js/pinit.js"></script>
</div>

Linkedin Share Button

You do not have to make any changes here too.

<div style="display:inline-block;vertical-align:top">
<script src="//platform.linkedin.com/in.js" type="text/javascript"> lang: en_US</script>
<script type="IN/Share" data-counter="right"></script>
</div>

Google +1 Button

In this code you need to change the value of the data-href = “” parameter. This should be a URL in the site. If you are using WordPress, you can type <?php the_permalink ()?>.

<div style="display:inline-block;vertical-align:top">
<script src="https://apis.google.com/js/platform.js" async defer></script>
<a class="g-plusone" data-size="medium" data-href="http://www.sitenizinurlsi.com"></a>
</div>

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