Tuesday, March 16, 2010

Code Golf - π day

Programmer Question

The Challenge



The shortest code by character count to display a representation of a circle of radius R using the *character, followed by an approximation of π.



Input is a single number, R.



Since most computers seem to have almost 2:1 ratio you should only output lines where y is odd. This means that when R is odd you should print R-1 lines. There is a new testcase for R=13 to clarify.



eg.



Input
5
Output Correct Incorrect

3 ******* 4 *******
1 ********* 2 *********
-1 ********* 0 ***********
-3 ******* -2 *********
2.56 -4 *******
3.44


Edit: Due to widespread confusion caused by odd values of R, any solutions that pass the 4 test cases given below will be accepted



The approximation of π is given by dividing twice the number of * characters by R².

The approximation should be correct to at least 6 significant digits.

Leading or trailing zeros are permitted, so for example any of 3, 3.000000, 003 is accepted for the inputs of 2 and 4.



Code count includes input/output (i.e., full program).



Test Cases



Input
2
Output
***
***
3.0

Input
4
Output
*****
*******
*******
*****
3.0

Input
8
Output
*******
*************
***************
***************
***************
***************
*************
*******
3.125

Input
10
Output
*********
***************
*****************
*******************
*******************
*******************
*******************
*****************
***************
*********
3.16


Bonus Test Case



Input
13
Output

*************
*******************
*********************
***********************
*************************
*************************
*************************
*************************
***********************
*********************
*******************
*************
2.98224852071


Find the answer here

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails