Archive

Archive for the ‘Programming’ Category

Primality Test v2.0

December 14th, 2009 Eric Silva No comments

After feedback from some friends of mine, and doing a little bit of background research, I am writing this update to my original post last week.  As it turns out, by checking all the numbers in the form 6k ± 1 \scriptstyle{}\leq\sqrt n instead of checking each number up to the input value, I have increased the speed by 7 times! determineIsPrime3 (line 48 below) is the fastest algorithm so far.  determineIsPrime2, a simple comparision against the \scriptstyle\sqrt n, was twice as fast as the original algorithm.

For now, I am putting this one to bed.  It was a fun exercise, but I have got what I need from it.

'''
Checks the specified value to determine if it is a prime number.
If it is not prime the divisor will be returned instead.

@author: Eric Silva
'''

import math, time

#Change this value to whatever value you want to test for prime.
#testValue = 65027
#testValue = 155188329701
testValue = 99194853094755497
#testValue = 10888869450418352160768000001
print 'Testing %d...' % testValue

def determineIsPrime(testPrime):
    if testPrime % 2 == 0:
        return 'Divisible by 2'
    if testPrime % 3 == 0:
        return 'Divisible by 3'
    testNum = 7
    testLimit = testPrime
    while testLimit >= testNum:
        if testPrime % testNum == 0:
           return 'Divisible by %d' % testNum
        testLimit = testPrime/testNum

        testNum = testNum + 2

    return '%d is prime!' % testPrime

def determineIsPrime2(testPrime):
    if testPrime % 2 == 0:
        return 'Divisible by 2'
    if testPrime % 3 == 0:
        return 'Divisible by 3'
    testNum = 5
    sqrt = math.sqrt(testPrime)
    while testNum <= sqrt:
        if testPrime % testNum == 0:
           return 'Divisible by %d' % testNum

        testNum = testNum + 2

    return '%d is prime!' % testPrime

def determineIsPrime3(testPrime):
    if testPrime % 2 == 0:
        return 'Divisible by 2'
    if testPrime % 3 == 0:
        return 'Divisible by 3'
    testNum = 7
    sqrt = math.sqrt(testPrime)
    while ((6 * testNum) + 1 <= sqrt) or ((6 * testNum) - 1 <= sqrt):
        if testPrime % testNum == 0:
           return 'Divisible by %d' % testNum

        testNum = testNum + 2

    return '%d is prime!' % testPrime

startTime = time.time()
result = determineIsPrime(testValue)
endTime = time.time()

print result
print '1. Calculation took %f s\n' % (endTime - startTime)

startTime = time.time()
result = determineIsPrime2(testValue)
endTime = time.time()

print result
print '2. Calculation took %f s\n' % (endTime - startTime)

startTime = time.time()
result = determineIsPrime3(testValue)
endTime = time.time()

print result
print '3. Calculation took %f s\n' % (endTime - startTime)

Results:

Testing 99194853094755497...
99194853094755497 is prime!
1. Calculation took 202.609000 s

99194853094755497 is prime!
2. Calculation took 114.813000 s

99194853094755497 is prime!
3. Calculation took 28.781000 s

Determining if a Number is Prime

December 10th, 2009 Eric Silva No comments

While working on some caching settings, I had a need to know if a number is prime. I wrote this little Python script which will tell you if the number defined in the script is indeed a prime.

'''
Checks the specified value to determine if it is a prime number.
If it is not prime the divisor will be returned instead.

@author: Eric Silva
'''

#Change this value to whatever value you want to test for prime.
testValue = 3011

def determineIsPrime(testPrime):
    if testPrime % 2 == 0:
        return 'Divisible by 2'
    testNum = 3
    testLimit = testPrime
    while testLimit >= testNum:
        if testPrime % testNum == 0:
           return 'Divisible by %d' % testNum
        testLimit = testPrime/testNum

        testNum = testNum + 2

    return '%d is prime!' % testPrime

result = determineIsPrime(testValue)

print result

Peer Code Review: An Agile Process

December 10th, 2009 Eric Silva No comments

Smart Bear Software recently released a white paper discussing the misconception that peer code review is a hindrance to Agile development methodologies.  For anyone who regularly performs peer code reviews, would like to start performing them, or thinks they are an obstacle when it comes to Agile development should read this paper.

The paper talks about the history of code review, how code review aligns with Agile, types of lightweight code review, and techniques to perform optimized code reviews.  Some of the key statements that I took away from the paper are this:

  • Code review allows for “continuous attention to technical excellence and good design”.  These enhances the agility of the code, the developers working on the code, and the overall Agile process.
  • Code review “promotes sustainable development”.  The “bus number” concept is one that I use consistently when promoting peer code review in my own workspace and corporate environment.  The white paper explains it simply, “How many team members would have to get struck by a bus before no one was left that understood the code?  If the bus number for a section of the code is less than two, then that’s a problem.”
  • The final take-away comes from the Agile Manifesto Principles itself, “The best architectures, requirements, and design emerge from self-organizing teams.”  The same is true of quality peer code review; “If peer code review is mandated by someone outside the team, its chance of success decreases.  If team members do not want code review to succeed, it will fail.”
    I use Code Collaborator.  I think it’s the best tool out there for performing peer code review, especially with distributed development teams.  I think peer code review allows developers to become better developers through the visibility and social nature of performing the review itself.  My opinions may be a bit biased, based upon my experiences with Smart Bear Software, but, in fairness, this white paper discusses the enormous benefits of peer code review without discussing specific products. It only discusses the principals and observed benefits, and is in no way a sales pitch to buy their product.

GWT 2.0 Released!

December 9th, 2009 Eric Silva No comments

I just saw on my RSS that GWT 2.0 has been released.  Gonna go play now. Bye.

Converting a Visual Studio 2005 Web Application Project to a Visual Studio 2008 Web Application Project

December 6th, 2009 Eric Silva No comments

For anyone looking to upgrade their VS 2005 Web Application project to VS 2008, I found good walkthrough provided by Microsoft here.

Visual Studio 2008 and Visio for Enterprise Architects

December 3rd, 2009 Eric Silva 6 comments

I have started working on a new project in .NET and the Problem Domain (PD) was modeled in Visio UML.  Fantastic.  Now I wanted to forward engineer the UML into C# classes to begin development, but wait, I can’t.  I only have Visio 2007 Professional, and the forward engineering features are only available in the Visio for Enterprise Architects version.  Okay, not a problem, I’ll go download it from MSDN.

After starting the installation, I got an error message, “You must first install one of the qualified Visual Studio editions”.  What the hell?  Visual Studio 2008 isn’t good enough?  I sure as hell don’t want to install another, older version Visual Studio just so I can install Visio for EA.

After poking around, I came across this registry trick.  Visio for Enterprise Architects is looking for the existence of the following key:

HKLM\Software\Microsoft\VisualStudio\8.0\Setup\VS\VSTD\

Within this key should be the String value “ProductDir”.  The text value for ProductDir can be anything other than a null value.

Once you add this key to your registry, the Visio for Enterprise Architects installer will work as hoped.

Modeling in Color

November 24th, 2009 Eric Silva No comments

I just finished leading the modeling session as chief architect of the problem domain (PD) for a new application, and I had forgotten how exhausting the process is.  We had a fantastic moderator who did well to keep us on point and focused toward our objective.  We have about 50 or so objects, and I think we will need about 10-20 more moment-interval (pinks) to handle the additional audit trail requirements.

I am working on finishing up merging and formatting the model notes from all the developers who participated.  I should be done with that by the end of the day.  Then it’s on to the real work of breaking this thing down into discrete features that we can put into work packages and get this thing built.

I did have a few “Being John Malkovich” moments during the modeling session when I thought Jeff De Luca was in my head controlling my actions, but it was all for the better.

The great thing about modeling a PD is the sense of accomplishment at the end of two weeks and seeing your problem domain in front of you on a 6’x10’ sheet of paper.

ACES_4372

Now I need to starting working on the UI and SI design.

PyXML for Python 2.5

October 30th, 2009 Eric Silva No comments

I was looking for a version of PyXML for Python 2.5 and had some difficulty due to the project being unmaintained.  I was able to find someone that compiled PyXML 0.8.4 for Python 2.5.  You can find it here.

Categories: InterWebNet, Programming Tags: ,

Windows Bulk File Move Script

October 13th, 2009 Eric Silva 1 comment

We have some applications that leave files sitting in a folder location out on some file server.  Eventually the disk space gets used up, or Windows starts choking because there are too many files in a directory, etc.  To delete the ‘old’ files, a support technician will try to connect to the file share, sort the files by date, and delete the files within a date range, e.g., older than a year.

What can happen, especially on a Window server, is that the operating system will attempt to load all the file information into memory, so that you can see it, and then tell it to sort all those files by date, which in turn, increases the time and memory needed by the OS to accomplish this.  If the file size/quantity is too great, the problem that caused you to want to clean up the disk space will reappear and choke Windows once more.

After going through this more than twice, I decided to write a JavaScript and execute it using the Windows Scripting Host (WSH).  The script performs very well and was able to iterate through a directory of 700,000+ files in a matter of an hour.  This was including the log statements from being enabled.  Disabling the ‘Echo’ statements may increase performance.

Here is the script:

var fso = new ActiveXObject('Scripting.FileSystemObject');
var argArray = new Array(WScript.Arguments.Named("src"), WScript.Arguments.Named("s"), ".");
var inSrcDirName = coalesceArray(argArray);

argArray = new Array(WScript.Arguments.Named("dest"), WScript.Arguments.Named("d"));
var archiveDir = coalesceArray(argArray);

argArray = new Array(WScript.Arguments.Named("year"), WScript.Arguments.Named("y"));
var beforeYear = coalesceArray(argArray);

if (archiveDir != null) {
	if (!fso.FolderExists(archiveDir)) {
		fso.CreateFolder(archiveDir);
	} else {
		fso.CreateFolder(archiveDir + "1");
	}
}

var gfldr = fso.GetFolder(inSrcDirName);

ArchiveFiles(gfldr);

function ArchiveFiles(fldr) {
	var i, bias;

	WScript.Echo("Loading all files...");
	for (files = new Enumerator(fldr.files); !files.atEnd(); files.moveNext()) {
		f = files.item();
		dt = new Date(f.DateCreated);
		bias = dt.getTimezoneOffset();

		dt.setMinutes(dt.getMinutes() - bias);

		fileYear = dt.getFullYear();
		//WScript.Echo(f.Name + ":" + fileYear);
		if (beforeYear > fileYear) {
			WScript.Echo("Moving \"" + f.Name + "\" with year " + fileYear + " to " + archiveDir);
			f.Move(archiveDir+"\\" + f.Name)
		} else {
			WScript.Echo("Skipping \"" + f.Name + "\" with year " + fileYear);
		}
	}
	WScript.Echo("Done loading files.");

}

function coalesceArray(arr) {
	var i;
	for (i = 0; i < arr.length; i++) {
		if (arr[i] != null) {
			return arr[i];
		}
	}
	return null;
}

Executing the script is performed by passing a source directory, destination directory (to move the files to), and a year to mark the year that all files must be equal to or younger than to not be moved as parameters.

Example:

cscript movefiles.js /s:c:\myapp\data /d:c:\backup\archive_2006 /y:2007

This will move all files from the c:\myapp\data folder that were created before January 1, 2007 to the c:\backup\archive_2006 folder.  The script can be easily modified to use the ‘modified’ or ‘accessed’ date instead.

Hope this helps someone else as much as it helped me.

Creately.com: Online Diagramming and Design

September 3rd, 2009 Eric Silva No comments

Pretty neat application that just went public.  In was in beta for several weeks and is a nice replacement for Microsoft Visio if you need to share and collaborate on a diagram with remote users.

http://creately.com