Archive

Posts Tagged ‘programming’

Project Euler Problem 5

October 28th, 2009 Dallas 1 comment

This one takes three or four seconds to run, so it’s a likely candidate for a rewrite.

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Project_Euler_5
{
 class Program
 {
  static void Main(string[] args)
  {
   new Program();
  }
 
  Program()
  {
   for (int i = 1; ; i++)
   {
    if(isEvenlyDivisibleByRange(i, 11, 20))
    {
     Console.WriteLine(i);
     Console.ReadKey();
    }
   }
  }
 
  bool isEvenlyDivisibleByRange(int value, int lowerBound, int upperBound)
  {
   for (int i = lowerBound; i <= upperBound; i++)
    if (value % i != 0)
     return false;
 
   return true;
  }
 }
}
Categories: Project Euler Tags: ,

Project Euler Problem 4

October 28th, 2009 Dallas 1 comment

This one was easier than I expected. I considered using some sort of lookup table to limit unnecessary calculations (103 x 104 = 104 x 103), but I’m pretty sure that would have only created more overhead.

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

Find the largest palindrome made from the product of two 3-digit numbers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Project_Euler_4
{
 class Program
 {
 
  static void Main(string[] args)
  {
   new Program();
  }
 
  Program()
  {
 
   int largestPalindromicProduct = 0;
 
   for (int i = 100; i <= 999; i++)
    for (int j = 100; j <= 999; j++)
    {
     int product = i * j;
 
     if (product > largestPalindromicProduct && isPalindromicNumber(product))
      largestPalindromicProduct = product;
    }
 
   Console.WriteLine(largestPalindromicProduct);
   Console.ReadKey();
  }
 
  bool isPalindromicNumber(int number)
  {
   string value = number.ToString();
 
   for (int i = 0; i < value.Length; i++)
   {
    if (!value[i].Equals(value[value.Length - 1 - i]))
     return false;
   }
 
   return true;
  }
 }
}
Categories: Project Euler Tags: ,

Quick Guide to Customizing Keyboard Bindings in OS X

October 21st, 2009 Dallas No comments

I love my Mac. In fact, I love just about everything about it. The places where it fails to please tend to be when handling input devices – specifically keyboard and mouse input.

The default acceleration curve for mouse motion is bizarre. There are two speeds – slow and relativistic. That was fixed on day one thanks to USB Overdrive. While it does cost $20, it is well worth it for the ability to use an external mouse.

The keyboard bindings took a while to become irritating, probably because as a laptop user some degree of keyboard discomfort is to be expected. The confined space means that come concessions must be made; keys are squished or missing, some are given dual roles, and some are out of place. As a programmer, I do a lot of navigation on the keyboard. Unless I’m moving between large chunks of code, I don’t touch the mouse at all. Because of this, I use a full size USB keyboard to give me access to all of those nifty keys that are missing on the laptop (Home/End/Page Up/Page Down). Unfortunately OS X maps different functions to some of those keys than I’m used to. For instance, “Home” and “End” scroll to the top and bottom of the document instead of the beginning and end of the current line of text. Fortunately I’ve found a resolution.

User specific keyboard bindings are stored in /Users/{User Name}/Library/KeyBindings/DefaultKeyBinding.dict. Note that this directory and file may not exist at first, so you may need to create it. If this is the case, you’ll also need to create the DefaultKeyBinding.dict file. I recommend copying the system wide keyboard bindings file from /System/Library/Frameworks/AppKit.framework/Resources/StandardKeyBinding.dict. This will give you a good base to work from. I don’t recommend modifying this file directly, as if you mess something up you’re really screwed.

Now that you’ve copied the file, all you have to do is modify it to suite your fancy. It’s possible to do this by hand, but it’s easier to use a specialized editor. I recommend the KeyBindingsEditor (free download). Fire up KBE and look for the “Home” key (make sure the “Modifiers” field is clear), then change the action to moveToBeginningOfLine. Do the same for the “End” key, except the action will be moveToEndOfLine. Make any other modifications you want, save the file, and restart any running applications to obtain the new bindings.

Easy right? There are a few exceptions (imagine that). Some applications, such as Firefox and Eclipse, ignore these key bindings altogether. I’ve figured out how to fix Firefox, but I’m not an Eclipse user (give Netbeans another try, the new version is really nice). Fixing Firefox isn’t nearly as straightforward and can’t be done on a per user basis (all changes are global). In fact, this was reported as a bug back in 2003 and is still an open item.

First you need to obtain the platformHTMLBindings.xml file. This contains all of the keyboard bindings used by Firefox and its location depends on what version you’re running. For pre-1.05 users (are there any?), the file is in /Applications/Firefox.app/contents/MacOS/res/builtin/. For the rest of us it’s inside the /Applications/Firefox.app/contents/MacOS/chrome/toolkit.jar archive. A .jar archive is nothing more than a renamed .zip file, so you can extract its contents as you would any other .zip. Once you extract the contents, you’ll find the file in the content/global/ directory. After you modify it you will need to update toolkit.jar with the new version so don’t delete anything and be sure to back up the original.

Inside platformHTMLBindings.xml, you’ll need to find and modify the following nodes in the <handlers> section of <binding id="inputFields">, <binding id="textArea">, and <binding id="editor">:

<!-- Change cmd_scrollTop to cmd_beginLine -->
<handler event="keypress" keycode="VK_HOME" command="cmd_scrollTop"/>
 
<!-- Change cmd_scrollBottom to cmd_endLine -->
<handler event="keypress" keycode="VK_END"  command="cmd_scrollBottom"/>

You can find other commands at Customizing Mozilla.

After you’ve made your modifications zip the folder back up, change the extension to .jar,  and copy it over the toolkit.jar archive.

Whew, OK that’s it. If you have any questions shoot me an e-mail at edwardsdl@gmail.com.

Categories: General Tags: , , , ,

Revenge is sweeter than life itself. So think fools.

October 19th, 2009 Dallas 1 comment

Last Friday my boss whipped up a program that would spam my phone with text messages.  It was cute – until I had 350 unread text messages and my phone wouldn’t stop vibrating.

Over the weekend my thirst for vengeance overpowered me, and I decided to give him a taste of his own medicine. I spent about 30 minutes writing an app that would not only spam his phone with text messages, but fill his e-mail inbox too.  Oh yes, that would show him!

Like most developers though, I can’t just leave well enough alone. An e-mail loop is just too simple, features must be added, functions must be enhanced.

Address spoofing – check.
Randomized subject lines – check.
Randomized content – check.

Then things got out of hand. Harmless subjects gave way to “Free Viagra!” and “You’re fired!”.  Messages became, “I think I’m pregnant”.  Worst of all, spoofed addresses went from mutual friends to mutual bosses.

At the time I thought my hellish little spam app was brilliant.  When Jaime called I expected him to concede victory, to beg me to stop killing his inbox.  Nope, not even close.

“Hey dude, I think you have a virus.  It’s sending e-mails to Barry.”  Barry is our boss’, boss.

Wait, what?  I didn’t code it to do that.  It would look like it came from him, but it surely wouldn’t send it to him.  How could this be?  I checked my e-mail to see if I was getting anything.  There it was, Mr. Mailer Daemon informing me that my message could not be delivered.   Apparently Jaime had disabled incoming text messages coming from e-mail gateways, and AT&T cheerily notified the sender each time one of these messages failed to deliver.  Oh, damn.  The one feature I didn’t add was the one to route the reply address back to me.  Actually, in hindsight, I think the one feature missing was common sense.

I never got my revenge.  I did get a heavy dose of humiliation, but everyone needs that once in a while.

For anyone wanting to hang themselves, or perhaps simply to amuse themselves at my expense, I posted the code below.

Read more…

Rent A Coder

October 15th, 2009 Dallas 1 comment

My boss and I have decided to try our hand(s?) at a little freelance development.  Neither of us have done this before, but it looks fun so we decided to have a go.

So far we’ve placed a bid on a pretty small project involving a wine database management and reporting software. It’s nothing too ambitious and I have no doubt we’d be able to handle this easily. Hopefully we’ll get the bid, though last I checked there were 17 others. Wish us luck!

Categories: General Tags: ,

Project Euler Problem 3

October 6th, 2009 Dallas 3 comments

This one was actually a little bit of a challenge.  My first attempt got the right answer, but took about 3 hours to do so.  After consulting some online resources (my girlfriend), and rediscovering prime factorization I rewrote  the program.  Now it takes under a second to solve the puzzle.

I thought this was interesting: if you search “prime factor of 600851475143” on Wolfram Alpha it gives you all of the factorizations, including the answer.

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Project_Euler_3
{
 class Program
 {
  static void Main(string[] args)
  {
   new Program();
  }
 
  Program()
  {
   ulong compositeValue = 600851475143;
   ulong compositeFactor = compositeValue;
   bool isDone = false;
 
   while (!isDone)
   {
    ulong prime = getNextPrime();
 
    while (compositeFactor % prime != 0)
     prime = getNextPrime(prime);
 
    compositeFactor /= prime;
 
    if (isPrimeValue(compositeFactor))
     isDone = true;
   }
 
   Console.WriteLine(compositeFactor);
   Console.ReadKey();
  }
 
  ulong getNextPrime()
  {
   return getNextPrime(0);
  }
 
  ulong getNextPrime(ulong value)
  {
   ulong nextPossiblePrime = value + 1;
 
   for (; !isPrimeValue(nextPossiblePrime); nextPossiblePrime++) ;
 
   return nextPossiblePrime;
  }
 
  bool isPrimeValue(ulong value)
  {
   for (ulong i = 1; i <= (ulong)Math.Ceiling(Math.Sqrt(value)); i++)
   {
    if (value == 1)
     return false;
    if (i == value)
     return true;
    else if (value % i == 0 && i != 1)
     return false;
   }
 
   return true;
  }
 }
}
Categories: Project Euler Tags: ,

Project Euler Problem 2

October 5th, 2009 Dallas No comments

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, …

Find the sum of all the even-valued terms in the sequence which do not exceed four million.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Project_Euler_2
{
 class Program
 {
  static void Main(string[] args)
  {
   new Program();
  }
 
  Program()
  {
   List<ulong> fibonacciSequence = new List<ulong>();
   fibonacciSequence.Add(0);
   fibonacciSequence.Add(1);
 
   ulong sum = 0;
   bool isDone = false;
 
   for (int i = fibonacciSequence.Count; !isDone ; i++)
   {
    ulong nextFibonacciNumber = fibonacciSequence[i - 1] + fibonacciSequence[i - 2];
 
    fibonacciSequence.Add(nextFibonacciNumber);
 
    if(nextFibonacciNumber > 4000000)
     isDone = true;
    else
     if (nextFibonacciNumber % 2 == 0)
      sum += nextFibonacciNumber;
   }
 
   Console.WriteLine(sum);
   Console.ReadKey();
  }
 }
}
Categories: Project Euler Tags: ,

Project Euler Problem 1

October 5th, 2009 Dallas No comments

To keep the mental gears spinning I’m going through the Project Euler problems.  I’m going to solve them primarily in C#, but I may jump between languages depending on the problem.

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Project_Euler_1
{
 class Program
 {
  static void Main(string[] args)
  {
   new Program();
  }
 
  Program()
  {
   int sum = 0;
 
   for (int i = 1; i < 1000; i++)
    if (i % 3 == 0 || i % 5 == 0)
     sum += i;
 
   Console.WriteLine(sum);
   Console.ReadKey();
  }
 }
}
Categories: Project Euler Tags: ,

Battleopolis / Grant’s Ants / Ants / Arcomage

September 22nd, 2009 Dallas No comments

Grant and I are working on rewriting a game he created several years ago called “Grant’s Ants” (he later renamed it to “Battleopolis”).  Originally it was a clone of a game called Ants by Miroslav Nemecek (which was itself a clone of Arcomage).  Grant was mainly interested in the rewrite since he wanted to learn C#, and I’m in it because I found the game strangely addictive.

You can find Grant’s original version on his blog.

You can download Miroslav’s version at Gosfish Games or I have it hosted here.

Categories: Grant's Ants Tags: , ,