• Forum
  • Lounge
  • I want to see a simple but incredible pi

 
I want to see a simple but incredible piece of code.

I want to see a short-medium piece of code that will baffle my mind in a way that it will make me realize I could never attain such creativity.
Anyone?
Last edited on
1
2
3
4
5
6
#include <iostream>

int main()
{
    std::cout << "Hello, World!" << std::endl;
}
Hello, World!



http://www.ioccc.org/ --these might baffle your mind.
http://www.cise.ufl.edu/~manuel/obfuscate/obfuscate.html --from ioccc
('&%:9]!~}|z2Vxwv-,POqponl$Hjig%eB@@>}=<M:9wv6WsU2T|nm-,jcL(I&%$#"
`CB]V?Tx<uVtT`Rpo3NlF.Jh++FdbCBA@?]!~|4XzyTT43Qsqq(Lnmkj"Fhg${z@>

Prints:
Hello World!


You have got to love Malbolge.
Last edited on
or any other esoteric language ;P
http://esolangs.org/wiki/Language_list
main(a,b)char**b;{int c=1,d=c,e=a-d;for(;e;e--)_(e)<_(c)?c=e:_(e)>_(d)?d=e:7;
while(++e<a)printf("\xe2\x96%c",129+(**b=8*(_(e)-_(c))/(_(d)-_(c))));}


^found this in ioccc. OMG that is so cool is that even C? lol
speaking of esolangs... tbf and brainfuck
void
Last edited on
and pray tell, how you would plant a bug in another language (and why does it have to be an esolang)? also how do you plan to get rich from that?
^ I found out that won't be possible. Since no program have been written in Malbolge lol.
If no programs have been written in Malbolge, then what was the hello world program there? And for the reference, that Malbolge program is actually as short as the shortest possible for C++, and only one line longer than the shortest C hello world program.
I was mighty impressed when I saw this ! (link to author's page: http://home.hccnet.nl/h.g.muller/max-src2.html)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/***************************************************************************/
/*                               micro-Max,                                */
/* A chess program smaller than 2KB (of non-blank source), by H.G. Muller  */
/***************************************************************************/
/* version 1.6 (1433 non-blank characters) features:                       */
/* - recursive negamax search                                              */
/* - quiescence search with recaptures                                     */
/* - recapture extensions                                                  */
/* - (internal) iterative deepening                                        */
/* - best-move-first 'sorting'                                             */
/* - full FIDE rules and move-legality checking                            */

/* accepts under-promotions: type 1,2,3 (=R,B,N) after input move          */
/* (input buffer c[] & *P made global, K and N encoding swapped for this)  */

#define W while

int M=136,S=128,I=8e3,C=799,Q,O,K,N;   

char L,*P,
w[]={0,1,1,-1,3,3,5,9},                      
o[]={-16,-15,-17,0,1,16,0,1,16,15,17,0,14,18,31,33,0,
     7,-1,6,11,8,3,6,                          
     6,4,5,7,3,5,4,6},                         
b[129],

n[]=".?+knbrq?*?KNBRQ",

c[9];

D(k,q,l,e,E,z,n)        
int k,q,l,e,E,z,n;      
{                       
 int j,r,m,v,d,h,i,F,G,s;
 char t,p,u,x,y,X,Y,H,B;

 q--;                                          
 d=X=Y=0;                                      

 W(d++<n||                                     
   z==8&K==I&&(N<1e6&d<98||                    
   (K=X,L=Y&~M,d=2)))                          
 {x=B=X;                                       
  h=Y&S;                                   
  m=d>1?-I:e;                                  
  N++;                                         
  do{u=b[x];                                   
   if(u&k)                                     
   {r=p=u&7;                                   
    j=o[p+16];                                 
    W(r=p>2&r<0?-r:-o[++j])                    
    {A:                                        
     y=x;F=G=S;                                
     do{                                       
      H=y=h?Y^h:y+r;                        
      if(y&M)break;                            
      m=E-S&&b[E]&&y-E<2&E-y<2?I:m;    /* castling-on-Pawn-check bug fixed */
      if(p<3&y==E)H^=16;                       
      t=b[H];if(t&k|p<3&!(y-x&7)-!t)break;       
      i=99*w[t&7];                             
      m=i<0?I:m;                       /* castling-on-Pawn-check bug fixed */
      if(m>=l)goto C;                          

      if(s=d-(y!=z))                           
      {v=p<6?b[x+8]-b[y+8]:0;
       b[G]=b[H]=b[x]=0;b[y]=u|32;             
       if(!(G&M))b[F]=k+6,v+=30;               
       if(p<3)                                 
       {v-=9*((x-2&M||b[x-2]-u)+               
              (x+2&M||b[x+2]-u)-1);            
        if(y+r+1&S)b[y]|=7,i+=C;               
       }
       v=-D(24-k,-l,m>q?-m:-q,-e-v-i,F,y,s);   
       if(K-I)                                 
       {if(v+I&&x==K&y==L&z==8)                
        {Q=-e-i;O=F;
         if(b[y]-u&7&&P-c>5)b[y]-=c[4]&3;        /* under-promotions */
         return l;
        }v=m;                                   
       }                                       
       b[G]=k+6;b[F]=b[y]=0;b[x]=u;b[H]=t;     
       if(v>m)                         
        m=v,X=x,Y=y|S&F;                       
       if(h){h=0;goto A;}                            
      }
      if(x+r-y|u&32|                           
         p>2&(p-3|j-7||                        
         b[G=x+3^r>>1&7]-k-6                   
         ||b[G^1]|b[G^2])                      
        )t+=p<5;                               
      else F=y;                                
     }W(!t);                                   
  }}}W((x=x+9&~M)-B);                          
C:if(m>I-M|m<M-I)d=98;                         
  m=m+I?m:-D(24-k,-I,I,0,S,S,1);    
 }                                             
 return m+=m<e;                                
}

main()
{
 int k=8;

 K=8;W(K--)
 {b[K]=(b[K+112]=o[K+24]+8)+8;b[K+16]=18;b[K+96]=9;
  L=8;W(L--)b[16*L+K+8]=(K-4)*(K-4)+(L-3.5)*(L-3.5); 
 }                                                   

 W(1)                                                
 {N=-1;W(++N<121)
   printf(" %c",N&8&&(N+=7)?10:n[b[N]&15]);          
  P=c;W((*P++=getchar())>10);
  K=I;                                               
  if(*c-10)K=*c-16*c[1]+C,L=c[2]-16*c[3]+C;          
  k^=D(k,-I,I,Q,O,8,2)-I?0:24;
 }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
.<script>
eval(z='p="<"+"pre>"/* ,.oq#+     ,._, */;for(y in n="zw24l6k\
4e3t4jnt4qj24xh2 x/* =<,m#F^    A W###q. */42kty24wrt413n243n\
9h243pdxt41csb yz/* #K       q##H######Am */43iyb6k43pk7243nm\
r24".split(4)){/* dP      cpq#q##########b, */for(a in t=pars\
eInt(n[y],36)+/*         p##@###YG=[#######y */(e=x=r=[]))for\
(r=!r,i=0;t[a/*         d#qg `*PWo##q#######D */]>i;i+=.05)wi\
th(Math)x-= /*        aem1k.com Q###KWR#### W[ */.05,0<cos(o=\
new Date/1e3/*      .Q#########Md#.###OP  A@ , */-x/PI)&&(e[~\
~(32*sin(o)*/* ,    (W#####Xx######.P^     T % */sin(.5+y/7))\
+60] =-~ r);/* #y    `^TqW####P###BP           */for(x=0;122>\
x;)p+="   *#"/* b.        OQ####x#K           */[e[x++]+e[x++\
]]||(S=("eval"/* l         `X#####D  ,       */+"(z=\'"+z.spl\
it(B = "\\\\")./*           G####B" #       */join(B+B).split\
(Q="\'").join(B+Q/*          VQBP`        */)+Q+")//m1k")[x/2\
+61*y-1]).fontcolor/*         TP         */(/\\w/.test(S)&&"#\
03B");document.body.innerHTML=p+=B+"\\n"}setTimeout(z)')//
</script>


http://www.aem1k.com/world/

I have yet to find out how it is done.
http://www.cise.ufl.edu/~manuel/obfuscate/obfuscate.html
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
wtf, how did that codes even compile
Last edited on
> I want to see a simple but incredible piece of code.

30 lines of the simplest kind of C code. That can take one's breath away.

The regular expressions we present here make use of four metacharacters: "^," "$," ".," and "*," with "*" specifying zero or more occurrences of the preceding period or literal character. This provides a large fraction of the power of general regular expressions with a tiny fraction of the implementation complexity. We'll use these functions to implement a small but eminently useful version of grep
...
Our implementation is admittedly unsophisticated, but it works. And, at fewer than 30 lines of code, it shows that regular expressions don't need advanced techniques to be put to use.
- Brian W. Kernighan and Rob Pike in Dr. Dobb's

http://www.drdobbs.com/architecture-and-design/regular-expressions/184410904


Not as simple, at about 300 lines or so, Ken Tompson's NFA implementation of the grep compiler (bytecode version; the original generates IBM 7094 machine code):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
 * Bytecode machine implementation of Thompson's
 * on-the-fly regular expression compiler.
 *
 * See also Thompson, Ken.  Regular Expression Search Algorithm,
 * Communications of the ACM 11(6) (June 1968), pp. 419-422.
 * 
 * Copyright (c) 2004 Jan Burgy.
 * Can be distributed under the MIT license, see bottom of file.
 */

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>

enum	{
	STOP,
	JUMP,
	MATCH,
	BRANCH,
	LPAREN = CHAR_MAX + 1,
	RPAREN,		/* This should	*/
	ALTERN,		/* reflect the	*/
	CONCAT,		/* precedence	*/
	KLEENE		/* rules!	*/
};

unsigned char *prepare (const char *src)
{
	unsigned char	escape[CHAR_MAX + 1] = "";
	unsigned char	*dest = malloc (2 * (strlen (src) + 1));
	int	c, i, j = 0, concat = 0, nparen = 0;

	escape['a'] = '\a';
	escape['b'] = '\b';
	escape['f'] = '\f';
	escape['n'] = '\n';
	escape['r'] = '\r';
	escape['t'] = '\t';
	escape['v'] = '\v';
	for (i = 0; (c = "\"()*\\|"[i]); i++)
		escape[c] = c;
	
	for (i = 0; (c = src[i]); i++) {
		
		switch (c) {

			case '(':
				dest[j++] = LPAREN;
				concat = 0;
				nparen++;
				continue;
			case ')':
				dest[j++] = RPAREN;
				nparen--;
				break;
			case '*':
				dest[j++] = KLEENE;
				break;
			case '|':
				dest[j++] = ALTERN;
				concat = 0;
				continue;
			case '\\':
				c = escape[(int)src[i + 1]];
				c ? i++ : (c = '\\');
			default:
				if (concat)
					dest[j++] = CONCAT;
				dest[j++] = c;

		}
		concat = 1;
		if (nparen < 0)
			printf ("unbalanced parentheses\n");
		
	}
	dest[j++] = RPAREN;
	dest[j++] = '\0';

	return	dest;
}

unsigned char *convert (const char *src)
{	/* http://cs.lasierra.edu/~ehwang/cptg454/postfix.pdf */
	unsigned char	stack[BUFSIZ] = "";
	unsigned char	*dest = prepare (src);
	int	c, i, j = 0, top = 0;

	stack[top++] = LPAREN;
	for (i = 0; (c = dest[i]); i++) {

		switch (c) {

			case LPAREN:
				stack[top++] = c;
				break;

			case RPAREN:
				while (c <= stack[top - 1])
					dest[j++] = stack[--top];
				--top;	/* discard LPAREN */
				break;

			case ALTERN:
			case CONCAT:
			case KLEENE:
				while (c <= stack[top - 1])
					dest[j++] = stack[--top];
				stack[top++] = c;
				break;

			default:
				dest[j++] = c;
				break;

		}

	}
	dest[j++] = '\0';

	return	dest;
}

struct	instr	{
	short	operand;
	short	address;
};

struct instr assemble (short operand, short address)
{
	struct	instr	this;

	this.operand = operand;
	this.address = address;
	return	this;
}

size_t memlen (const unsigned char *s)
{
	const unsigned char	*p = s;

	while (*p)
		p++;
	return	p - s;
}

struct instr *compile (const unsigned char *src)
{
	int	i, c, pc = 0, top = 0;
	int	stack[BUFSIZ];
	struct	instr	*code = malloc (5 * memlen (src) * sizeof *code / 2);

	for (i = 0; (c = src[i]); i++) {

		switch (c) {

			default:
				stack[top++] = pc;
				code[pc++] = assemble (JUMP, pc + 1);
				code[pc++] = assemble (MATCH, c);
				break;

			case CONCAT:
				--top;
				break;

			case KLEENE:
				code[pc++] = assemble (BRANCH, '*');
				code[pc++] = code[stack[top - 1]];
				code[stack[top - 1]] = assemble (JUMP, pc - 2);
				break;

			case ALTERN:
				code[pc++] = assemble (JUMP, pc + 4);
				code[pc++] = assemble (BRANCH, '|');
				code[pc++] = code[stack[top - 1]];
				code[pc++] = code[stack[top - 2]];
				code[stack[top - 2]] = assemble (JUMP, pc - 3);
				code[stack[top - 1]] = assemble (JUMP, pc);
				--top;
				break;

		}

	}

	code[pc++] = assemble (STOP, pc);

	return	code;
}

struct instr *study (const char *re)
{
	unsigned char	*p = convert (re);
	struct	instr	*q = compile (p);

	if (p) free (p), p = NULL;
	return	q;
}

void dump_code (struct instr *code)
{
	int	i, op;
	char	*str[] = {"STOP", "JUMP", "MATCH", "BRANCH"};

	for (i = 0; (op = code[i].operand); i++)
		printf (op == JUMP ? "%2d: %s\t%3d\n" : "%2d: %s\t'%c'\n",
				i, str[op], code[i].address);
}

int execute (struct instr *code, const char *src)
{
	short	i = 0, c = src[i++], pc = 0;
	short	clist[BUFSIZ], cnode = 0, shift = 0;
	short	nlist[BUFSIZ], nnode = 0;

	while (c) {

		switch (code[pc].operand) {

			case STOP:
				break;

			case JUMP:
				pc = code[pc].address;
				continue;

			case MATCH:
				if (c == code[pc].address)
					nlist[nnode++] = code[pc + 1].address;
				break;

			case BRANCH:
				clist[cnode++] = code[pc + 1].address;
				pc = code[pc + 2].address;
				continue;

		}

		if (shift == cnode) {
			if (!nnode) return 0;
			shift = cnode = 0;
			while (nnode > 0)
				clist[cnode++] = nlist[--nnode];
			c = src[i++];
		}
		pc = clist[shift++];

	}

	/* is any of the current states final? */
	for (i = shift; i < cnode; i++)
		if (code[clist[i]].operand == STOP)
			return	1;

	return	code[pc].operand == STOP;
}

int main (void)
{
	short	i;
	struct	{
		char	*re;
		char	*s;
	} test[] = {
		{ "abcdefg",	"abcdefg"	},
		{ "(a|b)*a",	"ababababab"	},
		{ "(a|b)*a",	"aaaaaaaaba"	},
		{ "(a|b)*a",	"aaaaaabac"	},
		{ "a(b|c)*d",	"abccbcccd"	},
		{ "a(b|c)*d",	"abccbcccde"	},
		{ NULL,		NULL		}
	};

	for (i = 0; test[i].re; i++) {

		struct	instr	*this = study (test[i].re);

		printf ("%s %s /%s/\n",
				test[i].s,
				execute (this, test[i].s) ? "~" : "!~",
				test[i].re);

		if (this) free (this), this = NULL;
	}

	return	EXIT_SUCCESS;
}

/*
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the
 * Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall
 * be included in all copies or substantial portions of the
 * Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
 * KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS
 * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */


Explanation of Thompson's algorithm: http://swtch.com/~rsc/regexp/regexp1.html
Topic archived. No new replies allowed.