Pascal Triangle String Sequence

I am currently working on finding sequences of strings from a Pascal triangle. I wanted to enter a line number and print a sequence of numbers in the list before that line. For example, it (Pascal 4)will give a result (1 1 1 1 2 1 1 3 3 1).

I am trying to use the algorithm I found. Here is the algorithm itself:

V c = V c-1 * ((rc) / c)

r and c must be rows and columns, and V 0 = 1. The algorithm can be defined on the wikipedia page in the section "Calculation and an individual series or diagonal".

Here is the code that I still have:

(define pascal n)
  (cond((zero? n) '())
       ((positive? n) (* pascal (- n 1) (/ (- n c)c))))

I know that hardly anything, but I struggle a lot for trying to find a function with letor lambdato include column values. In addition, I also struggled with recursion. I do not know how to set up a basic example and how to proceed to the next step. Basically, I'm lost everywhere. I know that this does not show much, but any step in the right direction would be greatly appreciated.

+2
source share
2 answers

Using the Wikipedia entry as a reference , this is a simple implementation of the algorithm for calculating the value in Pascal's triangle, given its row and column, as described in the link:

#lang racket

(define (pascal row column)
  (define (aux r c)
    (if (zero? c)
        1
        (* (/ (- r c) c)
           (aux r (sub1 c)))))
  (aux (add1 row) column))

, , , :

(pascal 0 0)

(pascal 1 0)
(pascal 1 1)

(pascal 2 0)
(pascal 2 1)
(pascal 2 2)

(pascal 3 0)
(pascal 3 1)
(pascal 3 2)
(pascal 3 3)

, ; Racket:

(define (pascal-up-to-row n)
  (for*/list ((i (in-range n))
              (j (in-range (add1 i))))
    (pascal i j)))

, :

(pascal-up-to-row 4)
> '(1 1 1 1 2 1 1 3 3 1)
+3

.

Vc . :

(define (row r)
  (let loop ((c 1) (row (list 1)))
    (if (= r c)
        row
        (loop (+ c 1) (cons (* (car row) (- r c) (/ c)) row)))))

, :

(define (rows r)
  (let loop ((r r) (rows (list)))
    (if (zero? r)
        rows
        (loop (- r 1) (append (row r) rows)))))

:

> (rows 4)
(1 1 1 1 2 1 1 3 3 1)

(= r c) (zero? r) .

, , TeX: - , . , Vc V_c, Vc-1 V_ {c-1}.

+3

All Articles