All about flooble | fun stuff | Get a free chatterbox | Free JavaScript | Avatars    
perplexus dot info

Home > Algorithms
Spirals (Posted on 2003-08-21) Difficulty: 3 of 5
Write a program (or explain how to do it) that will display a descending "spiral" of NxN numbers, using constant space (no arrays allowed). For example, here's what the spiral looks like for N=10:
   99    98    97    96    95    94    93    92    91    90
   64    63    62    61    60    59    58    57    56    89
   65    36    35    34    33    32    31    30    55    88
   66    37    16    15    14    13    12    29    54    87
   67    38    17     4     3     2    11    28    53    86
   68    39    18     5     0     1    10    27    52    85
   69    40    19     6     7     8     9    26    51    84
   70    41    20    21    22    23    24    25    50    83
   71    42    43    44    45    46    47    48    49    82
   72    73    74    75    76    77    78    79    80    81

See The Solution Submitted by DJ    
Rating: 4.4167 (12 votes)

Comments: ( Back to comment list | You must be logged in to post comments.)
Solution FULL SOLUTION (in C) | Comment 1 of 11
apologies that my indentions don't come through the HTML (but I don't want to type the non-breaking space character ;-)

#include "stdafx.h"
#include "stdlib.h"

int F(int i, int j, int x)
{
if (i == 0)
{
return(x*x-j-1);
}
if (i == (x-1) )
{
return( (x-2) * x + j - x + 2);
}
if (j == (x-1) )
{
return( (x-1)* x - i);
}
if (j == 0)
{
return( (x-2) * (x-2) + i - 1) ;
}
return (F(i-1, j-1, x-2));
}

int main(int argc, char* argv[])
{
int i,j;
int x = atoi(argv[1]);

for (i=0; i {
for (j=0; j {
printf("%d\t",F(i,j,x));
}
printf("\n");
}

return 0;
}

  Posted by Your buddy on 2003-08-21 15:50:00
Please log in:
Login:
Password:
Remember me:
Sign up! | Forgot password


Search:
Search body:
Forums (0)
Newest Problems
Random Problem
FAQ | About This Site
Site Statistics
New Comments (3)
Unsolved Problems
Top Rated Problems
This month's top
Most Commented On

Chatterbox:
Copyright © 2002 - 2024 by Animus Pactum Consulting. All rights reserved. Privacy Information