Problem 005

March 8th, 2010 Kevin Fairchild No comments

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 positive number that is evenly divisible by all of the numbers from 1 to 20?

Solution:
Function Prob005() As Integer
Dim FoundResult As Boolean = False
Dim k As Integer = 0
While FoundResult = False
k += 1
FoundResult = True
For f As Integer = 1 To 20
If k Mod f <> 0 Then
FoundResult = False
End If
Next
If FoundResult = True Then Return k
End While
End Function

Summary:
This was pretty easy, but it seems like the problems are building upon each other, so I’m sure the next few are going to be a major pain…

Tags:

Problem 004

March 8th, 2010 Kevin Fairchild No comments

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.

Solution:
Function Prob004() As Integer
For k As Integer = 100 To 999
For f As Integer = 100 To 999
Dim intResult As Integer = k * f
Dim strResult As String = intResult.ToString
If strResult = StrReverse(strResult) AndAlso intResult > Prob004 Then
Prob004 = intResult
End If
Next
Next
End Function

Summary:
This one was pretty quick and fun.

Tags:

Problem 003

March 7th, 2010 Kevin Fairchild No comments

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

What is the largest prime factor of the number 600851475143 ?

Solution:
Function Prob003() As Long
Dim Src As Long = 600851475143
Dim PrimeCol As New Collection
For tmpVal As Long = 2 To Src
If tmpVal > System.Math.Sqrt(Src) Then Exit For
If IsPrime(tmpVal) = True Then
PrimeCol.Add(tmpVal)
End If
Next
For k As Integer = 1 To PrimeCol.Count
If Src Mod PrimeCol(k) = 0 Then
If PrimeCol(k) > Prob003 Then Prob003 = PrimeCol(k)
End If
Next
End Function

Summary:
This one was kind of a pain. I had forgotten a lot about what makes a prime and the various rules, tricks, etc. when dealing with them…

Tags:

Problem 002

March 4th, 2010 Kevin Fairchild 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.

Solution:
Function Prob002() As Long
Dim Term1 As Long = 1
Dim Term2 As Long = 2
Dim tmpTerm As Long = 0
While Term1 < = 4000000
If Term1 Mod 2 = 0 Then Prob002 += Term1
tmpTerm = Term1 + Term2
Term1 = Term2
Term2 = tmpTerm
End While
End Function

Summary:
Even though it'll never be as exciting as in the movie Pi, I do love working with Fibonacci sequences.

Tags:

Problem 001

March 3rd, 2010 Kevin Fairchild No comments

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.

Solution:
Function Prob001() As Integer
For k As Integer = 3 To 999
If k Mod 3 = 0 OrElse k Mod 5 = 0 Then Prob001 += k
Next
End Function

Summary:
Easy-peasy.

Tags:

Another One Bites The Dust…

March 2nd, 2010 Kevin Fairchild No comments

A lot of my favorite blogs have been fading away in the past few years and there hasn’t been enough good ones stepping up to take their places.

One developer’s blog I absolutely loved back in the day was Steve Yegge’s Blog Rants. Even though he was always writing crazy-long posts, they were usually quite good, witty, and informative. His last post was in May 2009. While I’ll probably never have my own startup, write in Haskell, or whatever it was he was ranting about, it was always a bit interesting seeing things in a different light.

With a well-written blog and a pretty large audience, I used to read Jeff Attwood’s “Coding Horror” blog all of the time. Now that his focus is on his new company (and rightfully so, I guess), he tends to have a lot less meaningful posts. Instead, it’s either a quick couple paragraphs to talk about new stuff on StackOverflow or it’s just something to get people to react strongly and leave lots of comments agreeing or disagreeing with what he’s written. I don’t get a whole lot out of it anymore, which is a shame.

Last but certainly not least, I guess Joel is going to be added to my list…

I’m pretty bummed to read that Joel Spolsky is giving up on blogging, writing for Inc., and doing podcasts. He had a lot of great insights to share. I really liked hearing about things like evidence-based time estimates on projects. I used to love reading his blog and even have some of the books.

I guess it’s not THAT bad, though. Blogs like these are great when you’re still wet-behind-the-ears and need someone to authoritatively tell you what should and shouldn’t be done. Once you get a feel for real-world development and have a good enough grasp on best-practices, you really don’t need them as much.

If the blogging paradigm is slowly dying for the big and influential bloggers I like, I guess that’s just the way it goes… Thankfully, I still have a few good tech podcasts left :)

Getting To Know Eu…ler

February 26th, 2010 Kevin Fairchild No comments

One of the best ways to get to know a programmer is to see their code. You might not know their favorite hobbies, favorite foods, or favorite football team (trick question, programmers don’t like football. Hehe), but getting a glimpse at how someone goes about solving problems can really be quite helpful.

With that in mind, I’m going to be taking part in ProjectEuler.net. It’s all math-based to one degree or another. I am horrible at math, really. I know enough to get by, but a lot of it I’ve forgotten since being out of school. But, I must admit, I love a figuring out puzzles — especially by programming — so I’m anxious to get started.

If you’d like to follow along, feel free to set up an account on their site and try the stuff out yourself. It can get quite addictive :)

Since I’m going to post code-samples here, I might as well include one of my “helper” routines I ended up using in a couple of solutions. Since a few of the problems involved prime numbers, I made a generic function for allowing me to determine whether a specific number is a prime or not.

I’m sure there are more effective ways of figuring out if a number is a prime, but math was never a subject a particularly excelled at…

Function IsPrime(ByVal tmpVal As Long) As Boolean
Dim strVal As String = tmpVal.ToString

'If the number ends in 0,2,4,5,6,8 then it's NOT prime (might be a composite)
If strVal.Length > 1 AndAlso strVal.Substring(strVal.Length - 1, 1) = "0" Then Return False
If strVal.Length > 1 AndAlso strVal.Substring(strVal.Length - 1, 1) = "2" Then Return False
If strVal.Length > 1 AndAlso strVal.Substring(strVal.Length - 1, 1) = "4" Then Return False
If strVal.Length > 1 AndAlso strVal.Substring(strVal.Length - 1, 1) = "5" Then Return False
If strVal.Length > 1 AndAlso strVal.Substring(strVal.Length - 1, 1) = "6" Then Return False
If strVal.Length > 1 AndAlso strVal.Substring(strVal.Length - 1, 1) = "8" Then Return False

'Except for the number 2, if a number is even, it's always composite
If tmpVal <> 2 AndAlso tmpVal Mod 2 = 0 Then Return False

'If a number's digits add up to a number which is divisible by 3, it's composite
Dim tmpDigitSum As Integer = 0
If strVal.Length > 1 Then
For k As Integer = 0 To strVal.Length - 1
tmpDigitSum += strVal.Substring(k, 1)
Next
If tmpDigitSum = 3 Then Return False
End If

'If a number's square root is an integer, it's a composite
If System.Math.Sqrt(tmpVal) = CInt(System.Math.Sqrt(tmpVal)) Then Return False

'If it's not divisible by itself, it's not Prime
If tmpVal Mod tmpVal <> 0 Then Return False

Dim MaxVal As Long = System.Math.Sqrt(tmpVal)

For k As Long = 2 To MaxVal
If tmpVal Mod k = 0 AndAlso k <> 1 Then
Return False
End If
Next

Return True
End Function

Java

February 22nd, 2010 Kevin Fairchild No comments

I don’t just hate Java. I loathe it.

In the past, I’ve done everything I can do avoid using it.

If I want to develop for Android, though, I guess I need to bite the proverbial bullet and find a copy of “Java For Dummies”.

There are a few somewhat niche things I’d like to do with my phone that there simply aren’t applications for yet.

The only mildly-practical thing I might make is a bluetooth-based remote control app for my Playstation 3. And, really, that’s just more about teaching myself than something I’d actually use much of.

Other than that, I have two main “pet projects” I’d like to get working.

The first application I want to make really just comes down to my desire to have things as automated as possible when it makes sense. In this case, I have a podcast app (Google Listen) which I only listen to when I’m in the car. Since my car’s stereo uses Bluetooth, I’d like to launch Google Listen when it detects the car stereo (which is only going to be while the car is powered on) and close it when the signal is lost (if it was automatically launched earlier). Granted, I could probably use the “Locale” app for that, but — hey — that costs money and is not nearly as much fun as making something myself, right? ;)

The second project I have in mind, ties in with my various GPS-based social networking apps (FourSquare, Loopt, BrightKite, etc.). Essentially, what I’m looking to do is to “check in” at my favorite places if I’ve been there for at least x-amount of minutes and I haven’t already checked in recently (whether manually or via this program). All of these have APIs that can be tied into, so it shouldn’t be TOO hard. I’d just need to make sure not to abuse the GPS too much or it’ll drain my battery… There are some way to ensure the GPS only comes on when the phone gets close, though, by looking at nearby cell towers or wifi networks, for instance. It’s not fool-proof, but probably would be better than having the GPS running all of the time.

Tags: , ,

To The Root Of The Issue

February 22nd, 2010 Kevin Fairchild No comments

I’ve had the MyTouch 3G from T-Mobile for a while now. It’s a good, solid phone. There are a lot of nice apps and features and I instantly fell in love with the Android OS. It was a new experience and I really liked doing stuff on the phone.

Still, that feeling of ‘newness’ only lasts for so long…

Last week, I rooted it and installed the latest stable Cyanogen build. It’s been an absolutely wonderful experience. And really was a night-and-day difference between the official build and the custom ROM.

Plus, there are all sorts of neat tricks to eek out even more benefits. So far, my favorite is simply referred to as the “10MB hack”. The result is an extra 10MB of system memory to play with. Granted, that isn’t a WHOLE lot, but when you’re talking about smart-phones, every little bit counts.

Do I Look Fat In These Pixels?

February 17th, 2010 Kevin Fairchild No comments

As I mentioned in my A New Year’s (Image) Resolution post, I’ve had a side project that requires dealing with images that come in at varying resolutions. Regardless of resolution, though, they all have the same basic requirements. I need to be able to “stamp” the pages with a dynamic header text, the quality needs to remain high enough that they can be viewed/printed, and — ideally — I need to have the header text be relatively the same size once it’s scaled on the screen or printed.

I think I’m a little closer to where I want to be, but it’s still not quite ideal.

I have a font object that’s 10-point bold “Courier New”. I use a Generic Typographic string format with No Clip and Word Trimming. I then use the MeasureString method on my graphics object to store the string size. Once I have that, I basically just subtract the height of that object from the width and height of my target rectangle and offset my starting position of DrawImage accordingly.

What I had hoped it would do is, if writing my header required 4mm of height, I’d scale the image down enough to give me 4mm of room on both sides (to keep the same aspect ratio) and write the header text. Similarly, if the header text somehow required an inch of height, it’d scale down so there was an inch of room.

It does seem to work, but just not as good as I had hoped. On some images, the header text was very close to the original fax text (which I want). In others, though, the spacing was a bit more pronounced and I’m not quite sure why just yet.

Ideally, what I want to do next, is have a way to determine the ideal font size based on the image. If I’m dealing with a basic image at 1275×1755 (at 96dpi H-Res and 96dpi V-Res), I would need a different size font than if I was working with a 1728×1052 image (at 203dpi H-Res and 98dpi V-Res).

I just haven’t quite figured out how I’ll actually DO that… yet :)

Actually, I take that back… my true ideal solution is to toss the text on the top or bottom of the page in a spot that isn’t being used a not have to mess with the scaling at all. But that’s more of a Phase-II plan…

Tags: