JOEZACK.COM Code Musings and Such

2Jul/110

Project Euler : Problem 39 in Ruby

I had an easy time with this one, which makes me feel a lot better about all the ones I had problems with!

No fancy-pants recursion or math short-cuts here, just a straight forward logic problem. The only "trick" here is to realize that since a <e; b < c, we only need to check values of a and b up to 499.

I'm sure you could whittle that number down by crunching the numbers, but it's good enough for me!

Problem 39

For which value of p 1000, is the number of solutions maximised?

counts = {}
counts.default = 0

(1..499).each do |a|
  (a..499).each do |b|
    break if a + b > 500
    c = Math.sqrt(a**2 + b**2)
    next unless c.denominator == 1
    counts[a + b + c] += 1
  end
end

sorted = counts.sort { |a, b| a[1] <=> b[1] }

puts sorted.last[0]
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
29Jun/110

The Best Programming Music

I listen to a lot of music at work. All day, every day.

It helps smooth frustrations, keeps me awake, and provides "color" for my day. I want my office to feel like a 1960's Sci-Fi movie!

Most importantly, the music helps me "flow" by drowning out the regular office clatter and bang-clangery.

and I used to be over by the window, and I could see the squirrels

I used to be over by the window, and I could see the squirrels...

Admittedly, I tend to "burn out" early on music, but 2,000+ hours of work/year will eventually tire even the most stalwart listener and extensive collection. Especially since a lot of the music I like "in real life" doesn't mesh well with concentrating. I usually find lyrics to be too distracting.

Since audiobooks and podcasts have taken over my ipod, I do almost all of my music listening at work. This has introduced a strange and large gap between the music that I used to like and the music I actually listen to.

Although I love it, my office is a no Kanye zone!

My office is a No Kanye Zone

My office is a No Kanye Zone

Since I require so much "background" music, services like and Pandora are perfect fit for me. I'm constantly tweaking my stations, but here's my current fave for work:

My favorite Pandora Station:
Programming Music: Boards of Mercury Circles

And here's a few youtubes of songs you might hear on that station:







Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
27Jun/110

Project Euler : Problem 38 in Ruby

40 problems down, 10 more till level 2!

The real trick here is to cut down on the numbers you check. Since the problem gives you 918273645 as an example we know the answer must be greater than or equal to it...meaning we only need to check digits that start with 9!

I don't actually do it because I couldn't figure out an elegant way to do, but it runs in just a few milliseconds so it's fast enough in my book.

Another important factor to note is that you only need to check numbers up to 9_876 since this is the first 'half' of the largest pandigital possible.

Those two tricks will cut down the calculations you need to run to just a few thousand.

Problem 38

What is the largest 1 to 9 pandigital 9-digit number that can be formed as the concatenated product of an integer with (1,2, ... , n) where n 1?

def get_pandigital? n
  nums = []
  (1..9).each do |digit|
    nums += (n * digit).to_s.split ''
    return 0 if nums.size != nums.uniq.size || nums.include?('0')
    return nums.join('').to_i if nums.size == 9
  end
end

solution = 0

(9..9_876).each do |n|
  # could do better by only looking at 9's!
  result = get_pandigital? n
  if result > solution
    solution = result
  end
end

puts solution

Check out all of my solutions on bitbucket!

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
17May/110

Project Euler: Problem 37 in Ruby

It's been a while since I've done one of these, so I was afraid of being rusty but it worked out alright. I used the generator I made a while back to create the primes (pre-filled to the example given in the project) and used procs to take care of the truncation.

BAM!

Problem 37

The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3.

Find the sum of the only eleven primes that are both truncatable from left to right and right to left.

load 'prime_generator.rb'

def prime? n, truncate
  return false if !$primer.is_prime?(n)
  return true if n < 10
  prime? truncate.call(n), truncate
end

left = Proc.new { |n| n / 10 }
right = Proc.new { |n| n % 10**Math.log10(n).to_i }

$primer = Prime_Generator.new 3_797
n, sum, found = 0, 0, 0

while found < 11 do
  if (n += 1) > 10 && prime?(n, left) && prime?(n, right)
    found += 1
    sum += n
  end
end

puts sum

Also, I've finally moved my project euler solutions over to bitbucket. Long live Mercurial!

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
16Apr/110

Audio Automata with Otomata

I don't normally like to blog links, Twitter and Google Reader are much better platforms for that, however this was so cool I just had to shout it out.

Read more about it, and check it out yourself: Otomato

The rules for the automation are simple, but the hypnotic patterns that emerge are truly beautiful and interesting. The user interface is both easy and fun to use, and it does a great job of showing the simple beauty of what you're listening to.

The music reminds me a lot of one of my favorite bands Boards of Canada, so if like the sound of this then check them out, it makes for great programming music.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)