Pascal's Triangle (Ruby)

From LiteratePrograms
Jump to: navigation, search

Category:Pascal's Triangle

[edit] Bruteforce

This simple implementation uses a sliding array to spit out the Pascal's triangle

<<Pascal_s_triangle.rb>>=
def pascal(n)
	p=[1]
	while(p.length<n)
		puts p.join(" ")
		p=Array.new(p.length+1) {|i| 
			a=i<p.length ? p[i] : 0
			b=i>0 ? p[i-1] : 0
			a+b}
	end
end
test main
Download code
hijacker
hijacker
hijacker
hijacker