Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Spiral Numbers

Name: Anonymous 2009-05-01 20:05

Hello /prog/, I have a challenge for you.

It's called spiral numbers.  Being the expert programmers you are you've probably already have heard of it, but if not it goes a little something like this:

Given two positive integers a and b, output on the screen a rectangle with width = b numbers and height = a rows, containing the numbers from 1 to a*b in a spiraling fashion, as shown below.  For a = 4 and b = 3, the output is:

    1    2    3
    10    11    4
    9    12    5
    8    7    6

The columns must be properly n+1 positions, where n is the number of digits in the decimal representation of a *b.

Write a program that prompts for a and b and then displays the appropriate rectangle of numbers.

Example:
    If a = 3 and b = 4, the above rectangle should be

    1    2    3    4
    10    11    12    5
    9    8    7    6


Here's some more examples from my own implementation written in snake tongue.



Have fun!
H:\Documents\Python\Program of the week>"april15 Spiral.py"
Width:8
Height:12
1       2       3       4       5       6       7       8
36      37      38      39      40      41      42      9
35      64      65      66      67      68      43      10
34      63      84      85      86      69      44      11
33      62      83      96      87      70      45      12
32      61      82      95      88      71      46      13
31      60      81      94      89      72      47      14
30      59      80      93      90      73      48      15
29      58      79      92      91      74      49      16
28      57      78      77      76      75      50      17
27      56      55      54      53      52      51      18
26      25      24      23      22      21      20      19

Name: Anonymous 2009-05-02 13:29

>>19
More like
~ $ ./9 << HERE
10000
10000
HERE
Segmentation fault

because you allocate huge amounts of memory without checking.

Here's a OMG OPTIMIZED version of >>14, benchmarks first:
~ $ gcc -std=c99 -O3 9.c -o 9 -Wall
9.c: In function 'main':
9.c:28: warning: operation on 'direction' may be undefined
~ $ time ./9 > /dev/null << HERE
7000
7000
HERE

real    0m8.192s
user    0m7.716s
sys    0m0.373s
~ $ gcc -std=c99 -O3 31.c -o 31 -Wall
~ $ time ./31 7000 7000 > /dev/null

real    0m6.938s
user    0m6.866s
sys    0m0.037s


And the code:
#include <stdio.h>
#include <stdlib.h>

int main(int w, char **v)
{
    if(w != 3) return 1;
    char *e;
    long a = strtol(v[1], &e, 0);
    if(*e || !*v[1] || a < 0) return 2;
    long b = strtol(v[2], &e, 0);
    if(*e || !*v[2] || b < 0) return 3;

    for(long ai = 0; ai < a; ai++) {
        long c = 1, aq = a - 1, bq = b - 1, aiq = ai, biq = 0;
        for(long bi= 0; bi < b; bi++, biq++) {
            if(aiq > aq || biq > bq)
            {
                aq += 2;
                bq += 2;
                aiq++;
                biq++;
                c -= 2 * aq + 2 * bq;
            } else if(aiq != 0 && biq != 0 && aiq < aq && biq < bq) {
                c += 2 * aq + 2 * bq;
                aiq--;
                biq--;
                aq -= 2;
                bq -= 2;
            }

            long r;
            if(aiq == 0) {
                r = c + biq;
            } else  if(biq == bq) {
                r = c + bq + aiq;
            } else if(aiq == aq) {
                r = c + bq + aq + (bq - biq);
            } else if(biq == 0) {
                r = c + bq + aq + bq + (aq - aiq);
            } else {
                return 5;
            }

            if(printf("%ld\t", r) < 0) return 4;
        }
        if(printf("\n") < 0) return 4;
    }
    if(fflush(stdout) != 0) return 4;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List