Tad Thorley

creative commons license rss feed comments feed powered by Howex valid xhtml Add to Technorati Favorites

Free Ruby on Rails E-Book

Sitepoint is giving away free PDF copies of "Build Your Own Ruby on Rails Web Applications" by of Patrick Lenz for the next 60 days. I downloaded it this morning and it looks like a pretty solid book and a good introduction to rails.

Approximate Pi With Random Numbers

random exampleI came across some old computer science notes about using random numbers to calculate π and I thought it would be fun to code it up really quick in Ruby. First, however, I think it needs a bit of explanation. Let's start with a square with sides of length 1. Next, let's circumscribe a quarter circle within that square (making the radius of the circle 1). Let's also assume that the bottom left corner of the square is at the origin (0, 0). (I've included a--very simple--diagram in the way of illustration). We know that the area of the square is r2 or 12 or 1. We know that the area of the quarter circle is πr2/4 or π/4. (Notice that the r has dropped out.)

Now for the second part. Let's take a random number generator that generates random numbers between 0 and 1 (not so hard in computer science). A pair of them (xr, yr) will fall somewhere within our square. Using the distance formula, we can see if the random pair also falls within the circle (the square root of x2 + y2). We choose some total number of random coordinates and see how many fall within the circle compared to the total number (which all fall within the square). Using the ratio of the number within the square to the number within the circle we can take a guess at the value of π.

Here's the code:

1
2
3
4
5
print "Number of iterations: "
iterations = gets.chomp.to_i
count = 0
iterations.times { count += 1 if (rand**2 + rand**2)**0.5 < 1 }
puts 4.0*count.to_f/iterations.to_f

The more iterations that you do, the closer to π you get.

Prime for Ruby

I came across this algorithm for determining if a number is prime and I thought it might be fun to implement in Ruby. Some objectives were to stay true to the original algorithm, do it in less code and keep it just as readable. Here's my result:

1
2
3
4
5
6
7
8
9
10
11
class Fixnum
  def prime?
    (2..self.abs**0.5).each {|n| return false if self%n == 0}
    return true
  end
end

2.prime?   # => true
5.prime?   # => true
12.prime?  # => false
101.prime? # => true

I figured this is something that I could add to the Fixnum class so I could just ask a number if it is prime or not (in true OO fashion). I check the range from 2 to the square root of the number. If it is divisible it isn't a prime, if not then it is.

Of course, there is a Prime class for Ruby, so in many cases this would be unnecessary.

My first rbot plugin

Here's a plugin for rbot that queries google's calculator.
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
require 'rubygems'
require 'hpricot'
require 'open-uri'

class GcalcPlugin < Plugin

  def help(plugin, topic="")
    "gcalc <statement> => do some conversions/calculations using Google's calculator"
  end

  def retrieve(m)
    request = m.params.gsub(" ", "+")
    doc = Hpricot(open("http://google.com/search?q=#{request}"))
    response = (doc/"h2.r").first.to_plain_text.gsub(/\[|\]/, "")
    m.reply response
  end

  def privmsg(m)
    request = m.params.gsub(" ", "+")
    doc = Hpricot(open("http://google.com/search?q=#{request}"))
    response = (doc/"h2.r").first.to_plain_text.gsub(/\[|\]/, "")
    m.reply response
  end 
end

plugin = GcalcPlugin.new
plugin.map "gcalc *request", :action => 'retrieve'

Let's try this again

I finally got the bad hardware in my desktop computer replaced. Now I have the means to write articles reliably again.

I have also switched my blogging software from Wordpress to Mephisto. Mephisto doesn’t have all of the plugins and themes that a more popular blog like Wordpress has, but it does the things I need it to do and want it to do. It doesn’t try to reformat my posts and works with me instead of against me. It is also written in Ruby so I can see the interesting tricks that they’ve done with it and extend it myself.