Oct 9th, 2002, 02:33 PM
#1
Thread Starter
Frenzied Member
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;
}
Oct 9th, 2002, 02:36 PM
#2
Thread Starter
Frenzied Member
attached is the .exe file in a zip container
Attached Files
Oct 9th, 2002, 03:09 PM
#3
Frenzied Member
Mozilla doesnt seem to like attachments here... so i will grab it in a sec... but I thought this was funny =):
=).
Z.
Oct 9th, 2002, 03:09 PM
#4
Monday Morning Lunatic
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
Oct 9th, 2002, 03:14 PM
#5
Frenzied Member
Win2k doesnt like it, so I dont get to see it =(.
Z.
Oct 9th, 2002, 03:37 PM
#6
Thread Starter
Frenzied Member
Try Parksie's link - it shows what the thing looks like.
Oct 9th, 2002, 04:08 PM
#7
Frenzied Member
I Did =). Pretty nifty.
Z.
Oct 11th, 2002, 07:43 AM
#8
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.
Oct 11th, 2002, 08:55 AM
#9
Frenzied Member
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.
Oct 11th, 2002, 08:58 AM
#10
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
Forum Rules
Click Here to Expand Forum to Full Width