c3d2-wiki/Dc.mw

77 lines
1.4 KiB
Plaintext

[http://en.wikipedia.org/wiki/Dc_%28Unix%29 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:
<pre>
#!/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
</pre>
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 [http://de.wikipedia.org/wiki/Fibonacci-Folge Fibonacci-Zahlen] sind rekursiv definiert:
die ersten beiden Zahlen sind 0 und 1, jede weitere ist die Summe ihrer beiden vorhergehenden.
<pre>
#!/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
</pre>
==Sieb des Eratosthenes==
TBD