Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

dc "programming"

Name: Anonymous 2012-07-25 8:52

The UNIX program dc is quite interesting calculator. The man page says: "dc  is  a reverse-polish desk calculator which supports unlimited precision arithmetic."

For starters, a word about this thread: This is not a serious discussion about dc program, but more like how to have fun with dc.

After having some boring time at work, I read the man page of dc for fun. Before, I thought this is quite boring and useless program, but now I feel exited about it. The nice thing I found about it, is that it seems you can do pretty much everything with it.

After messing around a bit, I thought I'd implement fibonacci numbers in it. Here's the result:

30 1-sc1pp[lfx1+dlc>l]sl1dSFSF[LFLFrdSF+pSF]sfllx

To run this, you can run
echo '30 1-sc1pp[lfx1+dlc>l]sl1dSFSF[LFLFrdSF+pSF]sfllx' | dc
in your terminal.

This prints out 30 first fibonacci numbers. I think it's pretty short, and it could be much shorter, though making so might make things harder to read.

What I would like to see in this thread, is other "programs" written in dc. It could be better implementation of fibonacci numbers, or something completely different. I might try doing prime numbers next.

BTW, here's an overly commented version from fibonacci above (for those who don't understand anything)

# push 30 to stack, the count of fibonacci numbers to be calculated
30

# store count-1 (29) to register c. That's our loop counter register
1-sc

# push 1 to stack. we use it as our loop counter
1

# as we have 1 in stack, we might as well print the first two fibonacci numbers :P
pp


# create macro for our loop system
[
  # execute macro f
  lfx
  # increment top of stack (loop counter) by one
  1+
  # compare top of stack to count register (c), execute macro in register l if c is larger
  dlc>l
]
# now, we have macro text on the top of our stack, we save it to register l
sl

# create macro that the loop executes (which goes to register f)
# first, initialize own stack for this. that stack is in register F
# at first it has 1 and 1 in it's stack. the numbers in that stack are
# the two last numbers of fibonacci sequence.
1dSFSF

# begin macro
[
  # get the numbers from F stack to main stack (r reverses them, so they are in same order)
  LFLFr

  # put the top of the main stack back to F stack, but don't change main stack
  # this is the second last number of sequence
  dSF

  # add the two numbers in main stack to get new number for sequence
  +

  # print it
  p
  # store it to stack F. stack F now has two numbers again
  SF
]
# store macro to f
sf

# execute our loop macro in register l
llx

Name: Anonymous 2012-07-26 8:08

>>9
Also, it works with quite big numbers:
500lfxp
122013682599111006870123878542304692625357434280319284219241358838584\
537315388199760549644750220328186301361647714820358416337872207817720\
048078520515932928547790757193933060377296085908627042917454788242491\
272634430567017327076946106280231045264421887878946575477714986349436\
778103764427403382736539747138647787849543848959553753799042324106127\
132698432774571554630997720278101456108118837370953101635632443298702\
956389662891165897476957208792692887128178007026517450776841071962439\
039432253642260523494585012991857150124870696156814162535905669342381\
300885624924689156412677565448188650659384795177536089400574523894033\
579847636394490531306232374906644504882466507594673586207463792518420\
045936969298102226397195259719094521782333175693458150855233282076282\
002340262690789834245171200620771464097945611612762914595123722991334\
016955236385094288559201872743379517301458635757082835578015873543276\
888868012039988238470215146760544540766353598417443048012893831389688\
163948746965881750450692636533817505547812864000000000000000000000000\
000000000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000


so it's much better than your faggot C solutions.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List