Archive for the ‘Beautiful Codes’ Category
Multiply two Numbers Without Using * Operator
Lets have a fun question. Multiply two numbers without using the * operator.
Here’s the code in C/C++:
main(a,b,m)
{
while (~scanf("%d%d",&a,&b))
{
m=0;
while (a)
{
if (a&1)
m+=b;
a>>=1;
b<<=1;
}
printf("%d\n",m);
}
}
The above code is a implementation of an algorithm better known as the Ethiopian Multiplication or the Russian Peasant Multiplication.
Here’s the algorithm :
- Let the two numbers to be multiplied be a and b.
- If a is zero then break and print the result.
- If a is odd then add b to the result.
- Half a, Double b. Goto Step 2.
NJOY!
-fR0DDY
Program without header file?
So continuing in the series of program without, here are two more programs. The first one is just a valid C program without any header file.
int main()
{
return 0;
}The above program will actually compile and run. But you will say it did nothing. So lets have a program without header file which prints something.
extern "C"
{
int printf(const char *format,...);
}
int main()
{
printf("Hello World");
return 0;
}The only unexplained feature is extern “C”. For more about it read this.
NJOY!
fR0D
Programs without semicolons?
Let’s have a little fun. First of all try to write a C code to print Hello World! without using semicolons. Here’s the answer in C
#include <stdio.h>
int main()
{
if (printf("Hello World!\n")) {}
}or if you are a hardcore C++ fan then
#include <iostream>
int main()
{
if (std::cout << "Hello world!" << std::endl)
{}
}But the real gem is the following program. A program to find the factorial of a number.
#include<iostream>
int main(int n)
{
if (std::cin>>n)
{
if (int f=1 )
{
while (n != 1)
{
if ( f = n * f )
{
if (--n){}
}
}
if(std::cout<<"fac = "<<f<<std::endl)
{}
}
else
{}
}
}Infact any C program can be converted a non-semicolon form.
NJOY!
-fR0D
Bit Play
The first one is to add two integers using bit operators.
int add(int a,int b)
{
if(!a)
return b;
else
return(add((a&b)<<1,a^b));
}The next is to find maximum and minimum of two numbers :
r = y + ((x - y) & -(x < y)); // min(x, y) r = x - ((x - y) & -(x < y)); // max(x, y) where r is the result.
NJOY!!!
-fR0D
Program without main() ?
See this code first :
#include"stdio.h"
#define decode(s,t,u,m,p,e,d) m##s##u##t
#define begin decode(a,n,i,m,a,t,e)
int begin()
{
printf(" hello ");
}
Compile and run this. It works fine but how?Actually its a very good example of the preprocessor directive #define and token pasting or token merging operator ‘##’. Kool program isn’t it?
-fR0D
Size Contest
Looks like i am having some affair with shorter codes. Anyways, there is this question on SPOJ. You have to write the shortest code for finding the sum of all positive integers,given a set of T integers followed by T.
As always i am far away from optimal result :
#include"iostream"
int a,t,s;
main()
{
for(std::cin>>t;t--;std::cin>>a,s+=a>0?a:0);
std::cout<<s;
}
And Python Solution :
t=input()
s=0
while t:
x=input()
s+=x*(x>0)
t-=1
print sIf you have a shorter C++ or Python code, help me out. Between try this line to see the path of your file.
printf("The source file name is %s\n",__FILE__);
-fR0D
Binary Palindromes
There is this question from a programming competition which requires you to find whether the binary representation of a number is palindrome or not. The input begins with integer T followed by T cases.
The obvious code would be(my solution) :
main(v,r,t,x)
{scanf("%d",&t);while(t--){
for(scanf("%d",&v),x=v,r=0;v;r<<=1,r|=v&1,v>>=1){}
puts(r-x?"NO":"YES");}}
But there were better(shorter) solutions which used recursive main functions :
a,t;main(c,b)
{c?main(c/2,b*2+c%2):
t++/2&&puts(a-b?"NO":"YES"),
~scanf("%d",&a)&&main(a,0);}
-fR0D
Power of 2
There was an online competition which required to find whether an integer(<2^31) entered is a power of 2 or not. The condition was that code should be as small as possible and should not use any semicolons.The input is terminated by end of file. What me and my room mate could get to was this :
main(int N){while(scanf(“%d”,&N)+1&&puts(N&N-1?”no”:”yes”)){}}
Though the best solution turned out to be this which was five character less than our solution :
main(i){while(~scanf(“%d”,&i)&&puts(i&i-1?”no”:”yes”)){}}
Both are C programs, so you need to compile it using gcc(g++ won’t work).
-fR0D
Tower of Hanoi
This is a C program to solve the very famous Tower of Hanoi problem. It neither uses recursion nor stack. Moreover, it solves the problem in just 6 statements. See the indenting done to make it look like a tower.
main(
){int
z,y,n
;scanf("%d",&n);
for(y=1;(1<<n)-y
;y<<=z-1,printf(
"Move disk %i from %i to %i.\n"
,z,(y&y-1)%3+1,((y|y-1)+1)%3)+1,y
++)for(z=1;!(y&1);z++,y>>=1);}/****/
Found it while googling.
-fR0D

