# Visualizing Average Rainfall in the US by State

The inspiration for this post came to me while randomly browsing data sets on the internet. I found this page and decided to practice using ggplot2 to make heat maps. First, I had to transfer the data from the table on the website into a .csv file in Excel so I could properly import the data into R.

The code itself is very short which shows just how powerful and efficient ggplot2 really is.

# Project Euler in R Problem 7

Problem 7 of Project Euler asks us to find the 10001st prime number.

The ‘is.prime’ function is the same one that I use in Problem 3. I start the ‘repeat’ loop at 3 so I have to manually set the starting count at 1 (since 2 is prime). I start at 3 because then I can have ‘i’ go up by 2 every iteration (since every prime after 2 is odd). The ‘repeat’ loop then goes through all odd numbers and if they are prime (as determined by the is.prime function I wrote) adds 1 to the count. When the count reaches 10001, the repeat loop breaks and prints the 10001st prime.

# Visualizing the Magnitude of Earthquakes in New Zealand

I wanted to test out a few packages in R and explore some data visualization techniques so I decided to write this script. The power and versatility of ggplot2 continually amazes me.

“datasets” gives me access to the ‘quakes’ dataset which gives the locations and magnitudes of 1000 seismic events near Fiji. Since the ‘maps’ package does not include a map of Fiji, I used a map of New Zealand to display the locations.

I called a map of New Zealand and then plotted the latitudes and longitudes of the seismic events. The ‘maps’ package is very easy to use and has plenty of helpful documentation.

# Sieve of Eratosthenes

Today I’ll be discussing a way to find all primes up to a certain number using the Sieve of Eratosthenes. The algorithm is useful for many Project Euler problems and it was an interesting challenge to code in R.

# Project Euler in R Problem 6

This is a short one which I suppose is good since I should be doing homework anyway!

Problem 6 of Project Euler asks us to find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

I took advantage of R’s element-wise vector multiplication to produce a nice 2-line script:

# Project Euler in R Problem 5

The goal of this problem was to find the smallest positive number which is evenly divisible by all of the numbers from 1 to 20. In other words, we are looking for the least common multiple of the integers 1 through 20.

I considered a few circuitous routes before I found an efficient method. First I defined a function named “gcd” to find the greatest common divisor of two integers. I simply employed Euclid’s Algorithm for finding GCDs to construct this relatively simple function. I then wrote another short function named “lcm” which finds the least common multiple of two integers by multiplying them and dividing the product by the greatest common divisor of the two (nothing fancy here, I used the basic definition of LCM).

Finally, I created a numeric vector and set the first entry to be 1 (since the least common multiple of 1 and 1 is 1). For spots 2 through 20 I ran a for loop to calculate successive LCMs. For example, the second slot of the “ans” vector was the LCM(1,2), the third slot was LCM(LCM(1,2),3) and so on.

# Taxicab Numbers

I got a little distracted halfway through my number theory homework so I decided to write a post. At least the topic is pertinent to number theory, right?

I first came across taxicab numbers a few years ago while jumping between Wikipedia math articles. Last summer, I was watching the play “Proof” (which was amazing by the way, if you’re ever in New York while its playing then I recommend you watch it) with a friend of mine and the main character mentioned that “1729” is a special number. As soon as I heard that, I felt a memory trying to make its way out from the back of my brain. After a few seconds I finally remembered where I’d seen it and I started excitedly whispering to my friend, frantically trying to explain why the number was special (she still makes fun of how worked-up I got).

For those that are unfamiliar, 1729 is known as the Hardy-Ramanujan number after this anecdote:

# Simple Linear Regression and Analysis

I’ve been trying to get more accustomed with using Hadley Wickham’s ggplot2 so today I’ll be going over simple linear regression and some easy ways to analyze the models you’ve created. ggplot2 is a phenomenal package for data visualization and I am actually lucky enough to have taken a class with Hadley at Rice University.

First we’ll load some packages and attach the “fat” dataset:

# Project Euler in R Problem 4

The goal of this problem was to find the largest palindrome made from the product of two 3-digit numbers. I had a lot of fun with this one and the string splitting practice was very useful for future problems.

First, I created a function named is.pal which takes a number, converts it to a character, and splits it by digit into a vector. It then tests whether the vector is equivalent forward and reverse to determine if the original input “x” is a palindrome.

I then ran a double for loop multiplying each 3-digit number by every other one and testing whether the result was palindromic. If it was, the loop then tested whether the product was larger than the previous max palindrome product and updated the “max” variable accordingly.