Dc
Zur Navigation springen
Zur Suche springen
dc ist eine Programmiersprache, die auf fast jedem Unix-Rechner installiert ist. dc ist sehr minimalistisch und gerade darum macht es Spaß, darin zu programmieren. Bitte tragt eure eigenen dc-Programme hier ein:
Range
Zum Warmwerden. Das folgende Programm gibt einfach aufeinanderfolgende Zahlen innerhalb der vorgegebenen Schranken aus:
#!/usr/bin/env dc [Starting from? ]n?sa [Up to? ]n?sb # a is the lower bound # b is the upper bound [ # load lower bound la # print lower bound p # increment 1+ # store upper bound dsa # copy lower bound lbdsb # compare and recurse >f ]sf # call f lfdsfx
Besonders schön sieht das dann als Einzeiler ohne Kommentare aus:
[Starting from? ]n?sa[Up to? ]n?sb[lap1+dsalbdsb>f]sflfdsfx
Fibonacci
Ein Klassiker. Die Fibonacci-Zahlen sind rekursiv definiert: die ersten beiden Zahlen sind 0 und 1, jede weitere ist die Summe ihrer beiden vorhergehenden.
#!/usr/bin/env dc [How many fibonacci numbers should be computed? ]n?sa # start values 0p 1p # counter 1sb [ # save second summand dsc # add numbers on stack + # restore second summand as new first lcr # print p # check counter and recurse lb1+dsbladsa>f ]sf # call f lfdsfx
Sieb des Eratosthenes
TBD