Solving problems with Ruby

I’m just trying to write few words to stay disciplined before I go to sleep. It’s 0.30am and happy Friday has just started. For the past few days, I have finished Ruby course on Codacademy and went through 9 chapters of Chris Pine book “Learn to Program”. I have also managed to solve first four problems on Project Euler. On top of that, I am still working through some lessons on Viking Code School. The first goal is very near. I hope to accomplish the last block – Coding from Prep Work by Sunday and then finally send my application to VCS. I am very motivated, I have just realized it has been over 50 days/over 120 hours since I started preparing for this course and changing my career.

Codacademy course equipped me with a lot of knowledge. Unfortunately, it is a type of knowledge which was slowly fading due to lack of proper practice (as this course wasn’t too challenging). Thankfully Chris’s book comes as needed addition. I am just finishing chapter 9 now. Most of the chapters ask to program solutions to problems although I solved all of them so far, it’s taking more and more time every next chapter to answer them. Solving some interesting problems le me experience how powerful computing is. How much time would take the brightest mind to find a number of seconds I live on Earth. Apparently, at these very seconds, I’m typing these words in, I am living exactly my  1051710466 second (from chapter 12, page 96-97).

Project Euler, on the other hand, came as the very challenging tour, of which one I wasn’t able to finish due to long time computer was processing all the iterations. I’m talking about Problem no 3: 

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

What is the largest prime factor of the number 600851475143 ?

And here is my answer:

num=gets.chomp.to_i
array=[]
1.upto(num) do |x|
if x%2!=0 || x%3!=0 # we only finding prime numbers here
if num%x == 0        # we check if those primes are dividing our number
array.push(x)
num=num/x
end
end
end

puts ‘—-‘
puts array
puts array.max

I assume it may take about 2 hours for my Mac to process this problem, but after checking some of the factors, it is working.

That’s it for now. I hope to write my next post from next point on this road.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s