Monday 9 July 2012

Project Euler, Problem 45: Triangle, pentagonal and hexagonal numbers

The problem itself:

Triangle, pentagonal, and hexagonal numbers are generated by the following formulae:

Triangle Tn=n(n+1)/2 1, 3, 6, 10, 15, ...
Pentagonal Pn=n(3n-1)/2 1, 5, 12, 22, 35, ...
Hexagonal Hn=n(2n-1) 1, 6, 15, 28, 45, ...
It can be verified that T285 = P165 = H143 = 40755.

Find the next triangle number that is also pentagonal and hexagonal.

Note: link to the problem @ ProjectEuler.net

How to solve the problem:

Brute-forcing the solution isn't fast, in fact it's incredibely slow, even with the some loop-optimizations. I speak from personal experience. :)
However, if we read about these numbers (triangle, pentagonal and hexagonal) we see some interesting properties that we can exploit.

We see that triangle numbers are super-set of the hexagonal numbers. Or put simply - every hexagonal is triangle number, even more, every odd triangle number is a hexagonal number. With this conslusion, we should only test if pentagonal number match given hexagonal number, because we already know that hexagonal number is also a triangle one.
We can easily see the connection between the indices of the triangle and hexagonal numbers - T(i) = H(i/2 + 1) or H(i) = T(2i - 1), where "i" is the index.

Because we need the next number that is triangle, pentagonal and hexagonal we could start from the index 144, because we don't care about previous solutions.

So now we want to start from the index 144, again brute-forcing the solution, but this time we have linear complexity - O(n).

The solution itself:

We have a function that calculates the hexagonal number for given index (nothing fancy here):
static public long CalculateHexagonalNumber(int index)
{
    long result = 2 * index * index - index;

    return result;
}


Now we need to test if a given hexagonal number is also pentagonal (well, more info @ Wikipedia how we can do that.
Here is the C# implmentation:

static public bool IsPentagonalNumber(int number)
{
 double pentagonalNumberTest = (Math.Sqrt((double)24 * number + 1) + 1) / 6;

 return pentagonalNumberTest == ((int)pentagonalNumberTest);
}


Here is the Main() function, which prints the next number that satisfy our condition and breaks:
static void Main(string[] args)
{
    int i = 144;

    while(true)
    {
        int hexagonalNumber = CalculateHexagonalNumber(i);

        if(IsPentagonalNumber(hexagonalNumber))
        {
            Console.WriteLine(hexagonalNumber);
            break;
        }

        i++;
    }
}


Conclusion:

This works pretty fast, doesn't use any additional storage, we don't need to precompute any values for pentagonal/hexagonal numbers.

Sunday 17 June 2012

Project Euler - Intro

Intro:

Project Euler is a website, which currently contains 389 problems. The authors claim that for every problem can be solved using programming language, and for some just using a sheet of paper and a pencil. To do the latter, you have to have a lot of knowledge in a lot of different math subjects - number theory, geometry, algebra etc..

More info:

You can read more about Project Euler on its website: http://projecteuler.net/
When you register your own account you can submit your answers and the system will show you if the answer is corrent or incorrent. The website's system also provides a tracking of your current progress, different levels (depends on how many problem you've solved), awards (solving problems a group of problem, etc).
You can filter the users by country, so if the user left some contact details you can chat with each other.
You can see the statistics for each individual problem (how many registered users solved it).
Something worth telling is until you solve a problem, you're not allowed to look at other people's solutions, only when you submit a correct solution you're able to do that. Another thing you have access to once you've submitted a correct solution is a access to the author's explanation (if provided, of course).

Outro

Project Euler provides a good competitive enviroment for training your programming, math and logic skills. Why wait?! Go to the website and start coding. :) Note: Maybe I will post some of my solutions in the future. And last, but not least - all greetings go to the authors of Project Euler and the people who provide new problems. ;)

Saturday 16 June 2012

AutoHotkey - great scripting tool!




About AutoHotkey (AHK)

You want to automate your actions? Sick of the ugly batch scripts?! Well, you have to try AutoHotkey, or better - some of its other flavors: AutoHotkey_L (AutoHotkey_L is a fork of the original AutoHotkey, more info some other post) or IronAHK (it's a .NET port of the original AutoHotkey, however introducing some new features).

AutoHotkey has proven itself to be very useful in everyday use, when using the keyboard is ten times faster and easier than using the mouse. Many people like such software, starting from the flame-war between VI vs. Emacs, lynx vs. links, Total Commander vs. Directory Opus, all of them which are great pieces of software and each one of them utilizing the keyboard to its full potential. However, no matter how great and useful a program can be, it will always have some flaws.. AutoHotkey to the rescue! :)

Key features:

Some of its features are:
- Easy handling of the keyboard keys and mouse clicks
- Not only keyboards and mice, you can have full support for USB hardware as well
- Easy to use GUI creator (something like Visual C#, Delphi 7, etc, but with less features)
- Built-in GUI creator which support GUI stealing :)
- Almost full-support of AutoIt2 script
- Great WinLIRC script, which can be a great start ;)
- Easy to learn syntax
- Useful help file (yes, you read that CORRECTLY), cool and plain examples
- Many, many more.. :)

Usage:

I have used AutoHotkey for quick hacks (a.k.a. hacking around a problem) and also for some more serious programs (not big projects, but useful full-features programs, hopefully bug-free). AutoHotkey supports DLL calls, which basically means that you can use almost every function provided by Win32 API, combining this with some great scripts: SKAN's List of Win32 CONSTANTS and Lexikos's StructParser, practically you are able to build a full-featured software in AutoHotkey in less time, with less efforts.

Compiling your scripts..

AutoHotkey also provides a compiler, so you can make your programs portable (you won't need an installed version of AutoHotkey to run a compiled script). The compiler can also the UPX packer, so the compiled EXEs' size is even smaller.
You can use a password for protecting your script from being decompiled in the future. However, this cannot be prevented if the user guesses the password (or knows it ;). Maybe you can try the Armadillo packer, it was not cracked (not a automated unpacker was written for it, however there were some tools which could greatly ease the process :).

Final words:

This is for now, in the future articles I would comment the main differences between AutoHotkey and AutoHotkey_L.