Archive

Archive for January, 2009

Flickr

January 23rd, 2009

This is a test post from flickr, a fancy photo sharing thing.

General

Use recursion to achieve Fibonacci numbers

January 21st, 2009

查看本文的中文版,请点击这里

Last article illustrated how to use the recursion to achieve factorial calculation by a simple example, and this article will show another classic application of recursion – Fibonacci numbers.

Fibonacci numbers refers to such an infinite numbers: 1, 1, 2, 3, 5, 8, etc. Except the first two numbers, each number is the sum of the two preceding numbers.

Since the Fibonacci numbers is defined by recurrence relation, then we can easily use recursion to achieve its calculation.

As the calculation of factorial, the bigger of the numerical calculation, the more times of recursion, and the number of redundant recursion will growth in a redundant geometric way, the efficiency of program will also be lower and lower (you can modify the program, personally test it). So you need according to the actual situation, and make a decision between readability and efficiency.

Example: Print 10 Fibonacci numbers. Use recursion to achieve it.

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Fibonacci
{
    class Program
    {
        public long Fibonacci(int n)
        {
            if (n > 0 && (n == 1 || n == 2))
            {
                return 1;
            }
            else
            {
                return Fibonacci(n - 1) + Fibonacci(n - 2);
            }
        }
 
        static void Main(string[] args)
        {
            Program p = new Program();
            for (int i = 1; i < 11; i++)
            {
                Console.Write(p.Fibonacci(i));
            }
        }
    }
}

Algorithm, C# , ,

Use recursion to achieve factorial calculation

January 20th, 2009

查看本文的中文版,请点击这里

According to the characteristics of factorial calculation, using recursion to achieve it is very convenient.

Note that recursion is a kind of low efficiency algorithm, especially when calculate with larger operations, so give careful consideration to the circumstances in deciding whether to use it.

This is a example of algorithm I wrote when learning C# programming language.

Example: Calculate 1! + 2! + … + 10! and print out the results. Use recursion to achieve it.

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Factorial
{
    class Program
    {
        public long Factorial(int n)
        {
            if (n == 0 || n == 1)
            {
                return 1;
            }
            else
            {
                return n * Factorial(n - 1);
            }
        }
 
        static void Main(string[] args)
        {
            Program p = new Program();
            long sum = 0;
            for (int i = 1; i < 11; i++)
            {
                sum += p.Factorial(i);
            }
            Console.WriteLine("1! + 2! + ... + 10! = " + sum);
        }
    }
}

Algorithm, C# , ,

Red Alert 3 on mac

January 17th, 2009

This an official port made by me. Everything works fine expect the auto-update. When the update window appears and asks you to update, just click “no”

Picture 1.png

Picture 2.png

Picture 3.png

Picture 4.png

Picture 5.png

Picture 6.png

If you have any problems, please leave a comment.

Download links:

http://rapidshare.com/files/184102994/RA3Cider.app.part04.sitx

http://rapidshare.com/files/184105190/RA3Cider.app.part01.sitx

http://rapidshare.com/files/184105302/RA3Cider.app.part03.sitx

http://rapidshare.com/files/184106918/RA3Cider.app.part05.sitx

http://rapidshare.com/files/184107476/RA3Cider.app.part02.sitx

http://rapidshare.com/files/184181702/RA3Cider.app.part06.sitx

http://rapidshare.com/files/184183191/RA3Cider.app.part09.sitx

http://rapidshare.com/files/184184110/RA3Cider.app.part08.sitx

http://rapidshare.com/files/184184654/RA3Cider.app.part07.sitx

http://rapidshare.com/files/184188633/RA3Cider.app.part10.sitx

http://rapidshare.com/files/184299299/RA3Cider.app.part19.sitx

http://rapidshare.com/files/184397938/RA3Cider.app.part16.sitx

http://rapidshare.com/files/184401204/RA3Cider.app.part18.sitx

http://rapidshare.com/files/184402077/RA3Cider.app.part17.sitx

http://rapidshare.com/files/184441411/RA3Cider.app.part11.sitx

http://rapidshare.com/files/184447574/RA3Cider.app.part15.sitx

http://rapidshare.com/files/184447915/RA3Cider.app.part12.sitx

http://rapidshare.com/files/184448028/RA3Cider.app.part13.sitx

http://rapidshare.com/files/184448352/RA3Cider.app.part14.sitx

Games

High Performance with PHP

January 15th, 2009

Preface

This article isn’t oriented a fresh man. so if you want to ask something about how to, please search before your ask. :)

  1. Compile your php code into opcode
    Zend Optimizer, eAccelerator, APC, XCache
  2. Using shared memory to boost your application
    You may use those extensions that i mentioned above, but i think memcache is more better. If you’re going to build a large application, memcache is necessary.
  3. Using apc / hidef instead of define statement
    Many people may ignore this… yes, this is a small optimization. I just listed it here if you have many constant defined.
  4. Using xdebug to profiling your application
    In many case, the application’s choke point is not in database but application’s logic. So you could turn on xdebug’s profilling, then use CacheGrind to view the output to see where the problem is.

PHP, Web Development , , ,

A fancy download tool for websites like rapidshare, megaupload etc

January 11th, 2009

Have you ever been bothered in fetching big files from some fire-sharing sites like rapidshare or megaupload?

Here a great tool to save your time. With Jdownloader, you just need to input all download links at one time. It will arrange download schedule automatically, even fill in those random character for you

Download it at here

What’s more, it’s cross-platform, that is, u can use it on windows mac or linux.

Picture 1.png

Linux, Software, Windows , ,