[ create a new paste ] login | about

Link: http://codepad.org/y5S3MDnP    [ raw code | output | fork ]

novatech - C, pasted on Jan 13:
/*
numeric diamond algorithm..
not sure if this is the fastest algorithm
novatech - http://wan.pengganas.net
*/
#include <stdio.h>
#include <math.h>
void diamond(int size) {
 int i,j,k,x;
 x = sqrt(size);
 int left,right=-1*x+1;
 for (i = 1; i < x*2 ;i++)
  {
   left = (i>x)?x*2-i:i;
   for (k=0;k<=x-left;k++) printf("   ");
   right = (i>x)?right+1:right+x;
   printf("%d",right);
   for (j = 1;j < left;j++) {
   printf("    %d",right-(x-1)*j);
   }
   printf("\n");
  }
}
int main() {
//input must be n^2
diamond(9);
diamond(36);
diamond(100);
diamond(121);
return 0;
}


Output:
         1
      4    2
   7    5    3
      8    6
         9
                  1
               7    2
            13    8    3
         19    14    9    4
      25    20    15    10    5
   31    26    21    16    11    6
      32    27    22    17    12
         33    28    23    18
            34    29    24
               35    30
                  36
                              1
                           11    2
                        21    12    3
                     31    22    13    4
                  41    32    23    14    5
               51    42    33    24    15    6
            61    52    43    34    25    16    7
         71    62    53    44    35    26    17    8
      81    72    63    54    45    36    27    18    9
   91    82    73    64    55    46    37    28    19    10
      92    83    74    65    56    47    38    29    20
         93    84    75    66    57    48    39    30
            94    85    76    67    58    49    40
               95    86    77    68    59    50
                  96    87    78    69    60
                     97    88    79    70
                        98    89    80
                           99    90
                              100
                                 1
                              12    2
                           23    13    3
                        34    24    14    4
                     45    35    25    15    5
                  56    46    36    26    16    6
               67    57    47    37    27    17    7
            78    68    58    48    38    28    18    8
         89    79    69    59    49    39    29    19    9
      100    90    80    70    60    50    40    30    20    10
   111    101    91    81    71    61    51    41    31    21    11
      112    102    92    82    72    62    52    42    32    22
         113    103    93    83    73    63    53    43    33
            114    104    94    84    74    64    54    44
               115    105    95    85    75    65    55
                  116    106    96    86    76    66
                     117    107    97    87    77
                        118    108    98    88
                           119    109    99
                              120    110
                                 121


Create a new paste based on this one


Comments: