2010: The year we make contact RSS 2.0
# Tuesday, February 17, 2009

Since I could not find a chart of some sort showing current rankings on StackOverflow.com, I decided to roll my own.  The link that follows points to a static cached version of the page that will only update manually for now.  Enjoy, and feel free to give your feedback or suggestions for improvements.

Current StackOverflow.com Rankings

Stackoverflow.com Rankings
Tuesday, February 17, 2009 1:16:09 AM (Eastern Standard Time, UTC-05:00)  #    Comments [1] -
Bookmark, Tweet, or Share
# Tuesday, February 10, 2009

F# has 'easy button' for finding large primes!  29 lines of code!  Thanks to Chris Smith and his 'completely unique view' on the Sieve of Eratosthenes

#light 

open System.Collections.Generic 
open System 

let findPrimes = 
    seq { 
        yield 2 //2 is a known prime

        let knownComposites = new HashSet<int>() 
        
        //Visit each odd
        for i in 3 .. 2 .. int 1E6 do 
            
            // Check known composites, if not present, it is prime.
            let found = knownComposites.Contains(i) 
            if not found then 
                yield i 

            // Add all multiples of i to our sieve, starting 
            // at i and irecementing by i. 
            do for j in i .. i .. int 1E6 do 
                   knownComposites.Add(j) |> ignore 
    } 
    
printfn "Here are the first 10001 primes:" 
Seq.iter (fun p -> printf "\t%d" p) (Seq.take 10001 findPrimes) 

Console.ReadKey(true)
Tuesday, February 10, 2009 12:15:23 AM (Eastern Standard Time, UTC-05:00)  #    Comments [0] -
Bookmark, Tweet, or Share
# Monday, February 09, 2009

Euler #6 is very straightforward. I was concerned about needing to work with numbers > 2^32, but this was not a factor. I initially set my bound to < max instead of <= max which threw my results off my a small margin (1-99 instead of 1-100). Runtime is < 1 ms, only ~2400 ticks.

using System;
using System.Diagnostics;

namespace Euler6
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            long result = SumDifference(1, 100);
            sw.Stop();
            Console.WriteLine("Runtime was " + sw.ElapsedMilliseconds + " ms");
            Console.WriteLine("Runtime was " + sw.ElapsedTicks +" ticks");
            Console.WriteLine("Sum difference is: " + result);
            Console.ReadLine();

        }

        static long SumDifference(int begin, int end)
        {
            return SquareOfSums(begin, end) - SumOfSquares(begin, end);
        }

        static long SumOfSquares(int begin, int end)
        {
            long returnValue = 0;
            for (int i = begin; i <= end; i++)
            {
                returnValue += Convert.ToInt64(Math.Pow(i,2));
            }
            return returnValue;
        }

        static long SquareOfSums(int begin, int end)
        {
            long returnValue = 0;
            for (int i = begin; i <= end; i++)
            {
                returnValue += i;
            }
            return Convert.ToInt64(Math.Pow(returnValue, 2));
        }
    }
}
Monday, February 09, 2009 11:01:49 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0] -
Bookmark, Tweet, or Share

This one was elusively easy since I went with a simple brute-force approach. Execution averages 1 second.
using System;
using System.Diagnostics;

namespace Euler5
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            int lcm = FindLeastCommonMultiple();
            sw.Stop();
            Console.WriteLine("Runtime was " + sw.ElapsedMilliseconds + " ms");
            Console.WriteLine("Runtime was " + sw.ElapsedTicks + " ticks");
            Console.WriteLine("LCM is " + lcm);
            Console.ReadLine();
        }

        private static int FindLeastCommonMultiple()
        {
            for (int i = 2520; i < int.MaxValue; i += 20)
            {
                if (IsDivisibleByRange(i, 1, 20))
                    return i;
            }
            return -1;
        }

        static long SmallestNumber()
        {
            return factorial(20);
        }

        static long factorial(long n)
        {
            long returnValue = 1;
            for (int i = 1; i < n; i++)
            {
                returnValue *= i;
            }
            return returnValue;
        }

        static bool IsDivisibleByRange(int num, int begin, int end)
        {
            for (int i = begin; i <= end; i++)
            {
                if ((num % i) != 0)
                {
                    return false;
                }
            }
            return true;
        }
    }
}
Monday, February 09, 2009 10:44:54 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0] -
Bookmark, Tweet, or Share

There is certainly a more efficient algorithm solve Euler #4, but here is my solution.  Runtime averages around 500ms.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Diagnostics;

namespace Euler4
{
    class Program
    {
        static void Main(string[] args)
        {
            long maxPal = 0;
            long iMax = 0;
            long jMax = 0;
            Stopwatch sw = new Stopwatch();
            sw.Start();
            for (int i = 100; i <= 999; i++)
            {
                for (int j = 100; j <= 999; j++)
                {
                    long curPal = (long)(i * j);
                    if (CheckPallindrome(curPal))
                    {
                        if (curPal > maxPal)
                        {
                            maxPal = curPal;
                            iMax = i;
                            jMax = j;
                        }
                    }
                }
            }
            sw.Stop();
            Console.WriteLine("Runtime was " + sw.ElapsedMilliseconds + " ms");
            Console.WriteLine("Runtime was " + sw.ElapsedTicks + " ticks ");
            Console.WriteLine(iMax + " x " + jMax + " = " + maxPal);
            Console.ReadLine();
            
        }

        private static bool CheckPallindrome(long input)
        {
            PallindromeChecker pc = new PallindromeChecker();
            pc.Pal = input;
            return pc.IsPallindrome();
        }
    }

    class PallindromeChecker
    {
        private long _pal;
        private string forward, reverse;

        public long Pal
        {
            get
            {
                return _pal;
            }
            set
            {
                _pal = value;
            }
        }


        public bool IsPallindrome()
        {
            GetReversedLong();
            if (forward == reverse)
                return true;

            return false;
        }

        private void GetReversedLong()
        {
            forward = Convert.ToString(_pal);
            StringBuilder temp2 = new StringBuilder();

            char[] arr = new char[forward.Length - 1];
            arr = forward.ToCharArray();
            for (int i = forward.Length - 1; i >= 0; i--)
            {
                temp2.Append(arr[i]);
            }
            reverse = temp2.ToString();
        }
    }
}
Monday, February 09, 2009 10:26:20 PM (Eastern Standard Time, UTC-05:00)  #    Comments [1] -
Bookmark, Tweet, or Share
# Wednesday, January 21, 2009

  In an effort to improve my algorithm design I have been working through some of the problems in the Euler Project and have found some of them to be quite intriguing.

Q: What is the largest prime factor of the number 600851475143 ?

I had the following issues to ponder:

  • How to store and manipulate a 40-bit unsigned integer?
  • To utilize recursion or not?
  • Will the algorithm run in less than a minute?
    • If not, how do I make it more efficient?

The end result was simpler and much more performant than I had expected.  The 40-bit input fit into a long just fine.  Recursion was not necessary, and average runtimes are in the single digit millisecond range.  There was even an opportunity to use some very basic LINQ to retrieve the largest factor from the List

Here is the C# code:

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;

namespace Euler3
{
    class PrimeFactorization
    {
        const long ToFactor = 600851475143;
        public static List<long> theFactors = new List<long>();

        static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();
            sw.Reset();
            sw.Start();

            Console.WriteLine("Highest prime factor of (" + ToFactor + ") is " + HighestPrimeFactor(ToFactor));
            sw.Stop();
            Console.WriteLine("Factorization algorithm runtime was: " + sw.ElapsedTicks + " ticks or " + sw.ElapsedMilliseconds + "ms");
            
            Console.ReadLine();
        }

        private static long HighestPrimeFactor(long input)
        {
            bool searching = true;
            long highestFactor = 0;
            while (searching)
            {
                long factor = IsPrime(input);
                if (factor != -1)
                {
                    theFactors.Add(factor);
                    input = input / factor; 
                }
                if (factor == -1)
                {
                    theFactors.Add(input);
                    highestFactor = theFactors.Max();
                    searching = false;
                }
            }
            return highestFactor;
        }

        private static long IsPrime(long input)
        {
            if ((input % 2) == 0)
            {
                return 2;
            }
            else if ((input == 1))
            {
                return 1;
            }
            else
            {
                long threshold = (Convert.ToInt64(Math.Sqrt(input)));
                long tryDivide = 3;
                while (tryDivide < threshold)
                {
                    if ((input % tryDivide) == 0)
                    {
                        Console.WriteLine("Found a factor: " + tryDivide);
                        return tryDivide;
                    }
                    tryDivide += 2;
                }
                Console.WriteLine("Found a factor: " + input);
                return -1;
            }
        }
    }
}
Output:
-Surely a blog entry title such as this earns me some sort of special place in Geek Hell.
Wednesday, January 21, 2009 10:42:21 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0] -
Bookmark, Tweet, or Share
# Monday, November 24, 2008

On my commute into the office this morning I heard of the most recent government bail-out of a private bank, CitiGroup.  This is the second second installment, at CitiGroup alone, of taxpayer dollars being used to try and fix problems the average taxpayer did not create.

While it makes some sense to put measures into place to ensure that major financial companies do not fail and collapse industries beyond the financial sectors, are we fundamentally doing the right thing by the American taxpayer?

Of course not!


photo courtesy of dalydose

Have we reached the point that our government is borrowing money to cover for haphazard and irresponsible behavior by investors and BANKS?  Why yes, yes we have.  But who started this mess?

The way I see it, here is how the snowball began rolling downhill:

  • For the past 10 years or more since the tightening of regulation following the savings and loan crisis, government regulation of the U.S. financial markets has loosened significantly.
  • Banks saw this combined with low interest rates as opportunities to increase their revenues significantly by tapping into the elusive "subprime loan" market.
  • A increasingly credit hungry society took advantage of achieving the previously out of reach so-called "American dream" of home ownership, regardless of whether or not this made good financial sense for them at the time.  (The former "dream" is now more of a nightmare.)
  • Banks decided they could crank up profits by closing a bunch of loans quickly and immediately selling the loans to someone else so they didn't have to keep these risky investments in-house, on their own balance sheets.
  • Banks, unsure of exactly what was the current risk with these subprime mortgages started selling "mortgage backed securities" like hotcakes.  Investors and other banks bought them by the truckload.
  • Ratings agencies tasked with rating the risk of these strange new investment vehicles did a rotten job rating the actual risk and kept slapping AAA ratings on everything in sight
  • Mortgages within these investment backed securities start failing exponentially, and banks start cleverly hiding them off their balance sheets.
  • Banks start to fail, starting with smaller players, then major banks and financial powerhouses start to fail when they can no longer hide their failing mostly-imaginary-now investments.
  • And then....the story comes full circle....and here comes Uncle Sam to save the banks by throwing BORROWED money at the problem to buy up these toxic financial investments.

Who screwed up?  Most everyone involved screwed up in one way or another.  But who enabled this to happen?  As we have seen, given enough rope, investors and banks will hang themselves. 

Perhaps we should stop providing the rope and repeal the Gramm-Leach-Biley Act that allowed the creation of Citigroup and others? 

Hindsight aside, why did we let Lehman fail and others who made the exact same bad decisions survive?

It's time for a large helping of what everyone should have had more of all along, Fiscal Responsibility.

Gramm-Leach-Bliley_Vote_1999.png (138.89 KB)
Monday, November 24, 2008 9:04:39 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0] -
Bookmark, Tweet, or Share
# Tuesday, November 11, 2008

Likely a fake, but a cool video nonetheless....

Plane loses a wing during an air show and then flawlessly lands the plane.


photo courtesy of Travis S.

Video: planeLosesWing.wmv (1.51 MB)

Tuesday, November 11, 2008 3:42:09 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0] -
Bookmark, Tweet, or Share
# Sunday, November 09, 2008

The UAW and what's left of the ‘not-so-big-anymore’ Three.


Photo of the Renaissance Center by Casino Jones

The basic premise of any Trade Union is to work to achieve the common goals of its members. These common goals can range from higher wages, better working conditions, improved benefits, to any key factor affecting member workers. These organizations rose out of 18th century industrialization in order for personnel to collectively bargain for a better deal than they could negotiate on their own. With minimal labor laws on the books and significantly dismal working conditions in industry, these groups championed harsh and unfair conditions through various methods and put power into the hands of rank and file employees who otherwise would have had no capacity to make things any better on their own.

While they bear stark similarities to Medieval Guilds in their member composition, their basic premise diverges significantly. The key difference that makes many modern ones self-defeating is that their interests do not lie in the preservation of the Corporations their workers are employed by. In many cases their only role seems to spite the companies of their members.

Trade Unions exist in order to restore an order lost in the employer’s lack of vested interest in the goals for which the unions are created. They work to restore an order that would otherwise be lost without the collective bargaining power they provide. During early industrialization, these entities were essential to workers.

The case in point is the United Auto Workers Union (UAW). The “Big Three” in the U.S. are often referred to as “Pension Funds that just happen to have car companies attached to them.” Unfortunately, the realization of this just doesn’t become apparent to those who could do something about the situation.

General Motors, Ford, and Chrysler have become dinosaurs that simply cannot compete in today’s market, end of story. Fueled by big oil in more ways than one, they have consciously chosen to continue doing things the way they always worked, “build it and someone will buy it.” With a lineup of models orders of magnitude more numerous than leaner, more efficient competitors, they have lost sight of one of the most basic principals of doing business successfully. “Do one thing, and do it well” Compare the model offerings of Volkswagen or BMW and the problem becomes more apparent. American car companies have become Jacks of All Trades, inherently doing none of them well. Poor management, a disconnect with what consumers want and really need, and I would venture to guess Chronic American Isolationism and inflexibility have all contributed heavily to their serious loss of market share and profitability in the last decade.

However, the most serious problem they currently face is that without major changes in their fundamental ways of conducting business, they will all be gone and carved up to the highest bidder within a year. While too late to save Chrysler, Ford and GM could stand a chance if they would heed the following:

Disband the UAW immediately and fire every worker that refuses to burn their union card.

Harsh as this sounds, and it is, the only way they can hope to have a possible path to viability in the market again is to take this route, effective yesterday if possible. The UAW, and many other Trade Unions, are vestiges of a bygone time and lack relevance in today’s market. How does a group sworn to protect its members do so by being the single largest factor in putting their members’ employers completely out of business? Each and every worker has needs that should be met to the best of the ability of their employer, but let’s be realistic folks. The UAW jumped the shark years ago and this destructive path taken in the name of those it claims to protect is a ruthless paradox.

A recent strike organized by the UAW was one of the most laughable and pointless ventures imaginable. Shutting down the assembly lines for which the end products have few or no interested buyers? This just defies the most basic capitalist principals, Supply and Demand. If the demand for what you supply is severely waning, bullying those in charge of selling these outmoded and undesirable products has no hope of improving your situation.

The pension plans put into place by the Big Three during good economic times, and dependent on the continuation of that prosperity are going to have to be re-evaluated and modified such that they are sustainable. An overarching symbol that applies to several parts of this equation is Sustainability. Sustainable promises to employees, or none at all if nothing can truly be delivered, sustainable products that meet the needs of the consumer, and sustainable long term plans which take into account diminishing demands for the product the way it is currently made.

Obviously there is no silver bullet, but the UAW and the policies it has bargained for and put into place are killing auto-making in this country. This is not something that can be fixed; it needs to go away as quickly as possible such the Big Three can rapidly generate plans for a road to recovery. It’s only a starting point, but a crucial one that cannot be ignored. The dichotomy between what the workers really want and need and what the UAW is fighting for on their behalf should be abundantly obvious by now, but somehow is not. There is no blood to be had from a turnip, no matter how effectively you negotiate with said turnip.

The current efforts by the Obama camp, including House Speaker Nancy Pelosi and Senator Harry Reid, are wholly misguided and if carried through will only prolong the inevitable, and do so on our dime. If companies are poorly managed, make bad decisions, and can no longer compete in the market due to the culminations of decisions they have made and directions they have freely chosen to go, then they fully deserve their fate. Loans and government subsidies seem like a quick fix to do what we can to save these companies, but let’s face it they just aren’t worth saving. They had their fair chance, and face-planted in the dirt time and time again. It’s simple capitalism, and Darwin wouldn’t have it any other way.

Sunday, November 09, 2008 10:56:01 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0] -
Bookmark, Tweet, or Share
# Thursday, October 30, 2008

 ---->   

Twitticator 0.1 is a little application that pushes your Microsoft Communicator status to Twitter each time you make an update to it.

The complete source code can be found below the binaries. Enjoy!

MD5 hash for TwitticatorApp.exe 5611D2B10CAC7B6387F96F3D0DE50EBF
MD5 hash for CommunicatorAPI.dll: 8EA40A1E7C6C3EA0527DD39AA892B72B

Updates and new features coming soon:

  • Bi-directional support to push Twitter status to communicator
  • Secure storage of your Twitter username and password
  • I welcome your suggestions!

No Warranties, expressed or implied.  Use at your own risk.

 

Windows Binaries
twitticator.zip (29.83 KB)*Not currently working, will fix soon!

Source Code
TwitticatorSourceCode.zip (167.75 KB) *Not currently working, will fix soon!

Thursday, October 30, 2008 1:00:15 AM (Eastern Daylight Time, UTC-04:00)  #    Comments [0] -
Bookmark, Tweet, or Share

Stackoverflow.com Rep
Flickr stream
Archive
<February 2009>
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
1234567
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Chris Ballance
Sign In
Statistics
Total Posts: 41
This Year: 6
This Month: 0
This Week: 0
Comments: 9
All Content © 2010, Chris Ballance

Valid XHTML 1.0 Transitional