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.

Read on →

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.

Read on →

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.

Read on →

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.

1
2
3
4
5
6
7
8
9
10
SieveOfE <- function(n) {
 primes <- rep(TRUE, n)
 primes[1] <- FALSE
 last.prime <- 2
 while (last.prime <= sqrt(n)) {
   primes[seq.int(2*last.prime, n, last.prime)] <- FALSE
   last.prime <- last.prime + min(which(primes[(last.prime + 1) : n]))
 }
 which(primes)
}
Read on →

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:

1
2
nat <- seq(1:100)
sum(nat)^2 - sum(nat^2)
Read on →

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.

Read on →

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:

Read on →

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:

1
2
3
4
5
library(faraway)
library(ggplot2)
library(gridExtra)
data(fat)
attach(fat)
Read on →

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.

Read on →

For this problem I had to find the largest prime factor of the number “600851475143”.

First I defined a function called “is.prime” to determine whether your input (is.prime(x)) is a prime number. To define this function, I used a for loop to test if the number is divisible by any number between 2 and the square root of the number. This follows from the fact that if you have two factors that multiply to a number “x”, only one can be greater than the square root of “x”.

If “x” is divisible by any number from 2 to sqrt(x) then the is.prime function will return 0. Otherwise, it will return 1 if “x” is prime. I had to add in a special case for x = 2 since 2 is divisible by 2 but it is still prime (since 2 is the only factor not equal to 1).

I then tested to see which values between 1 and sqrt(600851475143) were prime using a for loop and placed the 1 or 0 (corresponding to the is.prime function output) into a numeric vector named primes.

Read on →