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

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>
    <field name="NAME">AddSpaces</field>
    <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">
          <shadow id="]]tGZ3vxGC0)a$:0ITI2" type="math_number">
            <field name="NUM">1</field>
          </shadow>
        </value>
        <value name="TO">
          <shadow id="sIpGHgUT9bX,6)Ayq2I2" type="math_number">
            <field name="NUM">10</field>
          </shadow>
          <block id="xX_B]2_Z`+xd@[ABdPcm" type="variables_get">
            <field name="VAR">intSpaces</field>
          </block>
        </value>
        <value name="BY">
          <shadow id="CQ;VuuID|wOO@k#*M1Q6" type="math_number">
            <field name="NUM">1</field>
          </shadow>
        </value>
        <statement name="DO">
          <block id="soHcj!$8|dlKniBk17!W" type="text_append">
            <field name="VAR">strCurrentLine</field>
            <value name="TEXT">
              <shadow id="v0F!zYjlz/M8!b[=@|)r" type="text">
                <field name="TEXT">&amp;nbsp;</field>
              </shadow>
            </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">
                  <shadow id="_rh:mkM;g#5S#grP.RB;" type="math_number">
                    <field name="NUM">1</field>
                  </shadow>
                  <block id="MZ.-v2)Dqd~jutCZrlf5" type="variables_get">
                    <field name="VAR">x</field>
                  </block>
                </value>
                <value name="B">
                  <shadow id="J1W~?[T^(BCYrp7mL-4*" type="math_number">
                    <field name="NUM">1</field>
                  </shadow>
                  <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">
                          <shadow id="EMEYs4^_:Ctaf@{$90p$" type="math_number">
                            <field name="NUM">1</field>
                          </shadow>
                          <block id=":?p1WQHhyOuq/8Hq@Uez" type="variables_get">
                            <field name="VAR">x</field>
                          </block>
                        </value>
                        <value name="B">
                          <shadow id="Kd;)Y$=s+o7mCf*qOmJQ" type="math_number">
                            <field name="NUM">1</field>
                          </shadow>
                        </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">
      <shadow id="_5a.VM++L*t}_yc?R5Gq" type="math_number">
        <field name="NUM">0</field>
      </shadow>
    </value>
    <value name="TO">
      <shadow id="j},Z=ek?ftop~~=o(z8G" type="math_number">
        <field name="NUM">12</field>
      </shadow>
    </value>
    <value name="BY">
      <shadow id="lofqk$MnqfbJ!9!fj01R" type="math_number">
        <field name="NUM">1</field>
      </shadow>
    </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">
            <mutation name="AddSpaces">
              <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">
                  <shadow id="i?k%PXwrLp^1+HIcu|g," type="math_number">
                    <field name="NUM">30</field>
                  </shadow>
                </value>
                <value name="B">
                  <shadow id="U#_Lf+dcJ)}yn7sPRXkb" type="math_number">
                    <field name="NUM">1</field>
                  </shadow>
                  <block id=":Wu^:=/g}L73k!GTXGWU" type="math_arithmetic">
                    <field name="OP">MULTIPLY</field>
                    <value name="A">
                      <shadow id="$Z_*[8R,pn|3EbK=tj6j" type="math_number">
                        <field name="NUM">1</field>
                      </shadow>
                      <block id="Lpijk,,VwFHoh8!0-w.u" type="variables_get">
                        <field name="VAR">i</field>
                      </block>
                    </value>
                    <value name="B">
                      <shadow id="}J*GgOviEE-}Lq)r%Za`" type="math_number">
                        <field name="NUM">2</field>
                      </shadow>
                    </value>
                  </block>
                </value>
              </block>
            </value>
            <next>
              <block id=")$n~c=r.s1:zns5YgFUo" type="controls_for">
                <field name="VAR">j</field>
                <value name="FROM">
                  <shadow id="O}iKjlpG~eW$^:GvAdN%" type="math_number">
                    <field name="NUM">0</field>
                  </shadow>
                </value>
                <value name="TO">
                  <shadow id="Yd_0KClyzo@sf@k[iQB{" type="math_number">
                    <field name="NUM">10</field>
                  </shadow>
                  <block id="wXD3[rXLI#{%;A|3)M(o" type="variables_get">
                    <field name="VAR">i</field>
                  </block>
                </value>
                <value name="BY">
                  <shadow id=";F^`t=BC]Jp/yK=d}(wr" type="math_number">
                    <field name="NUM">1</field>
                  </shadow>
                </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">
                                          <shadow id="IEhWDPPMHh0gqZDju^Wd" type="math_number">
                                            <field name="NUM">1</field>
                                          </shadow>
                                          <block id="_t71NtJVB@=*/gA`:Wrs" type="variables_get">
                                            <field name="VAR">i</field>
                                          </block>
                                        </value>
                                        <value name="B">
                                          <shadow id="L%$dYp,0)lVT|?`iAW!f" type="math_number">
                                            <field name="NUM">1</field>
                                          </shadow>
                                          <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">
                                          <shadow id="Ic,hkwF2[t7lCHwifqP*" type="math_number">
                                            <field name="NUM">3.1</field>
                                          </shadow>
                                          <block id=")H#}XIW|lV/%]zjSgnX-" type="math_arithmetic">
                                            <field name="OP">DIVIDE</field>
                                            <value name="A">
                                              <shadow id="Be~o{u{g]TgqV5%.7i*x" type="math_number">
                                                <field name="NUM">1</field>
                                              </shadow>
                                              <block id="{;}zh22C]%b;Z@_.:GW6" type="variables_get">
                                                <field name="VAR">intFact_i</field>
                                              </block>
                                            </value>
                                            <value name="B">
                                              <shadow id=":b|.|qc7CY~+%B|Phe-)" type="math_number">
                                                <field name="NUM">1</field>
                                              </shadow>
                                              <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">
                                              <shadow id="PV%@OD|:5/?a%33zDYM!" type="math_number">
                                                <field name="NUM">3.1</field>
                                              </shadow>
                                              <block id="#s@YHa5f9kS62gxuCVzo" type="math_arithmetic">
                                                <field name="OP">DIVIDE</field>
                                                <value name="A">
                                                  <shadow id="Be~o{u{g]TgqV5%.7i*x" type="math_number">
                                                    <field name="NUM">1</field>
                                                  </shadow>
                                                  <block id="eoI{0-D:O{K[hpjd$U$P" type="variables_get">
                                                    <field name="VAR">intCoeff</field>
                                                  </block>
                                                </value>
                                                <value name="B">
                                                  <shadow id=":b|.|qc7CY~+%B|Phe-)" type="math_number">
                                                    <field name="NUM">1</field>
                                                  </shadow>
                                                  <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">
                          <shadow id=";drf+vgLRYBKqT!Y$dnQ" type="text">
                            <field name="TEXT"></field>
                          </shadow>
                          <block id="6~6}R%-am:AiyyfcrwNn" type="text_join">
                            <mutation items="2"></mutation>
                            <value name="ADD0">
                              <block id=";o=g[F$)+gZ~Pj_MZGdi" type="variables_get">
                                <field name="VAR">intCoeff</field>
                              </block>
                            </value>
                            <value name="ADD1">
                              <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">
                                      <shadow id="?j3y6u{,x#7M7VO}Ao[C" type="math_number">
                                        <field name="NUM">1</field>
                                      </shadow>
                                      <block id="BeO-%7.[Z%0q-ql|aa$W" type="variables_get">
                                        <field name="VAR">intCoeff</field>
                                      </block>
                                    </value>
                                    <value name="B">
                                      <shadow id="!,-]f~|19=9Z=1Fx}r.e" type="math_number">
                                        <field name="NUM">100</field>
                                      </shadow>
                                    </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">
                                <mutation name="AddSpaces">
                                  <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">
                                          <shadow id="?j3y6u{,x#7M7VO}Ao[C" type="math_number">
                                            <field name="NUM">1</field>
                                          </shadow>
                                          <block id="{oM^S7$t_m:Iqc*=B)`l" type="variables_get">
                                            <field name="VAR">intCoeff</field>
                                          </block>
                                        </value>
                                        <value name="B">
                                          <shadow id="ih^KF[j@l|fHP]nEip=$" type="math_number">
                                            <field name="NUM">10</field>
                                          </shadow>
                                        </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">
                                    <mutation name="AddSpaces">
                                      <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">
                      <shadow id="HNP6aXwX_Km+NAZEY.T#" type="text">
                        <field name="TEXT">abc</field>
                      </shadow>
                      <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;


procedure AddSpaces(intSpaces: 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 := '';
  AddSpaces(30 - i * 2);
  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
      AddSpaces(1);
    end;
    if ((intCoeff / 10 < 1)) then
    begin
      AddSpaces(1);
    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)

def AddSpaces(intSpaces):
  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 = ''
  AddSpaces(30 - i * 2)
  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:
      AddSpaces(1)
    if intCoeff / 10 < 1:
      AddSpaces(1)
  print(strCurrentLine)

Screenshot of Output

Pascal's Triangle

Pascal's Triangle

Programming - a skill for life!

XML, image and generated Smart Pascal, JavaScript and Python code of demonstration of recursion for calculating factorials required for the display of Pascal's triangle