Hosting Your Own Git Repositories

I was using Subversion as software versioning and revision control system with my previous company. Hosting repositories with Subversion is not easy, and it is a centralized system where you can’t have your own local commits; and mergings within branches to trunk always a nightmare for me.

I am happy to get to know about Git. Git is a free, open source and well developed source control management system developed by Linus Torvalds, initially for Linux kernel development.

Hosting with Git is easy, and it is a distributed version control system where you can have your codes committed to local or remotes. Git copy all of the data containing branches, tags and stored locally in client’s system in order to switch between branches in seconds. With Git, you can work offline, by committing your codes locally and push to the remotes whenever you have internet connections.

Requirements

  • Linux or Unix based system
  • Git to be installed on both server and client
  • SSH access to the server

Git on Server

SSH to the server, create the repository and create a bare Git repository.

$ mkdir test.git
$ cd test.git
$ git --bare init

That is all for the server side.

Git on Client

Open up Terminal, create a working directory, create an empty Git repository.

$ mkdir test
$ cd test
$ git init

We have created an empty Git repository. We will now add a file and commit the changes.

$ touch README
$ git add README
$ git commit -a -m 'first commit'

We have committed the changes. We will now setup the remote server and push to the server.

$ git remote add origin git@server.com:test.git
$ git push origin master

That is all for the first commit. Each and every time you want to push the changes to remotes, you can just do git push

Conclusion

Code versioning makes our life easy! Try to start everything with git init today!

Project Euler – Problem 23

Problem

A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.

A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.

As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.

Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.

Solution


					

Project Euler – Problem 22

Problem

Using names.txt (right click and ‘Save Link/Target As…’), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a name score.

For example, when the list is sorted into alphabetical order, COLIN, which is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN would obtain a score of 938 × 53 = 49714.

What is the total of all the name scores in the file?

Solution

$sum = 0;

for ($i = 1; $i < 10000; $i++) {
  if ($i == d(d($i)) && $i != d($i)) {
    $sum += $i;
  }
}

echo $sum;

function d($num) {
  $temp = 0;

  for ($i = 1; $i <= $num / 2; $i++) {
    if ($num % $i == 0) {
      $temp += $i;
    }
  }

  return $temp;
}

Project Euler – Problem 21

Problem

Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).
If d(a) = b and d(b) = a, where ab, then a and b are an amicable pair and each of a and b are called amicable numbers.

For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.

Evaluate the sum of all the amicable numbers under 10000.

Solution

$sum = 0;

for ($i = 1; $i < 10000; $i++) {
  if ($i == d(d($i)) && $i != d($i)) {
    $sum += $i;
  }
}

echo $sum;

function d($num) {
  $temp = 0;

  for ($i = 1; $i <= $num / 2; $i++) {
    if ($num % $i == 0) {
      $temp += $i;
    }
  }

  return $temp;
}

Project Euler – Problem 20

Problem

n! means n × (n − 1) × … × 3 × 2 × 1

For example, 10! = 10 × 9 × … × 3 × 2 × 1 = 3628800,
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.

Find the sum of the digits in the number 100!

Solution

echo problem20(100);

function problem20($num) {
  $sum = 0;
  $temp = 1;

  for ($i = 1; $i < $num; $i++) {
    $temp = bcmul($temp, $i);
  }

  $arrDigit = str_split($temp);

  foreach ($arrDigit as $digit) {
    $sum += $digit;
  }

  return $sum;
}