# Picture Pages : Getting Started with Arduino!

I’ve finally gotten around to messing with Arduino, and it’s been a lot of fun! I picked up an “Experimentation Kit” from Adafruit and it contained everything I needed to get started, including a little booklet of circuit examples.

I don’t know that I’ve gained any programming insight from my experimentations, but I’d wager most developers would enjoy this sort of thing. It could make an excellent Christmas gift for that hard-to-buy-for geek in your family!

# Project Euler: Problem 35 in Ruby

I used my prime generator from Problem 27 for this one. It would have been faster to build the rotation into my generator, but it ran fine without it.

How many circular primes are there below one million?

require 'prime_generator' primer = Prime_Generator.new 1_000_000 def is_rot_prime? primer, chars chars.size.times do |i| chars = Array.new(chars.size) { |i| chars[i - 1] } return false if !primer.is_prime?(chars.join("").to_i) end true end count = 0 primer.stack.each do |n| count += 1 if is_rot_prime? primer, n.to_s.split("") end # subtract 1 because "1" doesn't count puts count - 1

Speaking of the rotation, the ruby array initialize methods and negative indexers make it a cinch to rotate. How cool is this?

chars = Array.new(chars.size) { |i| chars[i - 1] }

# Project Euler: Problem 34 in Ruby

Practically every Project Euler problem benefits from memoization, however the issues I ran into with #34 had nothing to do with my algorithm.

The first hurdle was figuring out the upper bound. After scratching around in my notebook, I figured any number I would be looking for would have 7 digits or less. That gives us an upper bound of 9! * 7.

The second hurde took me much, much longer to figure out.

Here’s the secret: **0! = 1**

I had taken it for granted that 0! would (of course!) be 0, and I had pre-filled my cache with the number 0. I went round, and round, and round, and round, and round, and round before figuring out (quite accidentally) my error.

It doesn’t make any sense to me, but you can’t argue with math!

Find the sum of all numbers which are equal to the sum of the factorial of their digits.

# cache the digit factorials factorials = [1] (1..9).each do |i| factorials.push(i * factorials.last) end result = 0 (3..2_540_160).each do |n| sum = n.to_s.split("").inject(0) do |sum,n| sum + factorials[n.to_i] end result += n if n == sum end puts result

Runs in just under a minute ðŸ™‚

# Project Euler : Problem 36 in Ruby

I’ve been ill today, but there’s no better medicine than an easy Project Euler problem! I tried doing some bitwise magic, but in the end my simplest solution proved the fastest as well.

Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.

def palindrome? s s == s.reverse end sum = (1..1_000_000).inject(0) do |sum, n| sum += palindrome?(n.to_s) && palindrome?(n.to_s 2) ? n : 0 end puts sum

# Project Euler: Problem 33 in Ruby

It’s fugly, but it works. The hardest part was understanding the question. If the longer description didn’t say that there were exactly 4 fractions, I might have gone crazy.

For reals.

Discover all the fractions with an unorthodox cancelling method.

top, bottom = 1, 1 (10..98).each do |i| ((i/10)..9).each do |jt| jt *= 10 (1..9).each do |jo| j = jt + jo next if i >= j if i % 10 == j / 10 && i.to_f / j == (i / 10).to_f / (j % 10) top *= i bottom *= j end end end end puts bottom / bottom.gcd(top)

# Find the Longest Palindrome in a String

I recently discovered programmingpraxis.com, and one of the more recent posts dealt with one of the greplin programming challenges. I figured since people were posting their code there, then it wouldn’t be too bad for me to post mine here!

Find the Longest Palindrome in a string:

I doubt this is an optimal solution, but I like how it works:

text = "FourscoreandsevenyearsagoourfaathersbroughtforthonthiscontainentanewnationconceivedinzLibertyanddedicatedtothepropositionthatallmenarecreatedequalNowweareengagedinagreahtcivilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth" def find_longest_palindrome s1, size longest = "" s1.size.times do |start| break if start + size > s1.size s2 = s1[start, size].reverse if s1.include? s2 return s2 end end find_longest_palindrome s1, size - 1 end puts find_longest_palindrome(text, text.length)

I found the site via this post, 10 puzzle websites to sharpen your programming skills. Now that the wedding’s over (pics soon!) I can’t wait to get back to a regular extracurricular programming schedule!

# The Greplin Programming Challenge

My fiancee’s gone to bed, so I’m bragging to you dear internet.

I just completed The Greplin Programming Challenge! The problems were similar to some of the Project Euler problems I’ve done, but it was still a lot of fun. I can’t wait for the next one!

The End

# Unit Testing in PowerShell with PSUnit

I did a bit of playing around with wcf, PowerShell and PSUnit the other day.

PSUnit is a unit-testing framework based on NUnit (or should I say JUnit). It was a bit of work to set up but I was really impressed by the speed, robustness, and ISE integration.

It doesn’t make much sense to test application logic through a web service, or even with PowerShell at all, but it’s nice to know there’s such a great tool available. I’ve been having some issues with MSTest and 64 bit assemblies and although I’ll probably end up going with NUnit, it was a fun and educational journey!

Thanks PSUnit Guys!

# Ouch!

I had a minor bicycle malfunction that turned into an accident on the Cross Seminole Trail.

To pun it shortly: I fought the ground, and the ground won!