Results 1 to 10 of 10

Thread: spirals - Ulam's spiral

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370

    spirals - Ulam's spiral

    about 15 years ago, I wrote this code which draws Ulam's spiral.

    If you start with one, put a series of numbers in a counter-clockwise spiral around it, then highlight the numbers that are prime, you get an interesting effect - prime numbers appear as 'lines'. Nobody can explain it.
    Code:
         10  . . . .
          9 2 3   .
          8 1 4   .
          7 6 5   . 
        .   .   .    .

    Anyway, as a sort-of followup to the sprial discussion attached is the Turbo C code (V 2.0) to make the spiral. It ran on my XP box under the command prompt. Amazing....

    Here is the code:
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <graphics.h>
    #include <conio.h>
    #include <time.h>
    #include <dos.h>
    #define ZERO 239
    #define MAX_PT 479
    typedef struct XYcoord{
          int x;
          int y;
    };
    static struct XYcoord XY;
    
    
    void display_spiral(FILE *);
    void init_graph(void);
    void plot(int);
    void wait(void);
    void iterate(FILE *);
    
    unsigned long toint(double);
    double todbl(unsigned long);
    void test(FILE *);
    void calc_primes(FILE *);
    void add(unsigned long,FILE *in);
    static unsigned long counter=0;
    static unsigned long base_list[201];
    
    static unsigned long active_base=0;
    static const unsigned base_max=200;
    static unsigned long prime;
    
    int main(void){
            FILE *out;
    	test(out=fopen("prime.txt","wb+"));
    	calc_primes(out);
    	fflush(out);
            display_spiral(out);
    	fclose(out);
    	return 0;
    }
    void calc_primes(FILE *out){
    	register unsigned long i,j,k;
    	unsigned long max;
    	prime=1;
    	add(1,out);
            base_list[0]=2;
    	add(2,out);
            base_list[1]=3;
    	add(3,out);
    	prime=0;
    	add(4,out);
    	prime=1;
            base_list[2]=5;
    	add(5,out);
            active_base=2;
    	max=479;
    	max*=479;
    	for (i=6;counter<max;i++,counter++){
    	     if(!i%2){
                 	prime=0;
                 	add(i,out);
                 	continue;
                 }
                 k=toint(sqrt(todbl(i)))+1;
                 prime=1;
    	     for(j=0;j<=active_base && prime ;j++){
    		   if(i%base_list[j]==0) prime=0;}
                 j=base_list[active_base]+2;
    	     for(;j<=k && prime;j+=2) {
    		   if(i%j==0) prime=0; }
                 add(i,out);
    	}
    	printf("processed numbers=%ld\n",counter);
    	return;
    }
    unsigned long toint(double z){
         char tmp[20];
         sprintf(tmp,"%.0f",z);
         return atol(tmp);
    }
    
    void test(FILE *tst){
        if(tst==NULL){
          perror("Error opening file");
          exit(EXIT_FAILURE);
          return;
          }
    }
    void add(unsigned long i, FILE *out){
    
          char output=0x00;
          if(prime){
    
             if(active_base < base_max){
             	  active_base++;
    	          base_list[active_base]=i;
             }
          }
          if(prime==1) output=0x01;
          if(prime==0) output=0x00;
          fprintf(out,"%c",output);
          return;
    }
    double todbl(unsigned long i){
    	char tmp[20];
    	sprintf(tmp,"%lu",i);
            return atof(tmp);
    }
    void display_spiral(FILE *src){
    	char output;
    	int i;
    
        	fseek(src,0L,SEEK_SET);
        	init_graph();
        	XY.x=ZERO;
        	XY.y=ZERO;
    	plot(1);
    	fread(&output,1,1,src);
    	iterate(src);
            wait();
            return;
    }
    void iterate(FILE *src){
    	int i;
    	int ok=1;
    	char output;
            int currentwidth=0;
            int currentheight=0;
            while(ok){
                 currentwidth++;
    	     currentheight++;
    	     for(i=currentwidth;i&&ok;i--){
                 	ok=fread(&output,1,1,src);
                 	if(ok) {
                 	     XY.x+=1;
                 	     plot(output);
                 	}
    	     }
    
    	     for(i=currentheight;i&&ok;i--){
                 	ok=fread(&output,1,1,src);
                 	if(ok) {
                 	     XY.y+=1;
                 	     plot(output);
                 	}
                 }
                 currentwidth++;
    	     for(i=currentwidth;i&&ok;i--){
                 	ok=fread(&output,1,1,src);
                 	if(ok) {
                 	     XY.x-=1;
                 	     plot(output);
                 	}
                 }
                 currentheight++;
                 for(i=currentheight;i&&ok;i--){
                 	ok=fread(&output,1,1,src);
                 	if(ok) {
                 	     XY.y-=1;
                 	     plot(output);
                 	}
    
    	     }
    	}
    }
    
    void wait(void){
    	char ch;
            settextstyle(TRIPLEX_FONT,HORIZ_DIR,0);
            setcolor(YELLOW);
            outtextxy(10,430,"ESC = Quit");
      	while(1){
      		ch = getche();
      		if (ch == 27) {
      	 		closegraph();
      	 		break;
      	 	}
      	 	delay(100);
      	}
      	cleardevice();
    }
    
    
    void plot(int color){
    	if(!color)return;
    	if(XY.x && XY.y )
    	             	putpixel(XY.x,XY.y,color+14);
            return;
    }
    
    void init_graph(void){
    	int g_driver,g_mode;
            struct viewporttype pt;
            detectgraph(&g_driver,&g_mode);
            initgraph(&g_driver,&g_mode,"c:\TC");
            setbkcolor(BLACK);
            getviewsettings(&pt);
            return;
    }

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    attached is the .exe file in a zip container
    Attached Files Attached Files

  3. #3
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Mozilla doesnt seem to like attachments here... so i will grab it in a sec... but I thought this was funny =):
    Code:
    #define ZERO 239
    =).

    Z.

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    A quick Google found me this:

    http://www.alpertron.com.ar/ULAM.HTM

    I agree, spooky stuff! (PS: 15 years ago was when I first got my sticky little paws on a keyboard )
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Win2k doesnt like it, so I dont get to see it =(.

    Z.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    Try Parksie's link - it shows what the thing looks like.

  7. #7
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    I Did =). Pretty nifty.

    Z.

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Z: I could download it with Mozilla and execute it with Win2k...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Ah, just had to slice the .php off the end =).

    My install of Win2k doesnt like older stuff... Like I cant run Warcraft 2 =(.

    Z.

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Mine doesn't like Diablo2. I can run it, but is SLOOOOOOW...
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width