Skip to main content
removed `-e""` from byte count, per http://codegolf.stackexchange.com/questions/25670/output-pi-without-math#comment55713_25694
Source Link
juh
  • 983
  • 7
  • 15

dc, 5450 bytes

dc -e"?dsist[lt2/dstd*li<B]dsBx[lt1+dstd*li!<A]dsAxlt1-f"

Spaced out and explained:

               # The idea here is to start with the input and reduce it quickly until it is
               # less than what we want, then increment it until it's just right
?              # Take input from stdin
d si st        # Duplicate input, store in `i' and in `t'
[              # Begin macro definition (when I write in dc, "macro"=="function")
 lt            # Load t, our test term
 2/            # Divide t by two
 d st          # Store a copy of this new term in `t'
 d*            # Duplicate and multiply (square)
 li<B          # Load i; if i<(t^2), execute B
] d sB x       # Duplicate, store function as `B', and execute
               # Loop ends when t^2 is less than i
[              # Begin macro definition
 lt            # Load t, our test term
 1+            # Increment
 d st          # Store a copy of this new term in `t'
 d*            # Duplicate and multiply (square)
 li!<A         # Load i; if i>=(t^2), execute A
] d sA x       # Duplicate, store function as `A', and execute
               # Loop ends when t^2 == i+1
lt 1- f        # Load t, decrement, and dump stack

dc, 54 bytes

dc -e"?dsist[lt2/dstd*li<B]dsBx[lt1+dstd*li!<A]dsAxlt1-f"

Spaced out and explained:

               # The idea here is to start with the input and reduce it quickly until it is
               # less than what we want, then increment it until it's just right
?              # Take input from stdin
d si st        # Duplicate input, store in `i' and in `t'
[              # Begin macro definition (when I write in dc, "macro"=="function")
 lt            # Load t, our test term
 2/            # Divide t by two
 d st          # Store a copy of this new term in `t'
 d*            # Duplicate and multiply (square)
 li<B          # Load i; if i<(t^2), execute B
] d sB x       # Duplicate, store function as `B', and execute
               # Loop ends when t^2 is less than i
[              # Begin macro definition
 lt            # Load t, our test term
 1+            # Increment
 d st          # Store a copy of this new term in `t'
 d*            # Duplicate and multiply (square)
 li!<A         # Load i; if i>=(t^2), execute A
] d sA x       # Duplicate, store function as `A', and execute
               # Loop ends when t^2 == i+1
lt 1- f        # Load t, decrement, and dump stack

dc, 50 bytes

dc -e"?dsist[lt2/dstd*li<B]dsBx[lt1+dstd*li!<A]dsAxlt1-f"

Spaced out and explained:

               # The idea here is to start with the input and reduce it quickly until it is
               # less than what we want, then increment it until it's just right
?              # Take input from stdin
d si st        # Duplicate input, store in `i' and in `t'
[              # Begin macro definition (when I write in dc, "macro"=="function")
 lt            # Load t, our test term
 2/            # Divide t by two
 d st          # Store a copy of this new term in `t'
 d*            # Duplicate and multiply (square)
 li<B          # Load i; if i<(t^2), execute B
] d sB x       # Duplicate, store function as `B', and execute
               # Loop ends when t^2 is less than i
[              # Begin macro definition
 lt            # Load t, our test term
 1+            # Increment
 d st          # Store a copy of this new term in `t'
 d*            # Duplicate and multiply (square)
 li!<A         # Load i; if i>=(t^2), execute A
] d sA x       # Duplicate, store function as `A', and execute
               # Loop ends when t^2 == i+1
lt 1- f        # Load t, decrement, and dump stack
removed superfluous code (yay!)
Source Link
juh
  • 983
  • 7
  • 15

dc, 4654 bytes

dc -e"?[fq]s0d0=0d1=0dsist[lt1dsist[lt2/dstd*li<B]dsBx[lt1+dstd*li!<A]dsAxlt1-dstd*li<A]dsAxltf"f"

Spaced out and explained:

?               # TakeThe inputidea fromhere stdin
[fq]s0is to start with the input and reduce it #quickly Defineuntil ait functionis
 to deal with input of 0 or 1
d 0=0      # less than what #we Ifwant, inputthen isincrement 0,it dumpuntil stackit's andjust quitright
d? 1=0             # IfTake input is 1,from dittostdin
d si st        # Duplicate input, store in `i' and in `t'
[              # Begin macro definition (when I write in dc, "macro"=="function")
 lt            # Load `t'estt, our test term
 1-2/            # Decrement,Divide sincet lastby roundtwo
 didn'td workst or we'd be done by now    # Store a copy of this new term in `t'
 dd* st           # Duplicate and multiply (square)
 li<B          # Load i; if i<(t^2), thenexecute B
] d sB x       # Duplicate, store copyfunction as `B', and execute
               # Loop ends when t^2 is less than i
[              # Begin macro definition
 lt            # Load t, our test term
 1+            # Increment
 d st          # Store a copy of this new term in `t'
 d*            # Duplicate  and multiply (square)
 li<A li!<A         # Load `i';i; if i<i>=(t^2), execute A
] d sA x       # Duplicate, store function as `A', and execute
               # Loop ends when i>=(t^2); t is the number we== wanti+1
lt f  1- f        # Load t, decrement, and dump stack

dc, 46 bytes

dc -e"?[fq]s0d0=0d1=0dsist[lt1-dstd*li<A]dsAxltf"

Spaced out and explained:

?              # Take input from stdin
[fq]s0         # Define a function to deal with input of 0 or 1
d 0=0          # If input is 0, dump stack and quit
d 1=0          # If input is 1, ditto
d si st        # Duplicate input, store in `i' and in `t'
[              # Begin macro definition (when I write in dc, "macro"=="function")
 lt            # Load `t'est term
 1-            # Decrement, since last round didn't work or we'd be done by now
 d st          # Duplicate, then store copy as new `t'
 d*            # Duplicate  and multiply (square)
 li<A          # Load `i'; if i<(t^2), execute A
] d sA x       # Duplicate, store function as `A', and execute
               # Loop ends when i>=(t^2); t is the number we want
lt f           # Load t and dump stack

dc, 54 bytes

dc -e"?dsist[lt2/dstd*li<B]dsBx[lt1+dstd*li!<A]dsAxlt1-f"

Spaced out and explained:

               # The idea here is to start with the input and reduce it quickly until it is
               # less than what we want, then increment it until it's just right
?              # Take input from stdin
d si st        # Duplicate input, store in `i' and in `t'
[              # Begin macro definition (when I write in dc, "macro"=="function")
 lt            # Load t, our test term
 2/            # Divide t by two
 d st          # Store a copy of this new term in `t'
 d*            # Duplicate and multiply (square)
 li<B          # Load i; if i<(t^2), execute B
] d sB x       # Duplicate, store function as `B', and execute
               # Loop ends when t^2 is less than i
[              # Begin macro definition
 lt            # Load t, our test term
 1+            # Increment
 d st          # Store a copy of this new term in `t'
 d*            # Duplicate and multiply (square)
 li!<A         # Load i; if i>=(t^2), execute A
] d sA x       # Duplicate, store function as `A', and execute
               # Loop ends when t^2 == i+1
lt 1- f        # Load t, decrement, and dump stack
Source Link
juh
  • 983
  • 7
  • 15

dc, 46 bytes

dc -e"?[fq]s0d0=0d1=0dsist[lt1-dstd*li<A]dsAxltf"

Spaced out and explained:

?              # Take input from stdin
[fq]s0         # Define a function to deal with input of 0 or 1
d 0=0          # If input is 0, dump stack and quit
d 1=0          # If input is 1, ditto
d si st        # Duplicate input, store in `i' and in `t'
[              # Begin macro definition (when I write in dc, "macro"=="function")
 lt            # Load `t'est term
 1-            # Decrement, since last round didn't work or we'd be done by now
 d st          # Duplicate, then store copy as new `t'
 d*            # Duplicate  and multiply (square)
 li<A          # Load `i'; if i<(t^2), execute A
] d sA x       # Duplicate, store function as `A', and execute
               # Loop ends when i>=(t^2); t is the number we want
lt f           # Load t and dump stack