Skip to main content
Stack Overflow
  1. About
  2. For Teams
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.

Return to Answer

added 89 characters in body
Source Link
Dan Andreatta
  • 3.7k
  • 1
  • 24
  • 15

In dc: 9388 and 9693 (削除) 9493 94 96 102 105 129 138 141 (削除ここまで) chars

Just in case, I am using OpenBSD and some supposedly non-portable extensions at this point.

93 chars. This is based on same formula as FORTRAN solution (slightly different results than test cases). Calculates X^2=R^2-Y^2 for every Y

[rdPr1-d0<p]sp1?dsMdd*sRd2%--
[dd*lRr-vddlMr-32rlpxRR42r2*lpxRRAP4*2+lN+sN2+dlM>y]
dsyx5klNlR/p

9688 chars. Iterative solution. Matches test cases. For every X and Y checks if X^2+Y^2<=R^2

[rdPr1-d0<p]sp[dd*lRr-vddlMr-32rlpxRR42r2*lpxRRAP4*2+lN+sN2+dlM>y]sy
1?dsMdd*sRd2%-1r-lyx5klNlRsY[0lM-[dd*lYd*+lRr(2*d5*32+PlN+sN1+dlM!<x]dsxxAPlY2+dsYlM>y]
dsyx5klNlR/p

To run dc pi.dc.

Here is an older annotated version:

# Routines to print '*' or ' '. If '*', increase the counter by 2
[lN2+sN42P]s1
[32P]s2
# do 1 row
# keeping I in the stack
[
 # X in the stack
 # Calculate X^2+Y^2 (leave a copy of X)
 dd*lYd*+ 
 #Calculate X^2+Y^2-R^2...
 lR-d
 # .. if <0, execute routine 1 (print '*')
 0>1
 # .. else execute routine 2 (print ' ')
 0!>2 
 # increment X..
 1+
 # and check if done with line (if not done, recurse)
 d lM!<x
]sx
# Routine to cycle for the columns
# Y is on the stack
[
 # push -X
 0lM- 
 # Do row
 lxx 
 # Print EOL
 10P
 # Increment Y and save it, leaving 2 copies
 lY 2+ dsY 
 # Check for stop condition
 lM >y
]sy
# main loop
# Push Input value
[Input:]n?
# Initialize registers
# M=rows
d sM
# Y=1-(M-(M%2))
dd2%-1r-sY
# R=M^2
d*sR
# N=0
0sN
[Output:]p
# Main routine
lyx
# Print value of PI, N/R
5klNlR/p

In dc: 93 and 96 (削除) 94 96 102 105 129 138 141 (削除ここまで) chars

93 chars. This is based on same formula as FORTRAN solution (slightly different results than test cases). Calculates X^2=R^2-Y^2 for every Y

[rdPr1-d0<p]sp1?dsMdd*sRd2%--
[dd*lRr-vddlMr-32rlpxRR42r2*lpxRRAP4*2+lN+sN2+dlM>y]
dsyx5klNlR/p

96 chars. Iterative solution. Matches test cases. For every X and Y checks if X^2+Y^2<=R^2

[rdPr1-d0<p]sp[dd*lRr-vddlMr-32rlpxRR42r2*lpxRRAP4*2+lN+sN2+dlM>y]sy
?dsMdd*sRd2%-1r-lyx5klNlR/p

To run dc pi.dc.

Here is an older annotated version:

# Routines to print '*' or ' '. If '*', increase the counter by 2
[lN2+sN42P]s1
[32P]s2
# do 1 row
# keeping I in the stack
[
 # X in the stack
 # Calculate X^2+Y^2 (leave a copy of X)
 dd*lYd*+ 
 #Calculate X^2+Y^2-R^2...
 lR-d
 # .. if <0, execute routine 1 (print '*')
 0>1
 # .. else execute routine 2 (print ' ')
 0!>2 
 # increment X..
 1+
 # and check if done with line (if not done, recurse)
 d lM!<x
]sx
# Routine to cycle for the columns
# Y is on the stack
[
 # push -X
 0lM- 
 # Do row
 lxx 
 # Print EOL
 10P
 # Increment Y and save it, leaving 2 copies
 lY 2+ dsY 
 # Check for stop condition
 lM >y
]sy
# main loop
# Push Input value
[Input:]n?
# Initialize registers
# M=rows
d sM
# Y=1-(M-(M%2))
dd2%-1r-sY
# R=M^2
d*sR
# N=0
0sN
[Output:]p
# Main routine
lyx
# Print value of PI, N/R
5klNlR/p

In dc: 88 and 93 (削除) 93 94 96 102 105 129 138 141 (削除ここまで) chars

Just in case, I am using OpenBSD and some supposedly non-portable extensions at this point.

93 chars. This is based on same formula as FORTRAN solution (slightly different results than test cases). Calculates X^2=R^2-Y^2 for every Y

[rdPr1-d0<p]sp1?dsMdd*sRd2%--
[dd*lRr-vddlMr-32rlpxRR42r2*lpxRRAP4*2+lN+sN2+dlM>y]
dsyx5klNlR/p

88 chars. Iterative solution. Matches test cases. For every X and Y checks if X^2+Y^2<=R^2

1?dsMdd*sRd2%--sY[0lM-[dd*lYd*+lRr(2*d5*32+PlN+sN1+dlM!<x]dsxxAPlY2+dsYlM>y]
dsyx5klNlR/p

To run dc pi.dc.

Here is an older annotated version:

# Routines to print '*' or ' '. If '*', increase the counter by 2
[lN2+sN42P]s1
[32P]s2
# do 1 row
# keeping I in the stack
[
 # X in the stack
 # Calculate X^2+Y^2 (leave a copy of X)
 dd*lYd*+ 
 #Calculate X^2+Y^2-R^2...
 lR-d
 # .. if <0, execute routine 1 (print '*')
 0>1
 # .. else execute routine 2 (print ' ')
 0!>2 
 # increment X..
 1+
 # and check if done with line (if not done, recurse)
 d lM!<x
]sx
# Routine to cycle for the columns
# Y is on the stack
[
 # push -X
 0lM- 
 # Do row
 lxx 
 # Print EOL
 10P
 # Increment Y and save it, leaving 2 copies
 lY 2+ dsY 
 # Check for stop condition
 lM >y
]sy
# main loop
# Push Input value
[Input:]n?
# Initialize registers
# M=rows
d sM
# Y=1-(M-(M%2))
dd2%-1r-sY
# R=M^2
d*sR
# N=0
0sN
[Output:]p
# Main routine
lyx
# Print value of PI, N/R
5klNlR/p
added 1 characters in body
Source Link
Dan Andreatta
  • 3.7k
  • 1
  • 24
  • 15

In dc: 9493 and 9796 (削除) 9694 96 102 105 129 138 141 (削除ここまで) chars

9493 chars. This is based on same formula as FORTRAN solution (slightly different results than test cases). Calculates X^2=R^2-Y^2 for every Y

[rdPr1-d0<p]sp1?dsMdd*sRd2%--
[dd*lRr-vddlMr-32rlpxRR42r2*lpxRR10P4*2+lN+sN2+dlM>y]32rlpxRR42r2*lpxRRAP4*2+lN+sN2+dlM>y]
dsyx5klNlR/p

9996 chars. Iterative solution. Matches test cases. For every X and Y checks if X^2+Y^2<=R^2

[rdPr1-d0<p]sp[dd*lRr-vddlMr-32rlpxRR42r2*lpxRR10P4*2+lN+sN2+dlM>y]sy32rlpxRR42r2*lpxRRAP4*2+lN+sN2+dlM>y]sy
?dsMdd*sRd2%-1r-lyx5klNlR/p

To run dc pi.dc.

Here is an older annotated version:

# Routines to print '*' or ' '. If '*', increase the counter by 2
[lN2+sN42P]s1
[32P]s2
# do 1 row
# keeping I in the stack
[
 # X in the stack
 # Calculate X^2+Y^2 (leave a copy of X)
 dd*lYd*+ 
 #Calculate X^2+Y^2-R^2...
 lR-d
 # .. if <0, execute routine 1 (print '*')
 0>1
 # .. else execute routine 2 (print ' ')
 0!>2 
 # increment X..
 1+
 # and check if done with line (if not done, recurse)
 d lM!<x
]sx
# Routine to cycle for the columns
# Y is on the stack
[
 # push -X
 0lM- 
 # Do row
 lxx 
 # Print EOL
 10P
 # Increment Y and save it, leaving 2 copies
 lY 2+ dsY 
 # Check for stop condition
 lM >y
]sy
# main loop
# Push Input value
[Input:]n?
# Initialize registers
# M=rows
d sM
# Y=1-(M-(M%2))
dd2%-1r-sY
# R=M^2
d*sR
# N=0
0sN
[Output:]p
# Main routine
lyx
# Print value of PI, N/R
5klNlR/p

In dc: 94 and 97 (削除) 96 102 105 129 138 141 (削除ここまで) chars

94 chars. This is based on same formula as FORTRAN solution (slightly different results than test cases). Calculates X^2=R^2-Y^2 for every Y

[rdPr1-d0<p]sp1?dsMdd*sRd2%--
[dd*lRr-vddlMr-32rlpxRR42r2*lpxRR10P4*2+lN+sN2+dlM>y]
dsyx5klNlR/p

99 chars. Iterative solution. Matches test cases. For every X and Y checks if X^2+Y^2<=R^2

[rdPr1-d0<p]sp[dd*lRr-vddlMr-32rlpxRR42r2*lpxRR10P4*2+lN+sN2+dlM>y]sy
?dsMdd*sRd2%-1r-lyx5klNlR/p

To run dc pi.dc.

Here is an older annotated version:

# Routines to print '*' or ' '. If '*', increase the counter by 2
[lN2+sN42P]s1
[32P]s2
# do 1 row
# keeping I in the stack
[
 # X in the stack
 # Calculate X^2+Y^2 (leave a copy of X)
 dd*lYd*+ 
 #Calculate X^2+Y^2-R^2...
 lR-d
 # .. if <0, execute routine 1 (print '*')
 0>1
 # .. else execute routine 2 (print ' ')
 0!>2 
 # increment X..
 1+
 # and check if done with line (if not done, recurse)
 d lM!<x
]sx
# Routine to cycle for the columns
# Y is on the stack
[
 # push -X
 0lM- 
 # Do row
 lxx 
 # Print EOL
 10P
 # Increment Y and save it, leaving 2 copies
 lY 2+ dsY 
 # Check for stop condition
 lM >y
]sy
# main loop
# Push Input value
[Input:]n?
# Initialize registers
# M=rows
d sM
# Y=1-(M-(M%2))
dd2%-1r-sY
# R=M^2
d*sR
# N=0
0sN
[Output:]p
# Main routine
lyx
# Print value of PI, N/R
5klNlR/p

In dc: 93 and 96 (削除) 94 96 102 105 129 138 141 (削除ここまで) chars

93 chars. This is based on same formula as FORTRAN solution (slightly different results than test cases). Calculates X^2=R^2-Y^2 for every Y

[rdPr1-d0<p]sp1?dsMdd*sRd2%--
[dd*lRr-vddlMr-32rlpxRR42r2*lpxRRAP4*2+lN+sN2+dlM>y]
dsyx5klNlR/p

96 chars. Iterative solution. Matches test cases. For every X and Y checks if X^2+Y^2<=R^2

[rdPr1-d0<p]sp[dd*lRr-vddlMr-32rlpxRR42r2*lpxRRAP4*2+lN+sN2+dlM>y]sy
?dsMdd*sRd2%-1r-lyx5klNlR/p

To run dc pi.dc.

Here is an older annotated version:

# Routines to print '*' or ' '. If '*', increase the counter by 2
[lN2+sN42P]s1
[32P]s2
# do 1 row
# keeping I in the stack
[
 # X in the stack
 # Calculate X^2+Y^2 (leave a copy of X)
 dd*lYd*+ 
 #Calculate X^2+Y^2-R^2...
 lR-d
 # .. if <0, execute routine 1 (print '*')
 0>1
 # .. else execute routine 2 (print ' ')
 0!>2 
 # increment X..
 1+
 # and check if done with line (if not done, recurse)
 d lM!<x
]sx
# Routine to cycle for the columns
# Y is on the stack
[
 # push -X
 0lM- 
 # Do row
 lxx 
 # Print EOL
 10P
 # Increment Y and save it, leaving 2 copies
 lY 2+ dsY 
 # Check for stop condition
 lM >y
]sy
# main loop
# Push Input value
[Input:]n?
# Initialize registers
# M=rows
d sM
# Y=1-(M-(M%2))
dd2%-1r-sY
# R=M^2
d*sR
# N=0
0sN
[Output:]p
# Main routine
lyx
# Print value of PI, N/R
5klNlR/p
added 4 characters in body
Source Link
Dan Andreatta
  • 3.7k
  • 1
  • 24
  • 15

In dc: 9694 and 97 (削除) 10296 102 105 129 138 141 (削除ここまで) chars

9694 chars. This is based on same formula as FORTRAN solution (slightly different results than test cases). Calculates X^2=R^2-Y^2 for every Y

[2+]s21[rdPr1-d0<p]sp1?dsMdd*sRd2%--sY[0lM
[dd*lRr-[dd*lYd*+lRvddlMr-0r0>2d5*32+PlN+sN1+dlM!<x]32rlpxRR42r2*lpxRR10P4*2+lN+sN2+dlM>y]
dsxx10PlY2+dsYlM>y]dsyx5klNlRdsyx5klNlR/p

99 chars. Iterative solution. Matches test cases. For every X and Y checks if X^2+Y^2<=R^2

[rdPr1-d0<p]sp[dd*lRr-vddlMr-32rlpxRR42r2*lpxRR10P4*2+lN+sN2+dlM>y]sy
?dsMdd*sRd2%-1r-lyx5klNlR/p

To run dc pi.dc.

Here is an older annotated version:

# Routines to print '*' or ' '. If '*', increase the counter by 2
[lN2+sN42P]s1
[32P]s2
# do 1 row
# keeping I in the stack
[
 # X in the stack
 # Calculate X^2+Y^2 (leave a copy of X)
 dd*lYd*+ 
 #Calculate X^2+Y^2-R^2...
 lR-d
 # .. if <0, execute routine 1 (print '*')
 0>1
 # .. else execute routine 2 (print ' ')
 0!>2 
 # increment X..
 1+
 # and check if done with line (if not done, recurse)
 d lM!<x
]sx
# Routine to cycle for the columns
# Y is on the stack
[
 # push -X
 0lM- 
 # Do row
 lxx 
 # Print EOL
 10P
 # Increment Y and save it, leaving 2 copies
 lY 2+ dsY 
 # Check for stop condition
 lM >y
]sy
# main loop
# Push Input value
[Input:]n?
# Initialize registers
# M=rows
d sM
# Y=1-(M-(M%2))
dd2%-1r-sY
# R=M^2
d*sR
# N=0
0sN
[Output:]p
# Main routine
lyx
# Print value of PI, N/R
5klNlR/p

In dc: 96 and 97 (削除) 102 105 129 138 141 (削除ここまで) chars

96 chars. This is based on same formula as FORTRAN solution (slightly different results than test cases). Calculates X^2=R^2-Y^2 for every Y

[2+]s21?dsMdd*sRd2%--sY[0lM-[dd*lYd*+lR-0r0>2d5*32+PlN+sN1+dlM!<x]
dsxx10PlY2+dsYlM>y]dsyx5klNlR/p

99 chars. Iterative solution. Matches test cases. For every X and Y checks if X^2+Y^2<=R^2

[rdPr1-d0<p]sp[dd*lRr-vddlMr-32rlpxRR42r2*lpxRR10P4*2+lN+sN2+dlM>y]sy
?dsMdd*sRd2%-1r-lyx5klNlR/p

To run dc pi.dc.

Here is an older annotated version:

# Routines to print '*' or ' '. If '*', increase the counter by 2
[lN2+sN42P]s1
[32P]s2
# do 1 row
# keeping I in the stack
[
 # X in the stack
 # Calculate X^2+Y^2 (leave a copy of X)
 dd*lYd*+ 
 #Calculate X^2+Y^2-R^2...
 lR-d
 # .. if <0, execute routine 1 (print '*')
 0>1
 # .. else execute routine 2 (print ' ')
 0!>2 
 # increment X..
 1+
 # and check if done with line (if not done, recurse)
 d lM!<x
]sx
# Routine to cycle for the columns
# Y is on the stack
[
 # push -X
 0lM- 
 # Do row
 lxx 
 # Print EOL
 10P
 # Increment Y and save it, leaving 2 copies
 lY 2+ dsY 
 # Check for stop condition
 lM >y
]sy
# main loop
# Push Input value
[Input:]n?
# Initialize registers
# M=rows
d sM
# Y=1-(M-(M%2))
dd2%-1r-sY
# R=M^2
d*sR
# N=0
0sN
[Output:]p
# Main routine
lyx
# Print value of PI, N/R
5klNlR/p

In dc: 94 and 97 (削除) 96 102 105 129 138 141 (削除ここまで) chars

94 chars. This is based on same formula as FORTRAN solution (slightly different results than test cases). Calculates X^2=R^2-Y^2 for every Y

[rdPr1-d0<p]sp1?dsMdd*sRd2%--
[dd*lRr-vddlMr-32rlpxRR42r2*lpxRR10P4*2+lN+sN2+dlM>y]
dsyx5klNlR/p

99 chars. Iterative solution. Matches test cases. For every X and Y checks if X^2+Y^2<=R^2

[rdPr1-d0<p]sp[dd*lRr-vddlMr-32rlpxRR42r2*lpxRR10P4*2+lN+sN2+dlM>y]sy
?dsMdd*sRd2%-1r-lyx5klNlR/p

To run dc pi.dc.

Here is an older annotated version:

# Routines to print '*' or ' '. If '*', increase the counter by 2
[lN2+sN42P]s1
[32P]s2
# do 1 row
# keeping I in the stack
[
 # X in the stack
 # Calculate X^2+Y^2 (leave a copy of X)
 dd*lYd*+ 
 #Calculate X^2+Y^2-R^2...
 lR-d
 # .. if <0, execute routine 1 (print '*')
 0>1
 # .. else execute routine 2 (print ' ')
 0!>2 
 # increment X..
 1+
 # and check if done with line (if not done, recurse)
 d lM!<x
]sx
# Routine to cycle for the columns
# Y is on the stack
[
 # push -X
 0lM- 
 # Do row
 lxx 
 # Print EOL
 10P
 # Increment Y and save it, leaving 2 copies
 lY 2+ dsY 
 # Check for stop condition
 lM >y
]sy
# main loop
# Push Input value
[Input:]n?
# Initialize registers
# M=rows
d sM
# Y=1-(M-(M%2))
dd2%-1r-sY
# R=M^2
d*sR
# N=0
0sN
[Output:]p
# Main routine
lyx
# Print value of PI, N/R
5klNlR/p
edited body
Source Link
Dan Andreatta
  • 3.7k
  • 1
  • 24
  • 15
Loading
added 34 characters in body
Source Link
Dan Andreatta
  • 3.7k
  • 1
  • 24
  • 15
Loading
added 327 characters in body
Source Link
Dan Andreatta
  • 3.7k
  • 1
  • 24
  • 15
Loading
Fixed a bug and shortened
Source Link
Dan Andreatta
  • 3.7k
  • 1
  • 24
  • 15
Loading
deleted 18 characters in body
Source Link
Dan Andreatta
  • 3.7k
  • 1
  • 24
  • 15
Loading
added 1115 characters in body
Source Link
Dan Andreatta
  • 3.7k
  • 1
  • 24
  • 15
Loading
added 18 characters in body
Source Link
Dan Andreatta
  • 3.7k
  • 1
  • 24
  • 15
Loading
Post Made Community Wiki
Source Link
Dan Andreatta
  • 3.7k
  • 1
  • 24
  • 15
Loading

AltStyle によって変換されたページ (->オリジナル) /