Sieve of Eratosthenes

The Sieve of Eratosthenes is often one of the first algorithms taught in
computer science books. This simple algorithm showcases the ability
to calculate primes numbers through the power of a computer without learning
much of the nuts and bolts of a programming language. Finding the
first 100 prime numbers can be done in a fraction of a second, and the
first 1000 prime numbers performed in a split second. It definitely proves to have a
computer around to aid in computation!

From Wikipedia, here is an animation of how the algorithm works:

Animation showing the Sieve of Eratosthenes

The above animation uses the Sieve of Eratosthenes to find all prime
numbers up to 120.

The number 2 is chosen first as the starting point and first prime. Then, all multiple values of 2
are crossed off (in red) from being prime numbers. Next, number 3 is chosen as a
prime number, and all multiple values of 3 (in green) are crossed off from being
possible prime numbers. Since the number 4 is already marked off as red from being
a multiple of 2, the number 5 is chosen as the next starting point and saved as a prime. This continues
until all positions are either chosen as prime or crossed off.

If n is the value that we want to check prime numbers until, then we will ever cross off
multiples up to sqrt(n), because all multiples of sqrt(n) above sqrt(n) are larger than n.
(This is because we would only ever multiple sqrt(n) by values larger than sort(n) by the
time we get there, since values smaller than sqrt(n) have been crossed off).

The mod operation comes to mind when checking for prime numbers.
Here is the Sieve of Eratosthenes using the mod operation:

And here it is without using mod!

Gruff – Ruby Plotting Library

I’ve been looking around to find a Ruby plotting library in order
to play with some data. The scientific community has gathered more
around Python, and it seems like Ruby has a ways to go before catching up.
It took some digging, but I’ve settled on Gruff, which seems
to be a well-maintined plotting tool.

I’m also starting to look at the Introduction to Statistical Learning course offered online
by Stanford. I think the textbook and lectures are excellent, though
admittedly I’ve been following the textbook more closely than the lectures.

My goal is to do everything in Ruby, since I like Ruby so much!

There is a free online textbook for the course, and all the data is available
on the textbook’s website.

I like this textbook, because in addition to being pedagogical, the authors chose R
as the language to showcase statistical techniques.

To start things off, I used gruff to graph one of the first datasets, Advertising.csv.

Here is the graph (click for larger version):

Advertising scatter plot

Advertising scatter plot

And the code to produce the graph!

RSpec Test for POSTing a JSON file and Obtaining Correct Response

I wasn’t able to find my exact problem from a Google search,
so I’m posting this here in case it helps someone.

I’d like to create an RSpec test that posts a JSON file to
a Rails API controller, and ensure that the correct
output exists.

The particular use case for me is that my JSON response
must always be the same if the same JSON request is posted.

Here is the code to do it:

The squish method takes care of whitespaces and newlines.
The eql? method tests for equality of the value of an object.

Have a better way or would like to criticize? Please let me know in the comments!

Using Gist with WordPress on DreamHost

DreamHost makes it incredible easy to fire up a WordPress app on your domain.

DreamHost has “one-click installs,” which include several open-source tools for a website – including WordPress. It’s free if one has the fully-hosted plan.

One just adds the WordPress app. Once the WordPress app is installed, syntax highlighting can be performed through embedding Gists. It seems to be a simple option for introducing syntax highlighting in code.

I’m using oEmbed Gist, which allows you to paste the URL on it’s own line in the WordPress editor:

https://gist.github.com/8589700

…and, voilĂ !

The code is included, syntax highlighting and all!