# Displaying Pascal's Triangle

[XML] [Smart Pascal] [Python] [output]

This demonstration uses the Factorial recursive function on the previous page to display Pascal's triangle. We base the arrangement of blocks on the Tiny code. See that page for the theory and the reason for the name intCoeff.

The blocks

## XML Code of Blocks

```<xml xmlns="http://www.w3.org/1999/xhtml">
<block id="Nn/[ueg~tP`5P7,yHNe\$" type="procedures_defnoreturn" y="-138" x="12">
<mutation>
<arg name="intSpaces"></arg>
</mutation>
<comment pinned="false" h="80" w="160">Describe this function...</comment>
<statement name="STACK">
<block id="1d6BKWkYd!4DI3BeJLy." type="controls_for">
<field name="VAR">k</field>
<value name="FROM">
<field name="NUM">1</field>
</value>
<value name="TO">
<field name="NUM">10</field>
<block id="xX_B]2_Z`+xd@[ABdPcm" type="variables_get">
<field name="VAR">intSpaces</field>
</block>
</value>
<value name="BY">
<field name="NUM">1</field>
</value>
<statement name="DO">
<block id="soHcj!\$8|dlKniBk17!W" type="text_append">
<field name="VAR">strCurrentLine</field>
<value name="TEXT">
<field name="TEXT">&amp;nbsp;</field>
</value>
</block>
</statement>
</block>
</statement>
</block>
<block id="*apa,;:.WfaNdiyBQDvK" type="procedures_defreturn" y="13" x="13">
<mutation>
<arg name="x"></arg>
</mutation>
<field name="NAME">Factorial</field>
<comment pinned="false" h="80" w="160">Describe this function...</comment>
<statement name="STACK">
<block id="?h.BF/T{J=~L*wr[5w5Q" type="controls_if">
<mutation else="1"></mutation>
<value name="IF0">
<block id="Tu`d:8Y[LoMy6:BuB:m." type="logic_compare">
<field name="OP">EQ</field>
<value name="A">
<block id="Y5/1_./uR=+O}vMJ*e|L" type="variables_get">
<field name="VAR">x</field>
</block>
</value>
<value name="B">
<block id="{2KEP5e)Doy-khuG]n?8" type="math_number">
<field name="NUM">0</field>
</block>
</value>
</block>
</value>
<statement name="DO0">
<block id="i0]8os#B#C#*}n6:[%\$o" type="variables_set">
<field name="VAR">intResult</field>
<value name="VALUE">
<block id="gpJDci5MI*;dcUQQlc*]" type="math_number">
<field name="NUM">1</field>
</block>
</value>
</block>
</statement>
<statement name="ELSE">
<block id="m,ucmM,`@tI~}j?K*7}2" type="variables_set">
<field name="VAR">intResult</field>
<value name="VALUE">
<block id="aMg[2Rt%\$.m5Yn94d#^_" type="math_arithmetic">
<field name="OP">MULTIPLY</field>
<value name="A">
<field name="NUM">1</field>
<block id="MZ.-v2)Dqd~jutCZrlf5" type="variables_get">
<field name="VAR">x</field>
</block>
</value>
<value name="B">
<field name="NUM">1</field>
<block id="hn`Sr5.dCqg!(^]sj~Q{" type="procedures_callreturn">
<mutation name="Factorial">
<arg name="x"></arg>
</mutation>
<value name="ARG0">
<block id="NVE=3~8ZHUp^y,0oq=Vt" type="math_arithmetic">
<field name="OP">MINUS</field>
<value name="A">
<field name="NUM">1</field>
<block id=":?p1WQHhyOuq/8Hq@Uez" type="variables_get">
<field name="VAR">x</field>
</block>
</value>
<value name="B">
<field name="NUM">1</field>
</value>
</block>
</value>
</block>
</value>
</block>
</value>
</block>
</statement>
</block>
</statement>
<value name="RETURN">
<block id="3PwOVzx;phq5vD,LpK@e" type="variables_get">
<field name="VAR">intResult</field>
</block>
</value>
</block>
<block id="ny%-,+m+axJ:HlsMxtMZ" type="controls_for" y="238" x="13">
<field name="VAR">i</field>
<value name="FROM">
<field name="NUM">0</field>
</value>
<value name="TO">
<field name="NUM">12</field>
</value>
<value name="BY">
<field name="NUM">1</field>
</value>
<statement name="DO">
<block id="+Y8j`SMhLV@TMLC,0848" type="variables_set">
<field name="VAR">strCurrentLine</field>
<value name="VALUE">
<block id="I=C`\$+#I#NyrYdgv!z]b" type="text">
<field name="TEXT"></field>
</block>
</value>
<next>
<block id="#I%twG^CYHTcig}[k{tn" type="procedures_callnoreturn">
<arg name="intSpaces"></arg>
</mutation>
<value name="ARG0">
<block id="xe`4s~ofM0/Tw6`qht,Y" type="math_arithmetic">
<field name="OP">MINUS</field>
<value name="A">
<field name="NUM">30</field>
</value>
<value name="B">
<field name="NUM">1</field>
<block id=":Wu^:=/g}L73k!GTXGWU" type="math_arithmetic">
<field name="OP">MULTIPLY</field>
<value name="A">
<field name="NUM">1</field>
<block id="Lpijk,,VwFHoh8!0-w.u" type="variables_get">
<field name="VAR">i</field>
</block>
</value>
<value name="B">
<field name="NUM">2</field>
</value>
</block>
</value>
</block>
</value>
<next>
<block id=")\$n~c=r.s1:zns5YgFUo" type="controls_for">
<field name="VAR">j</field>
<value name="FROM">
<field name="NUM">0</field>
</value>
<value name="TO">
<field name="NUM">10</field>
<block id="wXD3[rXLI#{%;A|3)M(o" type="variables_get">
<field name="VAR">i</field>
</block>
</value>
<value name="BY">
<field name="NUM">1</field>
</value>
<statement name="DO">
<block id="f}Q*5R%o=hN-!uKwa^Ns" type="controls_if">
<mutation else="1"></mutation>
<value name="IF0">
<block id="NPA#QtmKq1);.?j@x/ft" type="logic_operation">
<field name="OP">OR</field>
<value name="A">
<block id="f@?D?Q-3/=faz!9Jk}%;" type="logic_compare">
<field name="OP">EQ</field>
<value name="A">
<block id="h^GF\$,NL]0A^+~lsISD|" type="variables_get">
<field name="VAR">j</field>
</block>
</value>
<value name="B">
<block id="-f#NPp=_zi_AiYC]URfl" type="math_number">
<field name="NUM">0</field>
</block>
</value>
</block>
</value>
<value name="B">
<block id="rw%UBCi|cLLv@l.QUx*t" type="logic_compare">
<field name="OP">EQ</field>
<value name="A">
<block id="Xu[ljP[`*q%!!vyPjC^x" type="variables_get">
<field name="VAR">j</field>
</block>
</value>
<value name="B">
<block id="/b)U8Q\$v-^sMy:q#rSaM" type="variables_get">
<field name="VAR">i</field>
</block>
</value>
</block>
</value>
</block>
</value>
<statement name="DO0">
<block id=".*R%?RcL|U_EL\$\$Rx@yc" type="variables_set">
<field name="VAR">intCoeff</field>
<value name="VALUE">
<block id="}RQwq?+\$gSH?y0pggQoi" type="math_number">
<field name="NUM">1</field>
</block>
</value>
</block>
</statement>
<statement name="ELSE">
<block id="a}A+kBIukqBhZ27l*r6)" type="variables_set">
<field name="VAR">intFact_i</field>
<value name="VALUE">
<block id="8G4~kFXjQgilmf.T3G{U" type="procedures_callreturn">
<mutation name="Factorial">
<arg name="x"></arg>
</mutation>
<value name="ARG0">
<block id="{sBq*W3{pM+m^Vy+c5vk" type="variables_get">
<field name="VAR">i</field>
</block>
</value>
</block>
</value>
<next>
<block id="@b+t4]6@`49~/s5l*NF," type="variables_set">
<field name="VAR">intFact_j</field>
<value name="VALUE">
<block id="%/psYzEj?,~-[V|BE@kw" type="procedures_callreturn">
<mutation name="Factorial">
<arg name="x"></arg>
</mutation>
<value name="ARG0">
<block id="I0;Lv|Pokoy-R2fF*ciQ" type="variables_get">
<field name="VAR">j</field>
</block>
</value>
</block>
</value>
<next>
<block id="hj^X3l{Z^fuEn\$385y#5" type="variables_set">
<field name="VAR">intFact_i_minus_j</field>
<value name="VALUE">
<block id="=SH,J-dT[pw:xdoH\$QCt" type="procedures_callreturn">
<mutation name="Factorial">
<arg name="x"></arg>
</mutation>
<value name="ARG0">
<block id="ZP;72yRwxbX(iZ`p2Zo;" type="math_arithmetic">
<field name="OP">MINUS</field>
<value name="A">
<field name="NUM">1</field>
<block id="_t71NtJVB@=*/gA`:Wrs" type="variables_get">
<field name="VAR">i</field>
</block>
</value>
<value name="B">
<field name="NUM">1</field>
<block id="}.dbzFt!JcH%S;^4P;_3" type="variables_get">
<field name="VAR">j</field>
</block>
</value>
</block>
</value>
</block>
</value>
<next>
<block id="=MU-i)HAX285ONO#DtWd" type="variables_set">
<field name="VAR">intCoeff</field>
<value name="VALUE">
<block id="mMo_W?d]Ua5N*E5kB?p=" type="math_round">
<field name="OP">ROUND</field>
<value name="NUM">
<field name="NUM">3.1</field>
<block id=")H#}XIW|lV/%]zjSgnX-" type="math_arithmetic">
<field name="OP">DIVIDE</field>
<value name="A">
<field name="NUM">1</field>
<block id="{;}zh22C]%b;Z@_.:GW6" type="variables_get">
<field name="VAR">intFact_i</field>
</block>
</value>
<value name="B">
<field name="NUM">1</field>
<block id="YUHjR9o!v}xp^~0A/:Oy" type="variables_get">
<field name="VAR">intFact_j</field>
</block>
</value>
</block>
</value>
</block>
</value>
<next>
<block id="k`|XqN}3GT7N8Y04Pb:Q" type="variables_set">
<field name="VAR">intCoeff</field>
<value name="VALUE">
<block id="bwcPkb1wFp*4ki~gTew(" type="math_round">
<field name="OP">ROUND</field>
<value name="NUM">
<field name="NUM">3.1</field>
<block id="#s@YHa5f9kS62gxuCVzo" type="math_arithmetic">
<field name="OP">DIVIDE</field>
<value name="A">
<field name="NUM">1</field>
<block id="eoI{0-D:O{K[hpjd\$U\$P" type="variables_get">
<field name="VAR">intCoeff</field>
</block>
</value>
<value name="B">
<field name="NUM">1</field>
<block id="7J-[@FC+r%-7IF+W~Y-r" type="variables_get">
<field name="VAR">intFact_i_minus_j</field>
</block>
</value>
</block>
</value>
</block>
</value>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</statement>
<next>
<block id="@n1hcDX%`n4LCfEIo[@L" type="text_append">
<field name="VAR">strCurrentLine</field>
<value name="TEXT">
<field name="TEXT"></field>
<block id="6~6}R%-am:AiyyfcrwNn" type="text_join">
<mutation items="2"></mutation>
<block id=";o=g[F\$)+gZ~Pj_MZGdi" type="variables_get">
<field name="VAR">intCoeff</field>
</block>
</value>
<block id="5/q+ooD.4mrwa^^~#Bma" type="text">
<field name="TEXT"> </field>
</block>
</value>
</block>
</value>
<next>
<block id="\$|D=^JvSw*a|:Ip0[,~}" type="controls_if">
<value name="IF0">
<block id="}~4].`v,6`it_*4JLG9r" type="logic_compare">
<field name="OP">LT</field>
<value name="A">
<block id="=IPIp{;!YWQZ,7Ofqqa=" type="math_arithmetic">
<field name="OP">DIVIDE</field>
<value name="A">
<field name="NUM">1</field>
<block id="BeO-%7.[Z%0q-ql|aa\$W" type="variables_get">
<field name="VAR">intCoeff</field>
</block>
</value>
<value name="B">
<field name="NUM">100</field>
</value>
</block>
</value>
<value name="B">
<block id=")H`0_@+UCx{)MR2}=U|#" type="math_number">
<field name="NUM">1</field>
</block>
</value>
</block>
</value>
<statement name="DO0">
<block id="K+9I/%[qx~/`!D@t.S[^" type="procedures_callnoreturn">
<arg name="intSpaces"></arg>
</mutation>
<value name="ARG0">
<block id="U6]\$P4!wP(aJ]ijbCCZ*" type="math_number">
<field name="NUM">1</field>
</block>
</value>
</block>
</statement>
<next>
<block id="Zs`t-Mqyohp3N0dv)y?!" type="controls_if">
<value name="IF0">
<block id="E5];cD9B/`[9)r2j1RhI" type="logic_compare">
<field name="OP">LT</field>
<value name="A">
<block id="xM!*94!W}LH[r,Lf_Rvq" type="math_arithmetic">
<field name="OP">DIVIDE</field>
<value name="A">
<field name="NUM">1</field>
<block id="{oM^S7\$t_m:Iqc*=B)`l" type="variables_get">
<field name="VAR">intCoeff</field>
</block>
</value>
<value name="B">
<field name="NUM">10</field>
</value>
</block>
</value>
<value name="B">
<block id="uduOX/x\$7\$7EZKDI9/xI" type="math_number">
<field name="NUM">1</field>
</block>
</value>
</block>
</value>
<statement name="DO0">
<block id="8eJ_BD!x0+dy%|QXn[Z4" type="procedures_callnoreturn">
<arg name="intSpaces"></arg>
</mutation>
<value name="ARG0">
<block id="H?Ha*gTj%/(V}Klw-q2g" type="math_number">
<field name="NUM">1</field>
</block>
</value>
</block>
</statement>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</statement>
<next>
<block id="c.EY\$v8H9E+93jTEErg8" type="text_print">
<value name="TEXT">
<field name="TEXT">abc</field>
<block id="fr[~R`n}4X2D|Z8!jWNK" type="variables_get">
<field name="VAR">strCurrentLine</field>
</block>
</value>
</block>
</next>
</block>
</next>
</block>
</next>
</block>
</statement>
</block>
</xml>

```

## Generated Smart Pascal Code

```var strCurrentLine: String;
var intSpaces: Integer;
var intResult: Integer;
var intCoeff: Integer;
var intFact_i_minus_j: Integer;
var intFact_j: Integer;
var intFact_i: Integer;

begin
for var k := 1 to intSpaces step 1 do
begin
strCurrentLine += '&nbsp;';
end;
end;

function Factorial(x: Variant): Integer;
begin
if ((x = 0)) then
begin
intResult := 1;
end
else
begin
intResult := x * Factorial(x - 1);
end;
Result := intResult;
end;

function Str(v: Variant) : string;
begin
asm
if (@v === 0) {
@Result = '0';
}
else if (@v === '') {
@Result = '';
}
else if (isNaN(@v)) {
@Result = @v;
}
else {
@Result = (@v).toString();
}
end;
end;

for var i := 0 to 12 do
begin
strCurrentLine := '';
for var j := 0 to i step 1 do
begin
if ((j = 0) or (j = i)) then
begin
intCoeff := 1;
end
else
begin
intFact_i := Factorial(i);
intFact_j := Factorial(j);
intFact_i_minus_j := Factorial(i - j);
intCoeff := Round(intFact_i / intFact_j);
intCoeff := Round(intCoeff / intFact_i_minus_j);
end;
strCurrentLine += Str(intCoeff) + Str(' ');
if ((intCoeff / 100 < 1)) then
begin
end;
if ((intCoeff / 10 < 1)) then
begin
end;
end;
Console.writeln(strCurrentLine);
end;

```

## Generated Python Code

We generated Python code from the same blocks then changed '&nbsp;' to ' ' (space in quotes) and changed both occurrences of round to int. You can try the code online.

```import math

strCurrentLine = None
intSpaces = None
k = None
intResult = None
x = None
intCoeff = None
intFact_i_minus_j = None
intFact_j = None
intFact_i = None
j = None
i = None

def upRange(start, stop, step):
while start <= stop:
yield start
start += abs(step)

def downRange(start, stop, step):
while start >= stop:
yield start
start -= abs(step)

global strCurrentLine, k, intResult, x, intCoeff, intFact_i_minus_j, intFact_j, intFact_i, j, i
for k in (1 <= float(intSpaces)) and upRange(1, float(intSpaces), 1) or downRange(1, float(intSpaces), 1):
strCurrentLine = str(strCurrentLine) + str(' ')

def Factorial(x):
global strCurrentLine, intSpaces, k, intResult, intCoeff, intFact_i_minus_j, intFact_j, intFact_i, j, i
if x == 0:
intResult = 1
else:
intResult = x * Factorial(x - 1)
return intResult

for i in range(13):
strCurrentLine = ''
for j in (0 <= float(i)) and upRange(0, float(i), 1) or downRange(0, float(i), 1):
if j == 0 or j == i:
intCoeff = 1
else:
intFact_i = Factorial(i)
intFact_j = Factorial(j)
intFact_i_minus_j = Factorial(i - j)
intCoeff = int(intFact_i / intFact_j)
intCoeff = int(intCoeff / intFact_i_minus_j)
strCurrentLine = str(strCurrentLine) + str(str(intCoeff) + str(' '))
if intCoeff / 100 < 1: