Math Is Fun Forum

  Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

#1 2009-12-13 07:46:45

John E. Franklin
Member
Registered: 2005-08-29
Posts: 3,588

C# boolean algebra, C# file i/o

Hi, I thought I'd share
this little program I wrote for those boolean
algebra enthusiasts or C# users.
It runs under Microsoft Visual C# 10 Beta Express.
It just downloaded the free development tools
2 days ago, the Express version is free!

Here's the code:

//project: Boolean 4 variable solver, called array5 in my C# projects, since it came from an array sampler,
// and I didn't dare change the name too much yet, just learning C#...
//This solves the 2 to the power of 16  4-variable boolean cases creating small equations.
//using operators that combine two items into one.
//The trivial cases have complexity of 0, the easy ones you start with, the variables and all true and all false.
//variables a, b, c, d, and their negatives, and 0 and 1.
//a list is created where each decimal value is two other easier ones combined together with boolean logic.
//folling down the tree, you can create an equation when you get to the trivial cases, which are variables.
//When the final list is written, the eight trivial cases are shown as 888777 still, just didn't change them.

using System;
using System.IO; //for file i/o


class DeclareArraysSample
{
    public static void Main()
    {

        Console.WriteLine("StartingProgram" + " yes");
        int[] jMom = new int[65538];
        int[] jDad = new int[65538];
        int[] jComplexity = new int[65538];
        int[] jConnector= new int[65538];


        int jiter = 0;
        while (jiter < 65537)
        {
            jComplexity[jiter] = 888777;
            jMom[jiter]=888777;
            jDad[jiter]=888777;
            jConnector[jiter]=888777;
            jiter++;
        }
        //hex values to follow...  They are the trivial cases from a truth table with a,b,c,d columns and output.
        //when the output below is the same or the invert as one of the 4 columns, then it is trivial.
        jComplexity[0xFFFF]=0; //jComplexity[0b1111111111111111]=0;
        jComplexity[0x0000]=0;//jComplexity[0b0000000000000000]=0;
        jComplexity[0xFF00]=0;//jComplexity[0b1111111100000000]=0;
        jComplexity[0x00FF]=0;//jComplexity[0b0000000011111111]=0;
        jComplexity[0x0F0F]=0;//jComplexity[0b0000111100001111]=0;
        jComplexity[0xF0F0]=0;//jComplexity[0b1111000011110000]=0;
        jComplexity[0x3333]=0;//jComplexity[0b0011001100110011]=0;
        jComplexity[0xCCCC]=0;//jComplexity[0b1100110011001100]=0;
        jComplexity[0x5555]=0;//jComplexity[0b0101010101010101]=0;
        jComplexity[0xAAAA]=0;//jComplexity[0b1010101010101010]=0;
        long jValueInc = 0;
        int jChangeMade = 1;
        while (jChangeMade == 1)
        {
        jChangeMade = 0;
        Console.WriteLine("Times");
        int jiter3 = 0;
        while (jiter3 <= 0xFFFF)
        {
            int jiter2 = 0;
            while (jiter2 <= 0xFFFF)
            {
                jValueInc += 1;
                if ((jComplexity[jiter2] < 2222 ) && (jComplexity[jiter3] < 2222 ))
                {
                    int jAnswer;
                    int jComplexNow;
                    
                    /////////////////////////xor
                    jAnswer = jiter2 ^ jiter3; // ^ is xor
                    jComplexNow = jComplexity[jiter2] + jComplexity[jiter3] + 1;
                    if (jComplexNow < jComplexity[jAnswer])
                    {
                        jConnector[jAnswer] = 55; //xor
                        jComplexity[jAnswer] = jComplexNow;
                        jMom[jAnswer] = jiter2;
                        jDad[jAnswer] = jiter3;
                        jChangeMade = 1;
                        //Console.WriteLine(jAnswer);
                    }
                    ////////////////////or
                    jAnswer = jiter2 | jiter3; // | is or
                    jComplexNow = jComplexity[jiter2] + jComplexity[jiter3] + 1;
                    if (jComplexNow < jComplexity[jAnswer])
                    {
                        jConnector[jAnswer] = 77; //or
                        jComplexity[jAnswer] = jComplexNow;
                        jMom[jAnswer] = jiter2;
                        jDad[jAnswer] = jiter3;
                        jChangeMade = 1;
                        //Console.WriteLine(jAnswer);
                    }
                    ///////////////////and
                    jAnswer = jiter2 & jiter3; // & is and
                    jComplexNow = jComplexity[jiter2] + jComplexity[jiter3] + 1;
                    if (jComplexNow < jComplexity[jAnswer])
                    {
                        jConnector[jAnswer] = 22; //and
                        jComplexity[jAnswer] = jComplexNow;
                        jMom[jAnswer] = jiter2;
                        jDad[jAnswer] = jiter3;
                        jChangeMade = 1;
                        //Console.WriteLine(jAnswer);
                    }

                }
                jiter2++;
            }
            jiter3++;
        } 
        }
        Console.WriteLine(jValueInc); //debugging stuff
        Console.WriteLine("John E. Franklin");  //debugging stuff
        //this is a place on my hard drive.
        string path = @"c:\area34\bigfile.txt";  //the file with 65536 lines in it, big file!!

        if (!File.Exists(path))
        {
            //create file to write to.
            using (StreamWriter sw = File.CreateText(path))
            {
                sw.WriteLine("Hello");
                sw.WriteLine("And");
                sw.WriteLine("Welcome");
            }
        }


        int jPrintm = 0;
        while (jPrintm <= 0xFFFF)
        {
            string jGate = "";

            if (jConnector[jPrintm] == 22)
            {
                jGate = " and ";
            }
            if (jConnector[jPrintm] == 77)
            {
                jGate = " or ";
            }
            if (jConnector[jPrintm] == 55)
            {
                jGate = " xor ";
            }
            //Console.WriteLine(jPrintm + " = " +  jMom[jPrintm] + jGate + jDad[jPrintm]);

            ///this  text is always added, making the file longer.
            using (StreamWriter sw = File.AppendText(path))
            {
                sw.WriteLine(jPrintm + " = " + jMom[jPrintm] + jGate + jDad[jPrintm]);
                //sw.WriteLine("65535 = trivial"); //for example...
            }
            
            jPrintm += 1;
        }
        Console.WriteLine("End Program--Press Enter to Exit");
        Console.ReadLine();

    }
}

igloo myrtilles fourmis

Offline

#2 2009-12-13 08:12:12

John E. Franklin
Member
Registered: 2005-08-29
Posts: 3,588

Re: C# boolean algebra, C# file i/o

This program takes about 12 minutes to run on a Pentium, about every 4 minutes, the word "Times" appears on the DOS window.  Even though I print to the DOS command window, I still have access to many MegaBytes of RAM if needed, I tried in another program and got 77 million 4-byte integers filled with numbers!!  The "Times" word appears three times, and then the file writes out in about 2 seconds, and is about 1.3 Megabytes long.  The carriage return line feeds are already provided for you with the WriteLine command.


igloo myrtilles fourmis

Offline

#3 2009-12-15 13:09:41

John E. Franklin
Member
Registered: 2005-08-29
Posts: 3,588

Re: C# boolean algebra, C# file i/o

In case you want to see the 65536 lines of output, I have them in a webpage now.
Some lines say, "trivial case", not 888777888, I replaced them to look better.
Here's the linky:
http://johnericfranklin.250free.com/boolean.html
If more than 150 people view it in a month, it will stop working though until the next month, since it is free hosting and the file is 1.5 megabytes.


igloo myrtilles fourmis

Offline

Board footer

Powered by FluxBB