On Jun 2, 2007, at 9:44 AM, Ari Brown wrote:

> Hey all,
> my second submission to RQ. My first one is GOING TO BE fizzbuzz.  
> Now now, now's not the time to battle my inconsistencies...
>
> Here's my code for Pascals Triangle. Can someone help me shorten it  
> up?
>
> # Pascals triangle
>
> def factorial(n)
>   sum = 1
>   sum.upto(n) { |i| sum *= i }
>   sum
> end

(1..n).inject(1) { |fact, i| fact * i }

> def nCr(n, r)
>   numerator   = factorial(n)
>   denominator = factorial(r) * factorial(n - r)
>
>   @combinations = numerator / denominator
> end
>
> def pascal max_row
>
>   0.upto(max_row) {|row_num|
>
>     holder = []
>     ticker = 0
>     while ticker != row_num
>       result = nCr(row_num, ticker)
>       ticker = ticker + 1
>       holder.push(result)
>     end

That inner loop could be:

(0...row_num).each do |ticker|
   # ...
end

>     puts holder.join(' ').center(80)
>
>   }
>
> end
>
> puts 'How many rows do you want?'
> max_row = gets.chomp.to_i
> pascal max_row

James Edward Gray II