Posts tagged ‘Math’

## Approximating Pi with Python

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)

### Uwe

Uwe Ziegenhagen mag LaTeX und Python, auch gern in Kombination.

Hat Dir dieser Beitrag geholfen und möchtest Du Dich dafür bedanken? Dann unterstütze doch vielleicht die Dingfabrik Köln e.V. mit einem kleinen Beitrag. Details zur Bezahlung findest Du unter Spenden für die Dingfabrik.

## Generating math exercises for kids

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

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")

### Uwe

Uwe Ziegenhagen mag LaTeX und Python, auch gern in Kombination.

Hat Dir dieser Beitrag geholfen und möchtest Du Dich dafür bedanken? Dann unterstütze doch vielleicht die Dingfabrik Köln e.V. mit einem kleinen Beitrag. Details zur Bezahlung findest Du unter Spenden für die Dingfabrik.