[OLD QUIZ] Pascal's Triangle

A

Ari Brown

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

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
puts holder.join(' ').center(80)

}

end

puts 'How many rows do you want?'
max_row = gets.chomp.to_i
pascal max_row


Thanks,
---------------------------------------------------------------|
~Ari
"I don't suffer from insanity. I enjoy every minute of it" --1337est
man alive
 
J

James Edward Gray II

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
 
S

Stefan Rusterholz

Ari said:
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?

Most of it has already been said, anyway, here's my take based on your
approach:

class Integer
def factorial
(1..self).inject(1) { |fac, i| fac*i } # as James already stated
end

def cr(r)
factorial/(r.factorial*(self-r).factorial)
end
end

# IMHO number of rows is more intuitive and leaves less questions open
(like: does it start with 0 or 1?)
def pascal(rows)
rows.times do |row_num|
holder = []
row_num.times do |ticker|
holder << row_num.cr(ticker)
end
holder # the formatting stuff etc. IMHO does not belong into this
function
end

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

Regards
Stefan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top