Hello, I want to display the values of Pascal's triangle in a table. Unfortunately my knowledge of Lua is not sufficient to run this code. Thank you. Fabrice \starttext \startluacode function Binom(n,k) if k > n then return 0 elseif (n == 0 or k == 0) then return 1 else return (n*Binom(n-1,k-1))/k end context(Binom(n,k)) end context.startxtable({"align={middle,lohi}, width=1.2cm,offset=0.8ex,bodyfont=9pt"}) context.startxrow({"background=color,backgroundcolor=green"}) for i = 0, 10 do for j = 0, i do context.startxcell() context(Binom(i,j)) context.stopxcell() end end context.stopxrow() \stopluacode \stoptext
Fabrice Couvreur schrieb am 18.07.2020 um 20:05:
Hello, I want to display the values of Pascal's triangle in a table. Unfortunately my knowledge of Lua is not sufficient to run this code. Thank you. Fabrice
\starttext
\startluacode function Binom(n,k) if k > n then return 0 elseif (n == 0 or k == 0) then return 1 else return (n*Binom(n-1,k-1))/k end context(Binom(n,k)) Are you sure you want to use context(...) here? end
context.startxtable({"align={middle,lohi}, width=1.2cm,offset=0.8ex,bodyfont=9pt"}) context.startxrow({"background=color,backgroundcolor=green"}) for i = 0, 10 do for j = 0, i do context.startxcell() context(Binom(i,j)) context.stopxcell() end end context.stopxrow() context.stopxtable()
Wolfgang
Hi, I improved the code to be able to display the grid but I cannot display the coefficients. Thank you Fabrice \starttext \startluacode function Binom(n,k) if k > n then return 0 elseif (n == 0 or k == 0) then return 1 else return (n*Binom(n-1,k-1))/k end end context.startxtable({"align={middle,lohi}, width=1.2cm,offset=0.8ex,bodyfont=9pt"}) for i = 1, 10 do context.startxrow({"background=color,backgroundcolor=cyan"}) for j = 1, i do context.startxcell() \ctxlua{context(Binom(i,j))} context.stopxcell() end context.stopxrow() end context.stopxtable() \stopluacode \stoptext Le sam. 18 juil. 2020 à 20:32, Wolfgang Schuster < wolfgang.schuster.lists@gmail.com> a écrit :
Fabrice Couvreur schrieb am 18.07.2020 um 20:05:
Hello, I want to display the values of Pascal's triangle in a table. Unfortunately my knowledge of Lua is not sufficient to run this code. Thank you. Fabrice
\starttext
\startluacode function Binom(n,k) if k > n then return 0 elseif (n == 0 or k == 0) then return 1 else return (n*Binom(n-1,k-1))/k end context(Binom(n,k))
Are you sure you want to use context(...) here?
end
context.startxtable({"align={middle,lohi}, width=1.2cm,offset=0.8ex,bodyfont=9pt"}) context.startxrow({"background=color,backgroundcolor=green"}) for i = 0, 10 do for j = 0, i do context.startxcell() context(Binom(i,j)) context.stopxcell() end end context.stopxrow()
context.stopxtable()
Wolfgang
___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net
___________________________________________________________________________________
Fabrice Couvreur schrieb am 18.07.2020 um 22:09:
Hi, I improved the code to be able to display the grid but I cannot display the coefficients. Thank you Fabrice
\starttext
\startluacode function Binom(n,k) if k > n then return 0 elseif (n == 0 or k == 0) then return 1 else return (n*Binom(n-1,k-1))/k end
end
context.startxtable({"align={middle,lohi}, width=1.2cm,offset=0.8ex,bodyfont=9pt"}) for i = 1, 10 do context.startxrow({"background=color,backgroundcolor=cyan"}) for j = 1, i do context.startxcell() \ctxlua{context(Binom(i,j))}
Remove \ctxlua: context(Binom(i,j)) Wolfgang
Hi Wolfgang, It works but why did you ask this question since I finally use context ?
Are you sure you want to use context(...) here?
How not to display the .0 ? Fabrice Le sam. 18 juil. 2020 à 22:22, Wolfgang Schuster < wolfgang.schuster.lists@gmail.com> a écrit :
Fabrice Couvreur schrieb am 18.07.2020 um 22:09:
Hi, I improved the code to be able to display the grid but I cannot display the coefficients. Thank you Fabrice
\starttext
\startluacode function Binom(n,k) if k > n then return 0 elseif (n == 0 or k == 0) then return 1 else return (n*Binom(n-1,k-1))/k end
end
context.startxtable({"align={middle,lohi}, width=1.2cm,offset=0.8ex,bodyfont=9pt"}) for i = 1, 10 do context.startxrow({"background=color,backgroundcolor=cyan"}) for j = 1, i do context.startxcell() \ctxlua{context(Binom(i,j))}
Remove \ctxlua:
context(Binom(i,j))
Wolfgang
___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net
___________________________________________________________________________________
Small correction :
\starttext
\startluacode
function Binom(n,k)
if k > n then
return 0
elseif (n == 0 or k == 0) then
return 1
else
return (n*Binom(n-1,k-1))/k
end
end
context.startxtable({"align={middle,lohi},
width=1cm,offset=0.8ex,bodyfont=9pt"})
for i = 0, 9 do
context.startxrow({"background=color,backgroundcolor=cyan"})
for j = 0, i do
context.startxcell()
context(Binom(i,j))
context.stopxcell()
end
context.stopxrow()
end
context.stopxtable()
\stopluacode
\stoptext
Le sam. 18 juil. 2020 à 22:25, Fabrice Couvreur
Hi Wolfgang,
It works but why did you ask this question since I finally use context ?
Are you sure you want to use context(...) here?
How not to display the .0 ? Fabrice
Le sam. 18 juil. 2020 à 22:22, Wolfgang Schuster < wolfgang.schuster.lists@gmail.com> a écrit :
Fabrice Couvreur schrieb am 18.07.2020 um 22:09:
Hi, I improved the code to be able to display the grid but I cannot display the coefficients. Thank you Fabrice
\starttext
\startluacode function Binom(n,k) if k > n then return 0 elseif (n == 0 or k == 0) then return 1 else return (n*Binom(n-1,k-1))/k end
end
context.startxtable({"align={middle,lohi}, width=1.2cm,offset=0.8ex,bodyfont=9pt"}) for i = 1, 10 do context.startxrow({"background=color,backgroundcolor=cyan"}) for j = 1, i do context.startxcell() \ctxlua{context(Binom(i,j))}
Remove \ctxlua:
context(Binom(i,j))
Wolfgang
___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki!
maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://context.aanhet.net archive : https://bitbucket.org/phg/context-mirror/commits/ wiki : http://contextgarden.net
___________________________________________________________________________________
Fabrice Couvreur schrieb am 18.07.2020 um 22:25:
Hi Wolfgang,
It works but why did you ask this question since I finally use context ?
Are you sure you want to use context(...) here?
In your first version you used the context function in the loop to print the output of the Binom but the Binom function used context as well, this could result in context(context(...)).
How not to display the .0 ?
Format the content of the context function to show only integer values, e.g. context("%d",Binom(...)). For more information search for formatter in cld-mkiv.pdf. Wolfgang
On 18 Jul 2020, at 22:25, Fabrice Couvreur
wrote: Hi Wolfgang,
It works but why did you ask this question since I finally use context ? Are you sure you want to use context(...) here? How not to display the .0 ?
Besides what Wolfgang suggested, ou can also use context(math.round(Binom(i,j))) to have integers instead of real numbers. Best regards: Otared
On 18 Jul 2020, at 23:35, Otared Kavian
wrote: On 18 Jul 2020, at 22:25, Fabrice Couvreur
wrote: Hi Wolfgang,
It works but why did you ask this question since I finally use context ? Are you sure you want to use context(...) here? How not to display the .0 ?
Or even better, you can return an integer in your Binom function: return math.round((n*Binom(n-1,k-1))/k) Best regards: Otared
participants (3)
-
Fabrice Couvreur
-
Otared Kavian
-
Wolfgang Schuster