Some good looking codes and interesting problems
Posted by ajay on July 21, 2006
I recently came across some awesome piece of codes. I dont understand any of them, but I find it worth sharing here.
1. A c program to solve The Tower of Hanoi problem without using functions, stack etc. and does the task in 6 lines. Moreover, the code looks like a tower as well
.
main(
){int
z,y,n
;scanf(“%d”,&n);
for(y=1;(1<<n)-y
;y<<=z-1,printf(
“disk %i from %i to %i.\n”/**/
,z,(y&y-1)%3,((y|y-1)+1)%3),y
++)for(z=1;!(y&1);z++,y>>=1);}
2. The queen of problems – N queens problem. If you are a computer science student, you must have heard about n queens problem and probably solved that as well. This C program solves the problem very quickly, and for the rest – just have a look at the code.
int v,i,j,k,l,s,a[99];main(){for(scanf(“%d”,&s);*a-s;v=a[j*=v]-a[i],k=i<
s,j+=(v=j<s&&(!k&&!!printf(2+”\n\n%c”-(!l<<!j),” #Q”[l^v?(l^j)&1:2])&&++
l||a[i]<s&&v&&v-i+j&&v+i-j))&&!(l%=s),v||(i==j?a[i+=k]=0:++a[i])>=s*k&&
++a[--i]);printf(“\n\n”);}
3. Prints first 15000 digits of PI.
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;}
4. An interesting problem -
what is the “condition” such that, this snippet of code prints HelloWorld!!
if condition
printf(“Hello);
else
print(“World!!);
my friend abhilash gave a nice solution for that ..
if ( printf(“Hello”)<0){
printf(“Hello”);
else
printf(“World!!);
5. Another interesting problem -
Write a small C program, which while compiling takes another program
from input terminal, and on running gives the result for the second
program. (NOTE: The key is, think UNIX).
Suppose, the program is answer.c
Then, while compiling
$ cc -o answer answer.c
int main()
{
printf(“Hello World\n”);
}
^D
$ ./answer
Hello World
$
if anybody can solve this problem please let me know by commenting over the post with the solution.
Happy coding.
Atul said
gud work dude………
but I didn’t get what does if(printf(“Hello”) >0 ) );
plz mention that also..
ajay said
@atul plzz mention that also
coz printf(“Hello”) returns 1 so this condition will be true and in that case the output would be
HelloHello
which we don’t want .. so we need to somehow make that if condition fail
.. I hope its clear now
Turbo said
Keep coming with these kinds of posts.
sharatechno said
gud work dude
bluecap said
its gr* work keep it up
bluecap said
its gr8
work keep it up
navin said
For the last question , modify the
$ gcc -o ans -xc -
#include
int main()
{
printf(“hello world\n”);
}
^D
well you can alias cc or write a script for that.Hope this solves your problem
nik said
#include”/dev/tty”
/* obfuscated c contest 1988 prize winner Spinellis.c */
harsha said
hi there,
nice works buddy. i loved the tower of hanoi problem for which u hve given a nice code.
hey, can u help me by sending c-codes for the linux commands awk,grep,pipe.
Thanks.
-faithfully
harsha
ajay said
@Harsha
I don’t have c codes for such utilities ( why write them if you have the commands ? ). :d
Siddharth said
U can find more of such interesting codes by googling for IOCCC ,:).
aravindakella said
Dude … ur blog rocks,I hav seen u in a lot of OPCs.
Keep up the good work.
Abhishek Mishra said
checkout
http://www.ocf.berkeley.edu/~wwu/1337/code.shtml
for amazing obfuscated codes.. they look so beautiful..
#define X
#define XX
#define XXX
#define XXXX
#define XXXXX
#define XXXXXX
#define XXXXXXX
#define orfa for
#define XXXXXXXXX
#define archa char
#define ainma main
#define etcharga getchar
#define utcharpa putchar
X X
X X X X
X X X X
X X X X
X X X X
X X X X
X X X X
X X X X X X
X XX X X XX X
X XXX X XXXXXXXXX X XXX X
X XXX X XXXX XXXX X XXX X
X XXXX X XX ainma(){ archa XX X XXXX X
X XXXX X oink[9],*igpa, X XXXX X
X XXXXXX atinla=etcharga(),iocccwa XXXXXX X
X XXXX ,apca=’A',owla=’a',umna=26 XXXX X
X XXX ; orfa(; (atinla+1)&&(!((( XXX X
X XX atinla-apca)*(apca+umna-atinla) XX X
X X >=0)+((atinla-owla)*(owla+umna- X X
X atinla)>=0))); utcharpa(atinla), X
X X atinla=etcharga()); orfa(; atinla+1; X X
X X ){ orfa( igpa=oink ,iocccwa=( X X
X X (atinla- XXX apca)*( XXX apca+umna- X X
X atinla)>=0) XXX XXX ; (((( X
X atinla-apca XXXXX XXXXXXX XXXXX )*(apca+ X
X umna-atinla XXXXXX )>=0) XXXXXX +((atinla- X
X owla)*(owla+ XXXX umna- XXXX atinla)>=0)) X
X &&”-Pig-” XX “Lat-in” XX “COb-fus” X
X “ca-tion!!”[ X (((atinla- X apca)*(apca+ X
X umna-atinla) X >=0)?atinla- X apca+owla: X
X atinla)-owla X ]-’-')||((igpa== X oink)&&!(*( X
X igpa++)=’w') X )||! X (*( X igpa X ++)=owla); * X
X (igpa++)=(( X ( XXX XXX X atinla-apca X
X )*(apca+ X umna XXX – XXX X atinla)>=0) X
X ?atinla- X apca XXX + XXX owla X :atinla), X
X atinla= X X X X etcharga()) X
X ; orfa( X atinla=iocccwa?(( X (atinla- X
X owla)*(owla+ X umna-atinla)>=0 X )?atinla- X
X owla+apca: X atinla): X atinla; ((( X
X atinla-apca)* X (apca+umna- X atinla)>=0)+( X
X (atinla-owla)* X (owla+ X umna-atinla)>= X
X 0)); utcharpa( XX XX atinla),atinla X
X =etcharga()); XXXXXXX orfa(*igpa=0, X
X igpa=oink; * igpa; utcharpa( X
X *(igpa++))); orfa(; (atinla+1)&&(!((( X
X atinla-apca )*(apca+ X
X umna- XXXXX XXXXX atinla)>=0 X
X )+(( XXXXX atinla- X
XX owla)*( owla+umna- XX
XX atinla)>=0))); utcharpa XX
XX (atinla),atinla= XX
XX etcharga()); } XX
XXXX } XXXX
XXXXXXXXX
Abhishek Mishra said
woops… couldn’t paste it properly
Jigsaw said
Hey for that problem
“$ cc -o answer answer.c
int main()
{
printf(”Hello World\n”);
}
^D
$ ./answer
Hello World”
The trick lies in smart usage of include files
answer.c contains
#include “stdin”
So while preprocessing it tries to read from the std input till it encounters the end of file and substitutes the #include…. with the file content. Then that’s preprocessed again and compiled.
Done.. that’s it
pavansunny said
bhai somani i want to know how to do primality testing for a no consisting upto 1000 digits in some what efficient way ?
please help !!!!!!!111
@ pavan.sunny52@gmail.com