Showing posts with label 2. Show all posts
Showing posts with label 2. Show all posts

Thursday, March 1, 2012

Day 2 of the Seattle Expedition

Day 2 of the Seattle Expedition was much better for several reasons:

First, I didn't have to wake up at 4:30 in the morning to go catch a bus down to Utah: definitely an improvement.  

Second, we got to visit some much cooler companies:  Nordstrom, Fresh Consulting, and Microsoft to name the ones that most impressed me.  Each had their own culture and mentality that was hard to miss, and each were cordial and a pleasure to work with in turn.  I definitely felt like the best fit for me was either at Microsoft or Fresh Consulting; Microsoft having a very competitive and intelligent nature, while Fresh Consulting and terrific work ethic and an innovative and entrepreneurial spirit.  

Mike from Fresh Consulting was very transparent, and in everything he did he sought to instill in us the attributes and the lessons that helped him in his path to success.  He was very transparent, trusting of us, and humble.  He started his little talk by telling a 10-minute version of his life and the most important moments therein:  The passing of his first wife to cancer, and the joy and renewal he found in marrying and bearing more children.  I appreciated his speaking with us and being so clearly moved by pictures he showed us, without making us uncomfortable because it was a lesson in transparency:  be the first to trust and to put your feelings and emotions on the line.  He spoke mostly of the movement in social media and how important it is to become an active participant in that realm through producing and interacting with others.  He told many stories of doors opening to him because he reached out.  The principle he emphasized the most was to be exactly who you are online as you are in real life, because people want to connect with people.

He also mentioned a book:  Never Eat Alone, so Justin and I tried to do that tonight.  (Sushi, yum!)


He was a pleasure to listen to, and I felt like it was a good reminder to write in my blog.  :-)

Thursday, August 11, 2011

Project Euler #2 in Ruby

Project Euler #2

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

Here is my solution:

  1. one = 1
  2. two = 1
  3. sum = 0
  4. while (two <= 4_000_000)
  5.   temp = one
  6.   one = two
  7.   two = one + temp
  8.   if (two % 2 == 0)
  9.     sum += two
  10.   end
  11. end
  12. puts sum

You will notice the '_' symbol where the commas would be (or periods if you don't live in the States), for reference in keeping track of how many zeroes we have if we were writing '4,000,000' out by hand.  Coding up '4_000_000', is basically the same, the compiler will ignore the underscores.  It is just so we can keep track of how many zeroes there are. 

I wanted to illustrate something handy in this tutorial, called 'parallel assignment'.  Nothing earth shattering, but it could cut down on the number of times you have to press the enter and equals key.

Ruby allows you to assign variables to values, lined up one after the other, on a single line of code.  If you will notice we combined lines 1 through 3 above into a single line (1) below.  The same is true for lines 5 and 6 above into a single line 3 below.  Line 4 below is still on its own line as Ruby won't let you do arithmetic while doing parallel assignment. 

  1. one, two, sum = 1, 1, 0
  2. while (two <= 4_000_000)
  3.   temp, one = one, two
  4.   two = one + temp
  5.   if (two % 2 == 0)
  6.     sum += two
  7.   end
  8. end
  9. puts sum

This isn't the most clever way of solving this problem, but certainly the easiest to understand.  We simply do what the question asks for:  Add up each even term of the Fibonacci sequence below 4,000,000.