Factorial

Write a method that takes an integer ‘n’ in; it should return n*(n-1)*(n-2)*…*2*1. Assume n >= 0.

As a special case, ‘factorial(0) == 1’.

Provided solution:

Screen Shot 2016-08-17 at 11.03.24 PM

My solution:

screen-shot-2016-09-06-at-9-57-31-pm

Turned out I didn’t actually need the elsif statement as result would default back to 1 (since it was initialized as result = 1) when n is not > 0.

Also, note to self: only use multiple ‘if’ statements if you want more than 1 condition to be true. Otherwise, use elsif to ensure that only one condition is executed.

Advertisements
Factorial

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