COME ON CODE ON

A blog about programming and more programming.

Archive for the ‘Beautiful Codes’ Category

Multiply two Numbers Without Using * Operator

with 7 comments

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 :

  1. Let the two numbers to be multiplied be a and b.
  2. If a is zero then break and print the result.
  3. If a is odd then add b to the result.
  4. Half a, Double b. Goto Step 2.


NJOY!
-fR0DDY

Written by fR0DDY

April 8, 2010 at 10:05 AM

Posted in Beautiful Codes

Tagged with , ,

Program without header file?

with one comment

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

Written by fR0DDY

August 7, 2009 at 2:35 PM

Posted in Beautiful Codes

Tagged with , , , , , ,

Programs without semicolons?

with 10 comments

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
int main()
{
if (std::cout << "Hello world!" << std::endl) {} }[/sourcecode] But the real gem is the following program. A program to find the factorial of a number. [sourcecode language='cpp']#include
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 = "<

Written by fR0DDY

July 21, 2009 at 9:35 PM

Posted in Beautiful Codes

Tagged with , , ,

Bit Play

with 2 comments

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)); }[/sourcecode] 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

Written by fR0DDY

February 23, 2009 at 5:13 AM

Posted in Beautiful Codes

Tagged with ,

Program without main() ?

with one comment

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

Written by fR0DDY

February 18, 2009 at 12:54 PM

Posted in Beautiful Codes

Tagged with ,

Size Contest

with 7 comments

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;
} 
&#91;/sourcecode&#93;
And Python Solution :
&#91;sourcecode language='python'&#93;t=input()
s=0
while t:
    x=input()
    s+=x*(x>0)
    t-=1
print s

If 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

Written by fR0DDY

February 17, 2009 at 5:18 PM

Posted in Beautiful Codes, Programming

Tagged with ,

Binary Palindromes

with 4 comments

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

Written by fR0DDY

February 13, 2009 at 9:48 AM

Posted in Beautiful Codes

Tagged with

%d bloggers like this: