Short codes

How much can be achieved by only a few hundred bytes of codes? This page gives an answer.

The criteria to be included in this list:

The size is measured in bytes. All redundant white spaces (particularly, newlines added for readability) don’t count.

Under 140 bytes (fits in a tweet)

Pascal’s triangle. ([@t3rmin4t0r](http://twitter.com/t3rmin4t0r/statuses/5790833297), 73B)

main(c,r){for(r=32;r;)printf(++c>31?c=!r--,"\n":c<r?" ":~c&r?" `":" #");}

Perhaps the smallest ROT13 implementation in C. (Robert Harley, 78B)

main(g,h){for(;h=getchar(),h>=0;putchar(g<65||g>90?h:g<78?h+13:h-13))g=h&223;}

A simple web framework, which translates a c parameter to the function. Requires PHP 5.3. (Fabien Potencier, 117B)

require __DIR__.'/c.php';if(!is_callable($c=@$_GET['c']?:function(){echo'Woah!';
}))throw new Exception('Error');$c();

Calculates the first 15,000 decimal digits of pi (including “3.”). Uses the unbounded spigot algorithm. (Dik T. Winter, 133B)

a[52514],b,c=52514,d,e,f=1e4,g,h;main(){for(;b=c-=14;h=printf("%04d",e+d/f))for(
e=d%=f;g=--b*2;d/=g)d=d*b+f*(h?a[b]:f/5),a[b]=d%--g;}

A simple IP stack, which can respond to pings. Designed to be usable as a tunnel. Requires sizeof(short) == 2. (Adam Dunkels, 138B)

short s[70];*l=s;t;main(){for(;;){read(0,s,140);if((s[4]&65280)==256&s[10]==8){s
[10]=0;s[11]+=8;t=l[4];l[4]=l[3];l[3]=t;write(1,s,140);}}}

Under 256 bytes

The implementation of RSA system in Perl 5 and dc(1). Receives two hexadecimal arguments, one for decryption/encryption exponent and the another for modulus. Originally conceived as a way against the regulation over the export of cryptography in US, it has undergone a number of revisions for years. (Various authors, 146B)

print pack"C*",split/\D+/,`echo "16iII*o\U@{$/=$z;[(pop,pop,unpack"H*",<>)]}\EsM
sKsN0[lN*1lK[d2%Sa2/d0<X+d*lMLa^*lN%0]dsXx++lMlN/dsM0<J]dsJxp"|dc`

Recognizes the number 8, 9, 10 or 11 given as a bitmap of spaces and # (ends at the empty line), by calculating its Euler characteristics plus 9. Won the Best one-liner award in IOCCC 2004. This particular version differs from the award-winning program, as it required -Dif=while macro definition—hence it “has dirtiest IF”. (Eryk Kopczynski, 148B)

main(O){int I,Q,l=O;while(I=l*4){l=6;while(l>5)l+=Q-8?l-(Q=getchar()-2)%2:l;
while(Q*=2)O+="has dirtiest IF"[(I/-Q&12)-l/Q%4];}printf("%d\n",8+O%4);}

Brainfuck interpreter, where the code is given as the first argument. Supports all instructions but no comments allowed. Requires sizeof(int) == sizeof(int*) == sizeof(char*) and a set of syscalls. (Kang Seonghoon, 160B)

s[99],*r=s,*d,c;main(a,b){char*v=1[d=b];for(;c=*v++%93;)for(b=c&2,b=c%7?a&&(c&17
?c&1?(*r+=b-1):(r+=b-1):syscall(4-!b,b,r,1),0):v;b&&c|a**r;v=d)main(!c,&a);d=v;}

A program that converts a spelt number (“one hundred”) to the decimal (“100”). Supports numbers up to 1015-1. Works by filtering the unique letters (11 in this case) and constructing an octal representation of given word; it also contains a simple stemmer in order to recognize word roots. (Kang Seonghoon, 242B)

long long n,u,m,b;main(e,r){for(;n++||(e=getchar()|32)>0;b="ynwtsflrabg"[n%=11]-
e?b:b*8+n)for(r=b%64-25;e<47&&b;b/=8)for(n=19;n;"1+DIY/.K430x9G(kC["[n]-42&255^b
||(m+=n>15?n:n>9?m%u*~-u:~r?n+!r*16:n*16,b=0))u=1ll<<6177%n--*4;printf("%llx",m)
;}

Under 512 bytes

CSS descrambler implemented in C. (Charles M. Hannum, 434B)

#define m(i)(x[i]^s[i+84])<<
unsigned char x[5],y,s[2048];main(n){for(read(0,x,5);read(0,s,n=2048);write(1,s,
n))if(s[y=s[13]%8+20]/16%4==1){int i=m(1)17^256+m(0)8,k=m(2)0,j=m(4)17^m(3)9^k*2
-k%8^8,a=0,c=26;for(s[y]-=16;--c;j*=2)a=a*2^i&1,i=i/2^j&1<<24;for(j=127;++j<n;c=
c>y)c+=y=i^i/8^i>>4^i>>12,i=i>>8^y<<17,a^=a>>14,y=a^a*8^a<<6,a=a>>8^y<<9,k=s[j],
k="7Wo~'G_\216"[k&7]+2^"cr3sfw6v;*k+>/n."[k>>4]*2^k*257/8,s[j]=k^(k&k*2&34)*6^c+
~y;}}

CSS descrambler implemented in Perl. (Keith Winstein and Marc Horowitz, 472B)

s''$/=\2048;while(<>){G=29;R=142;if((@a=unqT="C*",_)[20]&48){D=89;_=unqb24,qT,@
b=map{ord qB8,unqb8,qT,_^$a[--D]}@INC;s/...$/1$&/;Q=unqV,qb25,_;H=73;O=$b[4]<<9
|256|$b[3];Q=Q>>8^(P=(E=255)&(Q>>12^Q>>4^Q/8^Q))<<17,O=O>>8^(E&(F=(S=O>>14&7^O)
^S*8^S<<6))<<9,_=(map{U=_%16orE^=R^=110&(S=(unqT,"\xb\ntd\xbz\x14d")[_/16%8]);E
^=(72,@z=(64,72,G^=12*(U-2?0:S&17)),H^=_%64?12:0,@z)[_%8]}(16..271))[_]^((D>>=8
)+=P+(~F&E))for@a[128..$#a]}print+qT,@a}';s/[D-HO-U_]/\$$&/g;s/q/pack+/g;eval

Longer


ikiwiki를 씁니다.
마지막 수정