# [NTG-context] Code lua in a table

Jeong Dal haksan at me.com
Mon Jul 20 07:56:58 CEST 2020

```Dear Fabrice,

You may split Binom(n,k) function into two functions as following:

*****
\startluacode
P={}
combi = P

function P.fact (n)
if n <= 0 then
return 1
else
return n * P.fact(n-1)
end
end

function P.ncr(n,r)
return P.fact(n)/(P.fact(r)*P.fact(n-r))
end
combi = {
fact = fact,
ncr = ncr,
}
\stopluacode
******

Your table is actually Pascal’d triangle.
Using the above function, I was able to draw Pascal’s triangles.
Hans helped me to complete it.
I couldn’t wikify it at that time because I don’t know how to.  I’ll do it soon.

Here is the whole code for Pascal’s triangle in two different ways using Lua, Metafun and ConTeXt.
You may enhance the code.

\startbuffer[pt1]
numeric n,r,s,u,dx,dy,tt; u := 1.8cm;
path p, q;
pair A,B,start,now;
A := dir(210)*u;
B := dir(-30)*u;
dy := sind(30)*u;
dx := 2*cosd(30)*u;
for n=0 upto 4:
start := n*dir(210)*u;
for r=0 upto n:
s := n-r;
tt := lua("mp.print(P.ncr(" & decimal n & "," &  decimal r & " ))");
now := start+r*right*dx;
dotlabel.top(textext("\$\displaystyle {" & decimal n & "\choose" & decimal r & "} = "& decimal tt & "\$"),now);
draw (now+A) -- now -- (now+B);
endfor;
endfor;
\stopbuffer
\startbuffer[pt2]
numeric n,r,s,u,dx,dy,tt; u := 1cm;
path p, q;
pair A,B,start,now;
A := dir(210)*u;
B := dir(-30)*u;
dy := sind(30)*u;
dx := 2*cosd(30)*u;
for n=0 upto 8:
start := n*dir(210)*u;
for r=0 upto n:
s := n-r;
tt := lua("mp.print(P.ncr(" & decimal n & "," &  decimal r & " ))");
now := start+r*right*dx;
label(textext("\$" & decimal tt & "\$"),now);
endfor;
endfor;
\stopbuffer
\startluacode
P={}
combi = P

function P.fact (n)
if n <= 0 then
return 1
else
return n * P.fact(n-1)
end
end

function P.ncr(n,r)
return P.fact(n)/(P.fact(r)*P.fact(n-r))
end
combi = {
fact = fact,
ncr = ncr,
}
\stopluacode
\starttext

\processMPbuffer[pt1]
\blank[big]
\processMPbuffer[pt2]

\stoptext

I hope that it helps.

Best regards,

Dalyoung

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.ntg.nl/pipermail/ntg-context/attachments/20200720/7367f295/attachment.htm>
```