wu :: forums (http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi)
riddles >> cs >> Addition of two Nos.
(Message started by: aki_scorpion on Jul 12th, 2007, 3:49am)

Title: Addition of two Nos.
Post by aki_scorpion on Jul 12th, 2007, 3:49am
Hi
Addition of two nos. might sound easy .. but is it still so easy if you are not allowed to use ANY operator.
No operators like +,-,*,/,bitwise or logical or any mathematical fn.

Hint : Think along the lines of coding ;)

Title: Re: Addition of two Nos.
Post by towr on Jul 12th, 2007, 4:44am
Every program must constain some operators, otherwise it can't operate. So "any" seems to be overstating the requirements; anything outlawed besides arithmetical, logical or bitwise operators?

Title: Re: Addition of two Nos.
Post by aki_scorpion on Jul 12th, 2007, 9:32am
As i mentioned earlier .. no mathematical fn. , arithmetic operators, logical operators, bitwise shold be used. Else u can use any other contraints of programming ...

Title: Re: Addition of two Nos.
Post by SMQ on Jul 12th, 2007, 12:09pm
Here's a dirty trick in C: int sum(int a, int b) { return (int)(http://www.ocf.berkeley.edu/~wwu/YaBBImages/symbols/amp.gif(((char*)(a))[b])); }

--SMQ

Title: Re: Addition of two Nos.
Post by SMQ on Jul 12th, 2007, 2:06pm
Without ANY operators:

unsigned int sum(unsigned char a, unsigned char b) {
 switch (a) {
 case 0:
   switch(b) {
   case 0: return 0;
   }
   break;
 case 1:
   switch(b) {
   case 0: return 1;
   case 1: return 2;
   }
   break;
 case 2:
   switch(a) {
   case 0: return 2;
   case 1: return 3;
   case 2: return 4;
   }
   break;
 [...]
 case 255:
   switch(b) {
   case 0: return 255;
   case 1: return 256;
   [...]
   case 255: return 510;
   }
 }
 return sum(b, a);
}

;D

--SMQ

Title: Re: Addition of two Nos.
Post by Grimbal on Jul 12th, 2007, 3:34pm
void printsum(String x, String y)
{
   System.out.print(x);
   System.out.println(y);
}

numbers are represented in simplified roman notation.
1 = I
2 = II
...
10 = IIIIIIIII
etc.

Title: Re: Addition of two Nos.
Post by aki_scorpion on Jul 12th, 2007, 7:24pm
The c trick was awesome SMQ .. tough there is one more such C trick
Here it is
printf("%d",printf("%*s%*s",a" ",b," "));
:P

Title: Re: Addition of two Nos.
Post by towr on Jul 13th, 2007, 12:46am
something like

return string('1',a).append('1',b).size()

would also work (return the length of a string of a 1's concatenated by a string of b 1's).
Of course behind the scenes a lot of arithmetic is done.

Title: Re: Addition of two Nos.
Post by softhacker on Sep 6th, 2007, 2:50am

on 07/12/07 at 19:24:03, aki_scorpion wrote:
The c trick was awesome SMQ .. tough there is one more such C trick
Here it is
printf("%d",printf("%*s%*s",a" ",b," "));
:P



hi aki,
      1) a=0 and b=20
      2) a=-10 and b=20
       try above test cases. ;D



Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board