13
3
You may write a program or function that receives an odd, positive integer n
, where n >= 3
, as either a function argument, command line arguments, or on STDIN (or equivalent for your system), and prints to STDOUT (or system equivalent) an ASCII spiral that spins inward clockwise where the top edge is exactly n
characters long. The first right edge should be n+1
characters long, obviously. For instance,
Input:
11
Output:
***********
*
********* *
* * *
* ***** * *
* * * * *
* * * * * *
* * *** * *
* * * *
* ******* *
* *
***********
The catches:
- Your program must use no more than
O(log n)
memory. - Your program may only print the characters
*
(ASCII 42),(ASCII 32),
<CR>
(ASCII 13) and<LF>
(ASCII 10). - Your program must print the string, not return it from the function.
- The Big-O restriction is only on memory, there are no restrictions on runtime.
- A trailing newline is optional.
- If your language does not support large integer types, you do not have to support higher than what it does support, but you may not use this as a trick to say "oh, well, I don't have to support above X so I can just make a huge array the maximum size every time"
Standard loopholes are banned, as usual.
2I don't believe this is possible. One cannot store the input
n
in O(1) memory. – xnor – 2015-07-01T20:07:52.127@xnor "O(1) constitutes a constant memory usage. So amount of input is inconsequential" - If input n fits in integer, then I'm sure it can be coded into a constant memory usage. – André – 2015-07-01T20:10:08.127
1Storing the input
n
takeslog n
bits. Asn
gets larger, so does the space needed to store it. Are you perhaps saying to do this with a limited number of variables? – xnor – 2015-07-01T20:14:23.960Or, alternatively, is there a limit on
n
? – Sp3000 – 2015-07-01T20:17:47.467I think he's saying you can't store the entire output at once, then just print it all at once because that will get larger. You probably have to recursively print it. – Jacob – 2015-07-01T20:23:28.233
@Sp3000 @durron597 I think capping
n
opens another can of worms. One could just make a table hardcoding every possible spiral, though that's not golfy. Or, store the whole spiral in memory, since its size it bounded by a constant. – xnor – 2015-07-01T20:25:48.180@xnor I think O(log n) memory is the only way to do this. Feh – durron597 – 2015-07-01T20:28:10.340
@durron597 Good idea, O(log n) should do what you intend. – xnor – 2015-07-01T20:29:16.640
@xnor On the other hand, if
n
is not capped, languages without standard arbitrary size int types will be pretty much out of luck. Well, I guess I could include some kind of big int implementation in a C solution, but it would certainly put it at a big disadvantage against languages who are typically about equally verbose. – Reto Koradi – 2015-07-01T21:35:16.067Do we assume a trans-dichotomous machine model? – FUZxxl – 2015-07-02T09:20:01.460
@FUZxxl that's fine. – durron597 – 2015-07-02T13:01:59.790