pascals-triangle

How to efficiently calculate a row in pascal's triangle?

隐身守侯 提交于 2019-11-28 03:56:44
I'm interested in finding the nth row of pascal triangle (not a specific element but the whole row itself). What would be the most efficient way to do it? I thought about the conventional way to construct the triangle by summing up the corresponding elements in the row above which would take: 1 + 2 + .. + n = O(n^2) Another way could be using the combination formula of a specific element: c(n, k) = n! / (k!(n-k)!) for each element in the row which I guess would take more time the the former method depending on the way to calculate the combination. Any ideas? >>> def pascal(n): ... line = [1] .

Tail-recursive Pascal triangle in Scheme

落爺英雄遲暮 提交于 2019-11-27 07:10:52
问题 I started to read SICP recently, and I'm very interested in converting a recursive procedure into a tail-recursive form. For "one dimensional" situations (linear ones), like the Fibonacci series or factorial computation, it is not hard to do the conversion. For example, as the book says, we can rewrite the Fibonacci computation as follows (define (fib n) (fib-iter 1 0 n)) (define (fib-iter a b count) (if (= count 0) b (fib-iter (+ a b) a (- count 1)))) And this form is obviously tail

Pascal's triangle 2d array - formatting printed output

馋奶兔 提交于 2019-11-27 06:31:57
问题 I have a small assignment where I have to use a 2d array to produce Pascal's triangle. Here is my code, and it works. there is an extra credit opportunity if I display the triangle like so: however, my spacing is not formatted like that. it simply displays the numbers all lined up on the left. its hard to describe but if you run it you will see what I mean. here is my code: import java.util.*; public class Pascal { public static final int ROW = 16; public static void main(String[] args) { int

Code-golf: generate pascal's triangle

你说的曾经没有我的故事 提交于 2019-11-27 05:21:41
问题 Locked . This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions. Generate a list of lists (or print, I don't mind) a Pascal's Triangle of size N with the least lines of code possible! Here goes my attempt (118 characters in python 2.6 using a trick): c,z,k=locals,[0],'_[1]' p=lambda n:[len(c()[k])and map(sum,zip(z+c()[k][-1],c()[k][-1]+z))or[1]for _ in range(n)] Explanation: the

How to efficiently calculate a row in pascal's triangle?

喜你入骨 提交于 2019-11-27 05:14:57
问题 I'm interested in finding the nth row of pascal triangle (not a specific element but the whole row itself). What would be the most efficient way to do it? I thought about the conventional way to construct the triangle by summing up the corresponding elements in the row above which would take: 1 + 2 + .. + n = O(n^2) Another way could be using the combination formula of a specific element: c(n, k) = n! / (k!(n-k)!) for each element in the row which I guess would take more time the the former

Pascal's Triangle for Python

天大地大妈咪最大 提交于 2019-11-26 22:58:37
问题 As a learning experience for Python, I am trying to code my own version of Pascal's triangle. It took me a few hours (as I am just starting), but I came out with this code: pascals_triangle = [] def blank_list_gen(x): while len(pascals_triangle) < x: pascals_triangle.append([0]) def pascals_tri_gen(rows): blank_list_gen(rows) for element in range(rows): count = 1 while count < rows - element: pascals_triangle[count + element].append(0) count += 1 for row in pascals_triangle: row.insert(0, 1)

Pascal's Triangle Format

♀尐吖头ヾ 提交于 2019-11-26 18:22:30
问题 The assignment is to create Pascal's Triangle without using arrays. I have the method that produces the values for the triangle below. The method accepts an integer for the maximum number of rows the user wants printed. public static void triangle(int maxRows) { int r, num; for (int i = 0; i <= maxRows; i++) { num = 1; r = i + 1; for (int col = 0; col <= i; col++) { if (col > 0) { num = num * (r - col) / col; } System.out.print(num + " "); } System.out.println(); } } I need to format the