I don't have an SO account, so go ahead and post that. While I'm at it,
I shaved a character off the original by noting that the length of the
circle is half the diameter for even lengths. This doesn't hold for odd
lengths, so it won't help there.

c=:({&' *',&":2*+/@,%*:@#)@:>_2{.\|@j./~@i:@<:

Marshall

On Mon, Jul 22, 2013 at 10:07:57PM -0400, Dan Bron wrote:
> Oh! Nice.  If you have an account on SO feel free to post it, I'll vote it
> up past my original. If you don't, I'll replace mine with yours and link to
> your message on the Forum.
> 
> -Dan
> 
> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]] On Behalf Of Marshall
> Lochbaum
> Sent: Monday, July 22, 2013 9:38 PM
> To: [email protected]
> Subject: Re: [Jprogramming] Code golf: approximate pi
> 
> I haven't been able to shorten your code. But here's a version which handles
> odd numbers, at the cost of only two characters:
> 
> c=:*:({&' *'@],&":2%(%+/@,))]>(|@j./~2&|#])@i:@<:
> 
> Rather than filtering by position (_2{.\]), we filter the x values by
> oddness (2&|#]).
> 
> Marshall
> 
> On Mon, Jul 22, 2013 at 07:54:21PM -0400, Dan Bron wrote:
> > There's a code golf challenge on StackOverflow [1] which asks us to 
> > generate an approximation of a circle in ASCII (i.e. using integral 
> > Cartesian
> > coordinates) and a corresponding estimation of pi.
> > 
> > For example, the 8th order circle in ASCII has 15 rows and 8 columns 
> > and gives an approximate value of pi of 3.125.  The detailed 
> > specifications of input, output, and edge cases can be found at 
> > http://stackoverflow.com/questions/2440314/code-golf-day/ but the 
> > general concept is take positive integer as an input, and output the 
> > circle with its corresponding estimate of pi. Like this:
> > 
> >        c 8
> >         *******    
> >      ************* 
> >     ***************
> >     ***************
> >     ***************
> >     ***************
> >      ************* 
> >         *******    
> >     3.125          
> > 
> > This is a code golf challenge. Most common programming languages can 
> > get it down to ~100 characters.  The shortest solution, outside of J, 
> > is written in bc (the command-line calculator) and weighs in at 88
> characters.
> > 
> > Without too much effort, I put together a J solution of 47 characters 
> > (a little over half the length of the previous champion)*.
> > 
> > Uncompressed, the verb might look like this:
> > 
> >     c =: verb define
> >             pythag   =. y > | j./~ i:y-1    NB.  r^2 > x^2 + y^2
> >             squished =. _2 {.\ pythag       NB.  Odd rows only
> >             piApx    =. (2 * +/ , squished) %  y*y
> >             (squished { ' *') , ": piApx
> >     )
> > 
> > Here it is in its compressed form:
> > 
> >     c=:*:({&' *'@],&":2%(%+/@,))_2{.\]>|@j./~@i:@<: 
> > 
> > Can you pare it down at all? Perhaps using a different approach?
> > 
> > Note also that there is extra credit available for a solution which 
> > handles odd inputs (i.e. odd numbers) appropriately, which this version
> doesn't.
> > 
> > -Dan
> > 
> > * The 47 characters includes the assignment to a name, so the function 
> > can be reused.
> >   
> > 
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
> 
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to