A
Ari Brown
What's that? You want me to ask more questions? Sure!
So I'm writing a pascals triangle program. My goal is to write it in
fewer lines than those on the RubyQuiz site. My idea is to use more
math, and less programming.
So in order for my method to work, I sorta need to recreate nPr and
nCr functions. To be official and learn more, I am doing this through
a class. However, every time I run it, I get the following errors:
pascal.rb:29:in `pascal': undefined method `nCr' for 1:Fixnum
(NoMethodError)
from pascal.rb:24:in `upto'
from pascal.rb:24:in `pascal'
from pascal.rb:41
My code:
# Pascals triangle
class Integer
def self.nPr(r)
numerator = factorial(self)
denominator = factorial(self - r)
@permutations = numerator / denominator
end
def self.nCr(r)
numerator = factorial(self)
denominator = factorial(r) * factorial(self - r)
@combinations = numberator / denominator
end
end
def pascal max_row
0.upto(max_row) {|row_num|
holder = []
ticker = 0
while ticker != row_num
result = row_num.nCr(ticker)
ticker = ticker + 1
holder = result.push
end
puts holder.join(' ').center(80)
}
end
puts 'How many rows do you want?'
max_row = gets.chomp.to_i
pascal max_row
Any help?
Thanks,
Ari
--------------------------------------------|
If you're not living on the edge,
then you're just wasting space.
So I'm writing a pascals triangle program. My goal is to write it in
fewer lines than those on the RubyQuiz site. My idea is to use more
math, and less programming.
So in order for my method to work, I sorta need to recreate nPr and
nCr functions. To be official and learn more, I am doing this through
a class. However, every time I run it, I get the following errors:
pascal.rb:29:in `pascal': undefined method `nCr' for 1:Fixnum
(NoMethodError)
from pascal.rb:24:in `upto'
from pascal.rb:24:in `pascal'
from pascal.rb:41
My code:
# Pascals triangle
class Integer
def self.nPr(r)
numerator = factorial(self)
denominator = factorial(self - r)
@permutations = numerator / denominator
end
def self.nCr(r)
numerator = factorial(self)
denominator = factorial(r) * factorial(self - r)
@combinations = numberator / denominator
end
end
def pascal max_row
0.upto(max_row) {|row_num|
holder = []
ticker = 0
while ticker != row_num
result = row_num.nCr(ticker)
ticker = ticker + 1
holder = result.push
end
puts holder.join(' ').center(80)
}
end
puts 'How many rows do you want?'
max_row = gets.chomp.to_i
pascal max_row
Any help?
Thanks,
Ari
--------------------------------------------|
If you're not living on the edge,
then you're just wasting space.