Monday, May 29, 2017

COBOL on Windows

For better or worse, I decided to learn COBOL. After a bit of research, I came across GNU Cobol, which had been Open COBOL in a future life. I wanted this to work in Windows and that's where the trouble began.

The easiest route was to run GNU Cobol in the Linux Layer for Windows, otherwise known as Bash for Windows 10. In Bash, it was simple enough to "sudo apt-get install open-cobol", which installs GNU Cobol 1.1, gcc, and everything else needed to build COBOL programs. Of course, this solution produced Linux binaries.

Getting GNU Cobol producing Win32/64 binaries was quite a bit more tedious. GNU doesn't distribute binaries, so it's up to one to build it yourself or find someone who has built it already. In the end I used the binary distribution from kiska.net for x64. However, GNU Cobol only generates C++ code and executes GCC to build the executable. The distribution from kiska didn't include the GCC, so I installed MinGW and the GCC from there.

Next, I had to configure the following environment variables for the COBOL compiler:
COB_CONFIG_DIR=c:\Program Files\OpenCOBOL\config
COB_COPY_DIR=c:\Program Files\OpenCOBOL\copy
COB_LIBRARY_PATH=C:\Program Files\OpenCOBOL\lib
COB_SCREEN_ESC=Y
COB_SCREEN_EXCEPTIONS=Y

The location of the MinGW bin directory and the GNU Cobol bin directory have to be added to the PATH.

Finally, the COBOL headers and libraries had to be added to MinGW where the GCC could find them. This included libcob.h, gmp.h, libcob, libpdcurses*, libgmp*, libdb*, libcob* from the lib and include directories.

With all that done, I am now able to produce 64-bit binaries for Windows.

Wednesday, July 29, 2015

(2^i)*(5^j)

I ran into an interesting problem the other day, someone asked for all positive integers of i & j, provide an ordered list of the solutions to (2^i)(5^j). After wracking my brains for quite a couple hours, I finally came up with the following solution. It work by finding integers for which there is an i and j in a sieve fashion. This works by solving for i, such that i = log2 (potential / (5^j)). This will allow the program to output as many values as the representation can support. I limit the number of values of j that I test for by limiting it to 5^j < potential. This gives the program O(n).

    class Program
    {
        static void Main()
        {
            int potential = 0;

            do
            {
                if (ExistsIandJ(potential))
                    Console.WriteLine("{0}", potential);
                    potential++;
            } while (potential <= int.MaxValue)

         }

        private static bool ExistsIandJ(int potential)
        {
            // potential = (2^i)*(5^j)
            // 1 = (2^i)*(5^j)/potential
            // 1/(2^1) = (5^j)/potential or (2^i) = potential / (5^j)
            // i = log2 (potential / (5^j))

            for (var j = 0; Math.Pow(5,j) <= potential; j++)
            {
                var i = Math.Log(potential / Math.Pow(5, j), 2);
                if (i == Math.Truncate(i))
                    return true;
            }
            return false;
        }       
    }


 I was expecting this to be fairly easy to begin with, the realized that wasn't going to be true. I then thought there must be a pattern to the values to i & j, but after a while realized there was not. Trees might be the answer, but only for a finite number of values, since you would have to determine when to prune the tree. Would you miss an answer. It was later that I realized that if I tested every answer for an i & j that it could be done infinitely. I pondered how to determine i & j, then decided to solve for i. The pieces fell into place.

Saturday, February 11, 2012

A few months ago, I started writing a game for the Android. It is a simple game inspired from an issue of 80 Micro. The game published in that magazine was for the TRS-80 Model I. Its objective was to pilot a space ship through a field of stars to land on a planet. The stars didn't move and the ship didn't move in real-time. It was a cool game none-the-less. My version of the game implements a simple version of gravity so that the ship is continuously falling toward the planet, and the stars are moving -- each in a different direction and rate.

To the right is an early snapshot of the game. The ship is at the top and the stars are sprinkled randomly across the screen. In the forthcoming posts I'm going to talk about the development of this game and how it is progressing.