wu :: forums
« wu :: forums - Addition of two Nos. »

Welcome, Guest. Please Login or Register.
May 6th, 2024, 5:28pm

RIDDLES SITE WRITE MATH! Home Home Help Help Search Search Members Members Login Login Register Register
   wu :: forums
   riddles
   cs
(Moderators: Icarus, SMQ, william wu, towr, Eigenray, Grimbal, ThudnBlunder)
   Addition of two Nos.
« Previous topic | Next topic »
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print
   Author  Topic: Addition of two Nos.  (Read 1200 times)
aki_scorpion
Newbie
*





   


Gender: male
Posts: 13
Addition of two Nos.  
« on: Jul 12th, 2007, 3:49am »
Quote Quote Modify Modify

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 Wink
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: Addition of two Nos.  
« Reply #1 on: Jul 12th, 2007, 4:44am »
Quote Quote Modify Modify

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?
IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
aki_scorpion
Newbie
*





   


Gender: male
Posts: 13
Re: Addition of two Nos.  
« Reply #2 on: Jul 12th, 2007, 9:32am »
Quote Quote Modify Modify

As i mentioned earlier .. no mathematical fn. , arithmetic operators, logical operators, bitwise shold be used. Else u can use any other contraints of programming ...  
IP Logged
SMQ
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 2084
Re: Addition of two Nos.  
« Reply #3 on: Jul 12th, 2007, 12:09pm »
Quote Quote Modify Modify

Here's a dirty trick in C: int sum(int a, int b) { return (int)((((char*)(a))[b])); }
 
--SMQ
IP Logged

--SMQ

SMQ
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 2084
Re: Addition of two Nos.  
« Reply #4 on: Jul 12th, 2007, 2:06pm »
Quote Quote Modify Modify

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);
}

Grin
 
--SMQ
IP Logged

--SMQ

Grimbal
wu::riddles Moderator
Uberpuzzler
*****






   


Gender: male
Posts: 7527
Re: Addition of two Nos.  
« Reply #5 on: Jul 12th, 2007, 3:34pm »
Quote Quote Modify Modify

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.
IP Logged
aki_scorpion
Newbie
*





   


Gender: male
Posts: 13
Re: Addition of two Nos.  
« Reply #6 on: Jul 12th, 2007, 7:24pm »
Quote Quote Modify Modify

The c trick was awesome SMQ .. tough there is one more such C trick
Here it is  
printf("%d",printf("%*s%*s",a" ",b," "));
 Tongue
IP Logged
towr
wu::riddles Moderator
Uberpuzzler
*****



Some people are average, some are just mean.

   


Gender: male
Posts: 13730
Re: Addition of two Nos.  
« Reply #7 on: Jul 13th, 2007, 12:46am »
Quote Quote Modify Modify

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.
« Last Edit: Jul 13th, 2007, 12:47am by towr » IP Logged

Wikipedia, Google, Mathworld, Integer sequence DB
softhacker
Newbie
*





  cool_ranjan4all@yahoo.com  
WWW

Gender: male
Posts: 13
Re: Addition of two Nos.  
« Reply #8 on: Sep 6th, 2007, 2:50am »
Quote Quote Modify Modify

on Jul 12th, 2007, 7:24pm, 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," "));
 Tongue

 
 
hi aki,
  1) a=0 and b=20
  2) a=-10 and b=20
   try above test cases. Grin
IP Logged
Pages: 1  Reply Reply Notify of replies Notify of replies Send Topic Send Topic Print Print

« Previous topic | Next topic »

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