pascals-triangle

How can I calculate the number at a given row and column in Pascal's Triangle?

丶灬走出姿态 提交于 2019-12-03 22:01:18
I'm trying to create a function that, given a row and column, will calculate the value at that position in Pascal's Triangle. Example: val = GetPasVal(3, 2); // returns 2 So here I'm specifying row 3, column 2, which as you can see: 1 1 1 1 2 1 ...should be a 2. The Pascal's triangle contains the Binomial Coefficients C(n,k); There is a very convenient recursive formula C(n, k) = C(n-1, k-1) + C(n-1, k) You can use this formula to calculate the Binomial coefficients. Using Armen's equation the recursive code for implementing pascals triangle will be like below: using System; using System

“application: not a procedure” while computing binomial

天大地大妈咪最大 提交于 2019-12-02 15:03:33
问题 I am defining a function binomial(n k) (aka Pascal's triangle) but am getting an error: application: not a procedure; expected a procedure that can be applied to arguments given: 1 arguments...: 2 I don't understand the error because I thought this defined my function: (define (binomial n k) (cond ((or (= n 0) (= n k)) 1) (else (+ (binomial(n) (- k 1))(binomial(- n 1) (- k 1)))))) 回答1: In Scheme (and Lisps in general), parentheses are placed before a procedure application and after the final

“application: not a procedure” while computing binomial

ⅰ亾dé卋堺 提交于 2019-12-02 10:06:43
I am defining a function binomial(n k) (aka Pascal's triangle) but am getting an error: application: not a procedure; expected a procedure that can be applied to arguments given: 1 arguments...: 2 I don't understand the error because I thought this defined my function: (define (binomial n k) (cond ((or (= n 0) (= n k)) 1) (else (+ (binomial(n) (- k 1))(binomial(- n 1) (- k 1)))))) In Scheme (and Lisps in general), parentheses are placed before a procedure application and after the final argument to the procedure. You've done this correctly in, e.g., (= n 0) (= n k) (- k 1) (binomial(- n 1) (-

Memoization pascals triangle

 ̄綄美尐妖づ 提交于 2019-12-02 03:46:51
I'm not interested in the actual solution or other methods solving the problem, it's the memoization i need help with :) I need help doing a pascals triangle problem with memoization. I want to get the middle number in the base of the triangle. (Project Euler 15) The first example is not memoized (although the name suggests so) "20 20" not solvable Second attempt is an attempt on doing something similar to: http://www.haskell.org/haskellwiki/Memoization Third is hlints suggestion on no2 if that is more readable for someone. I get this error, but i'm not sure its right even if it would compile.

Pascal Triangle in Haskell

大兔子大兔子 提交于 2019-11-30 17:46:19
问题 I'm new to Haskell and I really need some help! I have to write a program that includes a recursive function to produce a list of binomial coefficients for the power n=12 using the Pascal's triangle technique. I have some ideas in my head but because I'm just getting started I have no idea how to implement this to haskell?! Could someone please help me out?? first row: (a+b)^0 = 1 second row: (a+b)^1 = 1a+1b third row: (a+b)^2 = 1a^2+2ab+1b^2 and so on...this is my main idea. But I cant even

Convert normal recursion to tail recursion

此生再无相见时 提交于 2019-11-28 17:58:35
I was wondering if there is some general method to convert a "normal" recursion with foo(...) + foo(...) as the last call to a tail-recursion. For example (scala): def pascal(c: Int, r: Int): Int = { if (c == 0 || c == r) 1 else pascal(c - 1, r - 1) + pascal(c, r - 1) } A general solution for functional languages to convert recursive function to a tail-call equivalent: A simple way is to wrap the non tail-recursive function in the Trampoline monad. def pascalM(c: Int, r: Int): Trampoline[Int] = { if (c == 0 || c == r) Trampoline.done(1) else for { a <- Trampoline.suspend(pascal(c - 1, r - 1))

Tail-recursive Pascal triangle in Scheme

丶灬走出姿态 提交于 2019-11-28 12:25:31
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 recursive However, for a "two dimensional" situation, like calculating Pascal's triangle (Ex 1.12 in SICP), we

Pascal's triangle 2d array - formatting printed output

时间秒杀一切 提交于 2019-11-28 11:43:19
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[][] pascal = new int[ROW +1][]; pascal[1] = new int[1 + 2]; pascal[1][1] = 1; for (int i = 2; i <= ROW

Pascal's Triangle in C

試著忘記壹切 提交于 2019-11-28 11:01:31
问题 I'm a computer engineering student and next semester I am going to start C course. So in order to prepare myself a bit, I have started learning C by myself and stumbled across an interesting task, designed for, how it seemed to me at first sight, not a very advanced level. The task is to write a program to compute the value of a given position in Pascal's Triangle . And the formula given to compute it is written as element = row! / ( position! * (row - position)! ) I've written a simple

Code-golf: generate pascal's triangle

杀马特。学长 韩版系。学妹 提交于 2019-11-28 04:30:10
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 first element of the list comprehension (when the length is 0) is [1] the next elements are obtained the following way: take the previous list and make two lists, one padded with a 0 at the beginning and the other at the end. e.g. for the 2nd step, we take [1] and make [0,1] and [1