`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 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:

## Tuesday, January 27, 2009

### Ruby FTP Scanner

I was recently given the task to recursively scan an ftp directory and generate a list of all the files in all the folders in that directory. Here's a little ruby script that I wrote to accomplish the scan.

Enjoy!

`require 'net/ftp'`

# The scanDir function searches through the directory that is passed in as an argument. If that directory has any sub directories,

# then the function calls itself to handle them.

def scanDir(ftp, dir)

ftp.chdir(dir)

puts ftp.pwd + "/."

entries = ftp.list('*')

entries.each do |entry|

if entry.split[2] == "<dir>" then

# If this is a directory then call scanDir to scan it.

scanDir(ftp, entry.split.last)

else

# else, print out the name of the file.

puts ftp.pwd + "/" + entry.split.last

end

end

# Since we dipped down a level to scan this directory, lets go back to the parent so we can scan the next directory.

ftp.chdir('..')

end

# Determine if the user is asking for help.

helpArgs = ["h", "-h", "/h", "?", "-?", "/?"]

if helpArgs.index(ARGV[0]) != nil || ARGV.length == 0 then

puts "FTPSCAN:"

puts " The ftpscan script recursively scans an ftp directory and returns a list of all the files and directories contained therein."

puts

puts " USAGE:"

puts " ruby ftpscan.rb [ftpserver] [username] [password]"

puts

puts " EXAMPLE:"

puts " ruby ftpscan.rb tsftp.turner.com steveo stevepass"

puts

else

# Get the command line arguments

server, user, pass = ARGV

# Connect to the FTP Server

ftp = Net::FTP.new(server)

# Login to the FTP account

ftp.login(user, pass)

# Recursively scan the directories.

scanDir(ftp, '')

# Close the FTP connection.

ftp.close

end

Enjoy!

## 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:

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

"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.

Subscribe to:
Posts (Atom)