Showing posts with label Ruby. Show all posts
Showing posts with label Ruby. Show all posts

Friday, February 06, 2009

Project Euler - Problem 3

Problem 3 reads as follows:

"The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?"


This one is easy, thanks to Ruby's mathn library and the prime_division method. You can read more about it and even see the source code for the method at the documentation site.

All we have to do is require the mathn library, which comes with Ruby by default. I checked on my Windows and Mac OSX machines. Then call the prime_division method on the integer. That creates a multidimensional array.

For each item in the array, there are 2 numbers. The first is the actual prime number and the second relates how many times that prime number can be factored out. So, for the number 9, you would end up with [3,2].

Lastly, we grab the last prime number in the array for the answer.

require 'mathn'
primeArray = 600851475143.prime_division
puts primeArray[primeArray.length - 1][0]

Friday, January 30, 2009

Project Euler - Problem 2

Alright, on to problem 2. "Find the sum of all the even-valued terms in the [Fibonacci] sequence which do not exceed four million." This one is pretty easy. Just loop through all the numbers in the Fibonacci sequence up to four million. The largest actual number in the sequence that does not exceed 4 million is 3,524,578. Here's is my humble attempt at a solution:

x1,x2 = 0, 1
ans = 0

# While x1 is less than or equal to 4 million
while(x1 <= 4000000)

# Find the next number in the Fibonacci sequence
x1+=x2
x1,x2= x2,x1

# If that number is even, then add it to the running sum.
if(x1%2 == 0) then ans += x1 end
end

puts ans

Friday, January 23, 2009

Project Euler - Problem 1

I found a great site called Project Euler. The site explains it best:

"Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.

The motivation for starting Project Euler, and its continuation, is to provide a platform for the inquiring mind to delve into unfamiliar areas and learn new concepts in a fun and recreational context."

I've been using Ruby as an alternative to PowerShell, since it runs on the miriad of systems that I am exposed to in my current position. I figured that this would be a good way to increase my Ruby coding skills and have a little fun solving some puzzles at the same time. From time to time, I'll post my solution to a problem. I'm still learning, so if you have suggestions, I would love to hear your constructive criticism.

Problem 1:
Find the sum of all the multiples of 3 or 5 below 1000.

Solution:

ans=0

(1...1000).each{ |i|
  if( i%3 == 0 || i%5 == 0 ) then
    ans += i
  end
}

puts ans


I doesn't seem very "Ruby-ish" but it works.