Tuesday, January 25, 2011

New Series - Factorial and Fibonacci

I'm going to start a new series of posts called "Factorial and Fibonacci in [Language]". As with my previous 2 series, "OO Hello World" and "Language's Basics by Example", my aim is to show programming language's basic features, and as such, I will be showing stuff such as: static members, instance member, recursion, more loops and conditional constructs, and whatever else comes up. The idea is to write a simple OO program that implements the Factorial and Fibonacci algorithms in a Functional (Recursion) and Imperative (Statements-Loops) way using static methods and fields plus different loop statements such as Do, While, For, For each, etc. I will also add the Timer classes provided by the language's framework to count how much time those algorithms take (benchmark?) and do some comparison of the timing by language.

From now on, I will not be including 2 versions of the same program. First, to save some space and second, because counting keywords is not the main topic anymore. So, a mix of minimal and verbose syntax in each of the languages will be used instead.

The languages:
.NET/CLR: C#, VB.NET, C++/CLI, F#, Boo, Phalanger, IronPython, IronRuby, Delphi Prism, Zonnon, Nemerle, Cobra, JScript.NET
Java/JVM: Groovy, Java, Jython, JRuby, Fantom, Scala, JavaFX, Gosu

The Program's Structure will be (more or less) as follows:
// Factorial and Fibonacci in Language
    // Static Class
        // Static Fields
        // Static Constructor
        // Static Method 1
            // Recursive Fibonacci
        // Static Method 2
            // Imperative Fibonacci
        // Static Method 3
            // Recursive Factorial
        // Static Method 4
            // Imperative Factorial
        // Static Method 5
            // For Loop Range
                // Start Timer
                // Call Static Method: Recursive Fibonacci
                // Stop Timer
                // Print Result
            // While Loop Range
                // Start Timer
                // Call Static Method: Imperative Fibonacci
                // Stop Timer
                // Print Result
            // Do Loop Range
                // Start Timer
                // Call Static Method: Recursive Factorial
                // Stop Timer
                // Print Result
            // For Each Loop Range
                // Start Timer
                // Call Static Method: Imperative Factorial
                // Stop Timer
                // Print Result

    // Instance Class
        // Instance Fields
        // Instance Constructor
        // Instance Method 1
            // Call Static Recursive Fibonacci
        // Instance Method 2
            // Call Static Imperative Fibonacci
        // Instance Method 3
            // Call Static Recursive Factorial
        // Instance Method 4
            // Call Static Imperative Factorial

    // Console Program/Script
        // Calling Static Class and Methods
        // Calling Instance Class and Methods
        // Create a List of values to test
        // Benchmarking Fibonacci
        // Call Factorial Imperative
        // Call Factorial Recursive
        // Benchmarking Factorial
        // Call Fibonacci Imperative
        // Call Fibonacci Recursive
        // Stop and exit

And here below some definitions of the new concepts:

"In mathematics, the factorial of a positive integer n,[1] denoted by n!, is the product of all positive integers less than or equal to n. For example:

5! = 5 x 4 x 3 x 2 x 1 = 120

0! is a special case that is explicitly defined to be 1."
Taken from: http://en.wikipedia.org/wiki/Factorial#Definition

"In mathematics, the Fibonacci numbers are the numbers in the following integer sequence:

0,1,1,2,3,5,8,13,21,34,55,89,144, ...

By definition, the first two Fibonacci numbers are 0 and 1, and each subsequent number is the sum of the previous two. Some sources omit the initial 0, instead beginning the sequence with two 1s." Taken from: http://en.wikipedia.org/wiki/Fibonacci_series

"Recursion is the process of repeating items in a self-similar way. For instance, when the surfaces of two mirrors are exactly parallel with each other the nested images that occur are a form of infinite recursion. The term has a variety of meanings specific to a variety of disciplines ranging from linguistics to logic. The most common application of recursion is in mathematics and computer science, in which it refers to a method of defining functions in which the function being defined is applied within its own definition; specifically it is defining an infinite statement using finite components." Taken from: http://en.wikipedia.org/wiki/Recursion

"In computer science, imperative programming is a programming paradigm that describes computation in terms of statements that change a program state. In much the same way that imperative mood in natural languages expresses commands to take action, imperative programs define sequences of commands for the computer to perform." Taken from: http://en.wikipedia.org/wiki/Imperative_programming

Static Class
"A class can be declared static, indicating that it contains only static members. It is not possible to create instances of a static class using the new keyword.

Use a static class to contain methods that are not associated with a particular object. For example, it is a common requirement to create a set of methods that do not act on instance data and are not associated to a specific object in your code. You could use a static class to hold those methods.

  • The main features of a static class are:
  • They only contain static members.
  • They cannot be instantiated.
  • They are sealed.
  • They cannot contain Instance Constructors (C# Programming Guide).

Creating a static class is therefore much the same as creating a class that contains only static members and a private constructor. A private constructor prevents the class from being instantiated.

The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created.

Static classes are sealed and therefore cannot be inherited. Static classes cannot contain a constructor, although it is still possible to declare a static constructor to assign initial values or set up some static state." Taken from: http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.80).aspx

Static Variable
"In computer programming, a static variable is a variable that has been allocated statically — whose lifetime extends across the entire run of the program. This is in contrast to the more ephemeral automatic variables (local variables), whose storage is allocated and deallocated on the call stack; and in contrast to objects whose storage is dynamically allocated." Taken from: http://en.wikipedia.org/wiki/Static_variable#Static_Variables_as_Class_Variables

Static Method
"In object-oriented programming, a method is a subroutine that is exclusively associated either with a class (in which case it is called a class method or a static method) or with an object (in which case it is an instance method).

As mentioned above, a method may be declared as static, meaning that it acts at the class level rather than at the instance level. Therefore, a static method cannot refer to a specific instance of the class (i.e. it cannot refer to this, self, Me, etc.), unless such references are made through a parameter referencing an instance of the class, although in such cases they must be accessed through the parameter's identifier instead of this. Most importantly there is no need to make an object for accessing data .i.e. without creating an object we can access the data members of a static class." Taken from: http://en.wikipedia.org/wiki/Static_method#Static_methods

Static Constructor
"A static constructor is a static data initializer. Static constructors allow complex static variable initialization.[1] Static constructors can be called once and call is made implicitly by the run-time right before the first time the class is accessed. Any call to a class (static or constructor call), triggers the static constructor execution. Static constructors are thread safe and are a great way to implement a singleton pattern. When used in a generic programming class, static constructors are called on every new generic instantiation one per type (static variables are instantiated as well)." Taken from:http://en.wikipedia.org/wiki/Constructor_(object-oriented_programming)#C.23_static_constructor

"In computing, a benchmark is the act of running a computer program, a set of programs, or other operations, in order to assess the relative performance of an object, normally by running a number of standard tests and trials against it. The term 'benchmark' is also mostly utilized for the purposes of elaborately-designed benchmarking programs themselves." Taken from: http://en.wikipedia.org/wiki/Benchmark_(computing)


No comments:

Post a Comment