2017-03-25, 08:09
One of the many approximations of Pi is the Gregory-Leibniz series (image source Wikipedia):
Here is an implementation in Python:
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 25 06:52:11 2017
@author: Uwe Ziegenhagen
"""
sum = 0
factor = -1
for i in range(1, 640000, 2):
sum = sum + factor * 1/i
factor *= -1
# print(sum)
print(-4*sum) |
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 25 06:52:11 2017
@author: Uwe Ziegenhagen
"""
sum = 0
factor = -1
for i in range(1, 640000, 2):
sum = sum + factor * 1/i
factor *= -1
# print(sum)
print(-4*sum)
Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined.
Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.
More Posts - Website
2014-02-02, 10:59
Here’s a simple Python script which generates math exercises for kids. It uses the Century Schoolbook font which is said to be well readable and LaTeX’s longtable. I just ran some tests, generating 24’000 lines on 2000 pages was no deal at all.
#!/usr/bin/python
# coding: utf-8
import random
import os
min = 1 # number to start with
max = 10 # maximum number
count = 24 # 12 fit on one page
with open('Aufgaben.tex', 'w') as texfile:
texfile.write("\\documentclass[15pt,ngerman]{scrartcl}\n")
texfile.write("\\usepackage[utf8]{inputenc}\n")
texfile.write("\\usepackage{tgschola,longtable}\n")
texfile.write("\\usepackage[T1]{fontenc}\n")
texfile.write("\\setlength{\\parindent}{0pt}\n")
texfile.write("\\pagestyle{empty}\n")
texfile.write("\\renewcommand*{\\arraystretch}{1.5}\n")
texfile.write("\\begin{document}\n")
texfile.write("\\huge\n")
texfile.write("\\begin{longtable}{cccp{8cm}c}")
for i in range(count):
a = random.randint(min, max)
b = random.randint(min, max)
result = a + b;
texfile.write('%s &+ &%s &= &%s \\\\ \\hline\n'%(str(a),str(b),str(result)))
texfile.write("\\end{longtable}\n")
texfile.write("\\end{document}\n")
os.system("pdflatex Aufgaben.tex")
os.startfile("Aufgaben.pdf") # this line may not work under Linux |
#!/usr/bin/python
# coding: utf-8
import random
import os
min = 1 # number to start with
max = 10 # maximum number
count = 24 # 12 fit on one page
with open('Aufgaben.tex', 'w') as texfile:
texfile.write("\\documentclass[15pt,ngerman]{scrartcl}\n")
texfile.write("\\usepackage[utf8]{inputenc}\n")
texfile.write("\\usepackage{tgschola,longtable}\n")
texfile.write("\\usepackage[T1]{fontenc}\n")
texfile.write("\\setlength{\\parindent}{0pt}\n")
texfile.write("\\pagestyle{empty}\n")
texfile.write("\\renewcommand*{\\arraystretch}{1.5}\n")
texfile.write("\\begin{document}\n")
texfile.write("\\huge\n")
texfile.write("\\begin{longtable}{cccp{8cm}c}")
for i in range(count):
a = random.randint(min, max)
b = random.randint(min, max)
result = a + b;
texfile.write('%s &+ &%s &= &%s \\\\ \\hline\n'%(str(a),str(b),str(result)))
texfile.write("\\end{longtable}\n")
texfile.write("\\end{document}\n")
os.system("pdflatex Aufgaben.tex")
os.startfile("Aufgaben.pdf") # this line may not work under Linux
With little modification one can also generate substraction exercises:
#!/usr/bin/python
# coding: utf-8
import random
min = 1
max = 12
count = 24
import os # for sys calls
with open('Aufgaben.tex', 'w') as texfile:
texfile.write("\\documentclass[15pt,ngerman]{scrartcl}\n")
texfile.write("\\usepackage[utf8]{inputenc}\n")
texfile.write("\\usepackage{tgschola,nicefrac,longtable}\n")
texfile.write("\\usepackage[T1]{fontenc}\n")
texfile.write("\\setlength{\\parindent}{0pt}\n")
texfile.write("\\pagestyle{empty}\n")
texfile.write("\\renewcommand*{\\arraystretch}{1.5}\n")
texfile.write("\\begin{document}\n")
texfile.write("\\huge\n")
texfile.write("\\begin{longtable}{cccp{8cm}c}")
for i in range(count):
b = random.randint(min, max)
a = b + random.randint(1, 9)
result = a - b;
texfile.write('%s &- &%s &= &%s \\\\ \\hline\n'%(str(a),str(b),str(result)))
texfile.write("\\end{longtable}\n")
texfile.write("\\end{document}\n")
os.system("pdflatex Aufgaben.tex")
os.startfile("Aufgaben.pdf") |
#!/usr/bin/python
# coding: utf-8
import random
min = 1
max = 12
count = 24
import os # for sys calls
with open('Aufgaben.tex', 'w') as texfile:
texfile.write("\\documentclass[15pt,ngerman]{scrartcl}\n")
texfile.write("\\usepackage[utf8]{inputenc}\n")
texfile.write("\\usepackage{tgschola,nicefrac,longtable}\n")
texfile.write("\\usepackage[T1]{fontenc}\n")
texfile.write("\\setlength{\\parindent}{0pt}\n")
texfile.write("\\pagestyle{empty}\n")
texfile.write("\\renewcommand*{\\arraystretch}{1.5}\n")
texfile.write("\\begin{document}\n")
texfile.write("\\huge\n")
texfile.write("\\begin{longtable}{cccp{8cm}c}")
for i in range(count):
b = random.randint(min, max)
a = b + random.randint(1, 9)
result = a - b;
texfile.write('%s &- &%s &= &%s \\\\ \\hline\n'%(str(a),str(b),str(result)))
texfile.write("\\end{longtable}\n")
texfile.write("\\end{document}\n")
os.system("pdflatex Aufgaben.tex")
os.startfile("Aufgaben.pdf")
Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined.
Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.
More Posts - Website