Madmouse

Ulam's spiral generator for number enthusiasts

Oct 22nd, 2014
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.60 KB | None | 0 0
  1.  
  2. // ----------------------------------------------------------------------------
  3. // "THE BEER-WARE LICENSE" (Revision 43):
  4. // <[email protected]> wrote this file. As long as you retain this notice you
  5. // can do whatever you want with this stuff. If we meet some day, and you think
  6. // this stuff is worth it, you can buy me a beer in return Aaron R. Yool
  7. // ----------------------------------------------------------------------------
  8.  
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <math.h>
  14.  
  15. typedef enum{false, true = !false} bool;
  16.  
  17. usage(const char *name)
  18. {
  19.     printf("Usage: %s <grid size> <origin > 0>\n", name);
  20.     exit(-1);
  21. }
  22.  
  23. cursor(unsigned int x, unsigned int y)
  24. {
  25.     printf("\033[%i;%iH", y, x);
  26. }
  27.  
  28. color(unsigned int quality, unsigned int color)
  29. {
  30.     printf("\033[%i;%im", quality, color);
  31. }
  32.  
  33. clear_screen()
  34. {
  35.     printf("\033[1;1H\033[2J");
  36. }
  37.  
  38. int digits_needed(long double n)
  39. {
  40.     int i=0, c;
  41.     for(c=n;c>0;c=c/10)i++;
  42.     return i;
  43. }
  44.  
  45. // corners work like this where o = 1 and n = 10:
  46. //
  47. // f(n) = n^2-n+o
  48. // 1, 3, 7, 13, 21, 31, 43, 57, 73, 91
  49. // R  L  R   L   R   L   R   L   R   L
  50. //
  51. // f(n) = n^2+o
  52. // 2, 5, 10, 17, 26, 37, 50, 65, 82, 101
  53. // U  D   U   D   U   D   U   D   U    D
  54.  
  55. #define RIGHT 0x1
  56. #define LEFT 0x2
  57.  
  58. #define UP 0x3
  59. #define DOWN 0x4
  60.  
  61. bool crf(long double n, long double o, long double x)
  62. {
  63.     long double f, i = n; n = 1;
  64.     for(i;i > 0;--i)
  65.     {
  66.         f = 4*powl(n,2)-3*n+o;
  67.         if(x == f) return true;
  68.         n++;
  69.     }
  70.     return false;
  71. }
  72.  
  73. bool clf(long double n, long double o, long double x)
  74. {
  75.     long double f, i = n; n = 1;
  76.     for(i;i > 0;--i)
  77.     {
  78.         f = 4*powl(n,2)+n+o;
  79.         if(x == f) return true;
  80.         n++;
  81.     }
  82.     return false;
  83. }
  84.  
  85. bool cuf(long double n, long double o, long double x)
  86. {
  87.     long double f, i = n; n = 1;
  88.     for(i;i > 0;--i)
  89.     {
  90.         f = 4*powl(n,2)-n+o;
  91.         if(x == f) return true;
  92.         n++;
  93.     }
  94.     return false;
  95. }
  96.  
  97. bool cdf(long double n, long double o, long double x)
  98. {
  99.     long double f, i = n; n = 1;
  100.     for(i;i > 0;--i)
  101.     {
  102.         f = 4*powl(n,2)+3*n+o;
  103.         if(x == f) return true;
  104.         n++;
  105.     }
  106.     return false;
  107. }
  108.  
  109.  
  110. char crdf(long double n, long double o, long double x)
  111. {
  112.     long double f, i = n; n = 1;
  113.     for(i;i > 0;--i)
  114.     {
  115.         f = powl(n,2)-n+o;
  116.         if(x == f) return fmod(n, 2) ? RIGHT : LEFT;
  117.         n++;
  118.     }
  119.     return 0;
  120. }
  121.  
  122.  
  123. char cldf(long double n, long double o, long double x)
  124. {
  125.     long double f, i = n; n = 1;
  126.     for(i;i > 0;--i)
  127.     {
  128.         f = powl(n,2)+o;
  129.         if(x == f) return fmod(n, 2) ? UP : DOWN;
  130.         n++;
  131.     }
  132.     return 0;
  133. }
  134.  
  135. bool prime(long double x)
  136. {
  137.     long double i;
  138.     if(x == 2) return true;
  139.     else if(x < 2 || fmod(x, 2) == 0) return false;
  140.     for(i=3;i<=sqrtl(x);i++)
  141.     {
  142.         if(fmod(x, i) == 0) return false;
  143.     }
  144.     return true;
  145. }
  146.  
  147. generate_grid(long double n, long double o)
  148. {
  149.     long double direction, i, c, cld, crd, x = fmod(n, 2) ? n/2 : n/2-1, y=n/2, digits = digits_needed(n*n+o);
  150.     clear_screen();
  151.    
  152.     for(c=0;c<n;c++)
  153.     for(i=0;i<=n;i++)
  154.     {
  155.         cursor((i*(digits+1))+1,c+3);
  156.         putchar('|');
  157.     }
  158.    
  159.     for(i=o;i<=n*n+o-1;i++)
  160.     {
  161.         crd = crdf(n, o, i);
  162.         cld = cldf(n, o, i);
  163.         if(prime(i)) color(4,31);
  164.         if(crd){direction = crd;color(1,102);}
  165.         else if(cld){direction = cld;color(1,43);}
  166.         else if(crf(n, o, i))color(1,44);
  167.         else if(clf(n, o, i))color(1,45);
  168.         else if(cuf(n, o, i))color(1,46);
  169.         else if(cdf(n, o, i))color(1,42);
  170.        
  171.         cursor((x*(digits+1))+2,3+y);
  172.        
  173.         for(c=digits-digits_needed(i);c>0;c--)
  174.             putchar('0');
  175.         printf("%i",i);
  176.         color(0,0);
  177.        
  178.         switch((int)direction)
  179.         {
  180.             case RIGHT: x++;break;
  181.             case LEFT: x--;break;
  182.             case UP: y--;break;
  183.             case DOWN: y++;break;
  184.             default: break;
  185.         }
  186.     }
  187.    
  188.     cursor(0,n+3);
  189. }
  190.  
  191. main(unsigned int count, const char **args)
  192. {
  193.     long double i = 0, f, n = 0, o = 0;
  194.    
  195.     if(count == 3 ? (args[1][0] >= '0' && args[1][0] <= '9') : false)
  196.         n = atoll(args[1]);
  197.     else
  198.         usage(args[0]);
  199.    
  200.     if(args[2][0] >= '0' && args[2][0] <= '9')
  201.         o = atoll(args[2]);
  202.     else
  203.         usage(args[0]);
  204.    
  205.     if(o<1) usage(args[0]);
  206.    
  207.     generate_grid(n,o);
  208.     printf("\n\no = %.0LF\n", o);
  209.    
  210.     // right diagonal from origin
  211.     color(1,32);
  212.     printf("f1(n) = n^2-n+o\n");
  213.     i = n+100;n = 1;
  214.     for(i;i > 0;--i)
  215.     {
  216.         f = powl(n,2)-n+o;
  217.         color(0,0);
  218.         if(prime(f)) color(1,31);
  219.         i > 1 ? printf("%.0LF, ", f) : printf("%.0LF\n\n", f);
  220.         n++;
  221.         color(0,0);
  222.     }
  223.     color(0,0);
  224.    
  225.     // left diagonal from origin
  226.     color(1,33);
  227.     printf("f2(n) = n^2+o\n");
  228.     i = n-2;n = 1;
  229.     for(i;i > 0;--i)
  230.     {
  231.         f = powl(n,2)+o;
  232.         color(0,0);
  233.         if(prime(f)) color(1,31);
  234.         i > 1 ? printf("%.0LF, ", f) : printf("%.0LF\n\n", f);
  235.         n++;
  236.     }
  237.     color(0,0);
  238.    
  239.     // right of origin
  240.     color(1,34);
  241.     printf("f3(n) = 4n^2-3n+o\n");
  242.     i = n/2;n = 1;
  243.     for(i;i > 0;--i)
  244.     {
  245.         f = 4*powl(n,2)-3*n+o;
  246.         color(0,0);
  247.         if(prime(f)) color(1,31);
  248.         i > 1 ? printf("%.0LF, ", f) : printf("%.0LF\n\n", f);
  249.         n++;
  250.     }
  251.     color(0,0);
  252.    
  253.     // down from origin
  254.     color(0,32);
  255.     printf("f4(n) = 4n^2+3n+o\n");
  256.     i = n-2;n = 1;
  257.     for(i;i > 0;--i)
  258.     {
  259.         f = 4*powl(n,2)+3*n+o;
  260.         color(0,0);
  261.         if(prime(f)) color(1,31);
  262.         i > 1 ? printf("%.0LF, ", f) : printf("%.0LF\n\n", f);
  263.         n++;
  264.     }
  265.     color(0,0);
  266.    
  267.     // up from origin
  268.     color(1,36);
  269.     printf("f5(n) = 4n^2-n+o\n");
  270.     i = n;n = 1;
  271.     for(i;i > 0;--i)
  272.     {
  273.         f = 4*powl(n,2)-n+o;
  274.         color(0,0);
  275.         if(prime(f)) color(1,31);
  276.         i > 1 ? printf("%.0LF, ", f) : printf("%.0LF\n\n", f);
  277.         n++;
  278.     }
  279.     color(0,0);
  280.    
  281.     // to the left of origin
  282.     color(1,35);
  283.     printf("f6(n) = 4n^2+n+o\n");
  284.     i = n-2;n = 1;
  285.     for(i;i > 0;--i)
  286.     {
  287.         f = 4*powl(n,2)+n+o;
  288.         color(0,0);
  289.         if(prime(f)) color(1,31);
  290.         i > 1 ? printf("%.0LF, ", f) : printf("%.0LF\n\n", f);
  291.         n++;
  292.     }
  293.     color(0,0);
  294.    
  295.     return;
  296. }
Advertisement
Add Comment
Please, Sign In to add comment