-
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;
}
-
1 Attachment(s)
attached is the .exe file in a zip container
-
Mozilla doesnt seem to like attachments here... so i will grab it in a sec... but I thought this was funny =):
=).
Z.
-
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 :D)
-
Win2k doesnt like it, so I dont get to see it =(.
Z.
-
Try Parksie's link - it shows what the thing looks like.
-
I Did =). Pretty nifty.
Z.
-
Z: I could download it with Mozilla and execute it with Win2k...
-
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.
-
Mine doesn't like Diablo2. I can run it, but is SLOOOOOOW...