char*y,p[]=" 1 2 3 4 \n 5 6 7 8 \n 9101112 \n131415 \n";main(z){for(y=p+37;
puts(p);getchar())if(y[z="U AIM"[getchar()%5]-75]>47&y[z]<58)*y=y[z],y[-1]=y[z-1
],y[z-1]=y[z]=32,y+=z;}
Controls are u, d, l and r (as in up, down, left and right).
main(o,u){scanf("%d%d",&o,&u);for(printf("%d.",o/u);o=o%u*10;putchar(o/u+48));}
This program reads two numbers and divides them. The cool part is that it will
keep giving decimals until they run out or the user breaks, ie 1 3 gives
0.333333333333333333.... 1 7 gives 0.142857142857142857142857142857142857...
and so on.
main(i,c){for(scanf("%d",&c);1<(c/=i);printf("%d\n",i))for(i=1;c%++i;);}
This factorizes nubers. Shortest way I could think of.
main(a,b,c,d)char**b;{return(a==2?main(-a,1,atoi(b[1]),a):a<0?main(c%d?-c:7,0,c,
d+1):a>2?main(c/(d-1)>1?-8:0,printf("%d\n",d-1),c/(d-1),2):0);}
This also factorizes numbers, using a recursive call to main.
#define I(x,y) if(x)goto y;
#define M(x) x++;
#define L(x) x--;
main(q,v,a,b,c,d,e,T)char**v;{
a=atoi(v[1]);b=c=d=e=T=0;
M(T)L(a)I(a,f)M(a)M(b)M(c)I(T,s)f:M(a)M(b)g:M(b)M(d)M(c)M(e)h:L(d)I(d,h)i:L(c)
I(c,i)j:L(e)I(e,j)k:M(d)M(c)L(b)I(b,k)l:M(b)L(d)I(d,l)m:M(d)M(e)L(a)I(a,m)n:
M(a)L(d)I(d,n)o:L(e)L(c)M(d)I(e,p)I(T,r)p:I(c,o)C:M(c)L(d)I(d,C)I(T,o)r:I(c,g)
M(d)x:L(d)I(d,x)y:M(d)M(c)L(b)I(b,y)z:M(b)L(d)I(d,z)A:M(d)M(e)L(a)I(a,A)B:M(a)
L(d)I(d,B)s:L(a)L(b)I(b,s) printf("%d\n",c); I(a,t)I(T,Z)t:L(a)I(a,t)D:L(e)
L(c)M(d)I(c,D)M(a)w:M(c)L(d)I(d,w)I(e,D)M(b)I(T,g)Z:;
}
This factorizes numbers using only x++, x-- and if(x)goto y. (+ an atoi to read
the number and a printf to show result.)
main(z,x,y){for(x=time(0)%100;y-x;printf(y<x?"B%d":"S%d",z++))scanf("%d",&y);}
This program lets you guess a number between 0 and 100. It will reply S# or B#,
depending on if your number is smaller or bigger then your guess. # is the number of guesses so far.
char b,x[99]="OOOOO XXXXX\n0123456789:";w(y){if(x[y+=b+=x[b]&32]&32)x[y]=x[b],x[
b]=32;}main(a){for(;puts(x);w(a*2))w(a=(x[b=getchar()^48]&2)-1);}
This game is about moving pegs. It looks like this:
OOOOO XXXXX 0123456789:You can move pieces by jumping another piece or just move it one step. The O:s can move right and the X:es can move left. The point is to try to get the X:es to the left and the O:s to the right, like this:
XXXXX OOOOO 0123456789:To move a piece, enter the number or : under the piece.
main(c){float t,x,y,b=-2,a=b;for(;b-=a>2?.1/(a=-2):0,b<2;putchar(30+c),a+=.0503)
for(x=y=c=0;++c<90&x*x+y*y<4;y=2*x*y+b,x=t)t=x*x-y*y+a;}
This plots a mandelbrot using characters. It requires that the terminal it prints to wraps after 80 chars.
main(a,b){for(;a=223&getchar()+1;putchar(32))if(a>64&a<92)for(b=0,a="Xzjtpnd~xF\
TvHhDfRl|P\\^LZJr"[a-66];(a/=2)>1;b+=a%2)b&&putchar(45+a%2);}
This program makes morse code out of text. It only supports letters, anything
else will be seen as space.
main(c,v,t,s)char**v;{for(s=7;t=*v[1]++;s+=++c%2*(t+(t>52))+t);putchar(57-s%10);
}
This program will calculate the correct modula 10 parity digit for a nine digit
number (The sweding ss-numbers, for instance).
main(x,y){for(;x++;)for(y=2;x%y;)++y/x&&printf("\0%d\n",x);}
This will calculate prime numbers, starting with 3. This used to be in use
in my .signature, formated like this:
main(x,y){for(;x++/* Patrik Lundin, lundin@ludd.luth.se */;)for(y=2;x
%y;)++y/x&&printf/* http://www.ludd.luth.se/~lundin */("%d\n",x);}
void a(char*b,int d,int e,int f){char*c=b;for(;*c;c++){if(!d&*c==68)putchar(
92),putchar('\n');if(!d&*c==92)putchar(92);if(*c==d)a(b,0,0,0);else putchar(
*c==e?34:*c==f?'\n':*c);}}main(){char p[]=/*
andelion '94 */
"void a(char*b,int d,int e,int f){char*c=b;for(;*c;c++){if(!d&*c==68)putchar(\
D92),putchar('\\n');if(!d&*c==92)putchar(92);if(*c==d)a(b,0,0,0);else putchar(\
D*c==e?34:*c==f?'\\n':*c);}}main(){char p[]=/* \
Dandelion '94 */\
DCAC;a(p,65,67,68);}";a(p,65,67,68);}
This program prints it's source. When I wrote it, the point was to try to do it
without using printf, and with <cr>:s. Yes, I know it's the oldest trick in
the book, and I know it's not very short.
main(S,E,R,P){char _[2000];for(memset(_,' ',S=E=2000);S--;_[E]=79)P=E%80,E+=!(R=
rand()%3)?(40-P)/2-E/160*80:(R&1?80-P:-P)/2+(24-E/80)*40;printf("%.2000s",_);}
This plots the serpinski triangle using O:s. Like the mandelbrot plotter, it
requires 80 char lines with linewraps.
char*d,b=91,f[99];main(R,a){for(;b--;f[44]=46)f[b]=b/30-1&b%10/3-1?45:42;for(;b=
9*puts("012345678");){for(;b;printf("%.9s%d\n",f+b,b))b-=10;scanf("%d%c",&R,&b);
a="R=f[]"[b%5]-92;d=f+R+a;if(d[a]>45&*d<43&f[R]<43)*d=f[R]=46,d[a]-=4;}}
Move using a
number and a direction (u, d, l ,r for up, down, left, right), ie 64d, 48l, etc.
#includeThis prints the current time in 8x7 banner style. Again, 80 char linewrap is required. (I like this one :-)#define Q 10*8,y+T-> struct tm*T;time_t t;main(i,j,k){char x[561],*y=x;for(t=time(i=0);j="HCBCPFRCBC\ XCBCPFRCBCRDBCBCBLBFBFDDBCBCBDBFDDBCBCHC]DBBRDBCBIBFDDDFBKBABDDDJCBCRDrCBCRDj"[i ++];)for(;--j>63;*y++=i%2?35:32);for(T=localtime(&t);(y-=80)>=x; printf("%.8s %.8s %.8s %.8s %.8s %.8s\n",y+T->tm_hour/Q tm_hour%Q tm_min/Q tm_min%Q tm_sec/Q tm_sec%10*8));}
Well, that's just about it. I havn't written any new ones for a while, but maybee I'll take it up again one of these days. If you have any comments, mail 'em.
... and so I did. Kind of anyhow; here are three new one's I wrote a few days ago
main(c,r){for(r=0;c=getchar()+1;r=c-33&&c-11?r+r+2-c%2:
putchar(" etianmsurwdkgohvf-l-pjbxcyzq"[r])*0);}
This decodes morse code. Feed it with .:s, -:s, ' ':s and '\n':s or it might
hang. :-)
char*e,*s=" etianmsurwdkgohvf l pjbxcyzq";f(x){x&&f((x-1)/2)+
putchar(45+x%2);}main(b,a,c){for(c=0;a=223&getchar()+1;c=a>11?c+
c+2-a%2:putchar(s[c])*0)a=b-1?(e=strchr(s,--a|32))&&f(e-s)*0:a;}
This one both codes and decodes. With no arguments, it decodes. With arguments
it codes. Same limitations as the two previous morse proggs.
#includePlays hangman with words from /usr/dict/words. I'm not particulary proud of this one; It's kind of longish and doesn't really have any cool tricks in it...main(i,e,g,a){FILE*in;char w[99],s[99];srand(time(0));in=fopen("/usr/dict/words" ,"r");fseek(in,rand()%200000,0);for(i=98;e=i;s[--i]=45)fgets(w,98,in);s[i=strlen (w)-1]=10;for(s[i+1]=0;printf("%c%c%c\n%c %c\n%c %c\n\n%s",(e>2)+32,(e>3)*2+32,e >4?'o':32,(e>1)+32,e<6?32:"|1TTT"[e-6],e?'n':32,e<9?32:e-9?'^':'`',s)&&e<10&& strcmp(w,s);e+=!a){g=getchar();getchar();for(a=i=0;w[i];i++)if(w[i]==g)a=s[i]=g; }puts(e>9?w:"GratZ");}