Author |
Topic: Addition of two Nos. (Read 1219 times) |
|
aki_scorpion
Newbie
Gender:
Posts: 13
|
|
Addition of two Nos.
« on: Jul 12th, 2007, 3:49am » |
Quote 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
|
|
IP Logged |
|
|
|
towr
wu::riddles Moderator Uberpuzzler
Some people are average, some are just mean.
Gender:
Posts: 13730
|
|
Re: Addition of two Nos.
« Reply #1 on: Jul 12th, 2007, 4:44am » |
Quote 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:
Posts: 13
|
|
Re: Addition of two Nos.
« Reply #2 on: Jul 12th, 2007, 9:32am » |
Quote 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:
Posts: 2084
|
|
Re: Addition of two Nos.
« Reply #3 on: Jul 12th, 2007, 12:09pm » |
Quote 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:
Posts: 2084
|
|
Re: Addition of two Nos.
« Reply #4 on: Jul 12th, 2007, 2:06pm » |
Quote 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); } --SMQ
|
|
IP Logged |
--SMQ
|
|
|
Grimbal
wu::riddles Moderator Uberpuzzler
Gender:
Posts: 7527
|
|
Re: Addition of two Nos.
« Reply #5 on: Jul 12th, 2007, 3:34pm » |
Quote 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:
Posts: 13
|
|
Re: Addition of two Nos.
« Reply #6 on: Jul 12th, 2007, 7:24pm » |
Quote 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," "));
|
|
IP Logged |
|
|
|
towr
wu::riddles Moderator Uberpuzzler
Some people are average, some are just mean.
Gender:
Posts: 13730
|
|
Re: Addition of two Nos.
« Reply #7 on: Jul 13th, 2007, 12:46am » |
Quote 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
Gender:
Posts: 13
|
|
Re: Addition of two Nos.
« Reply #8 on: Sep 6th, 2007, 2:50am » |
Quote 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," ")); |
| hi aki, 1) a=0 and b=20 2) a=-10 and b=20 try above test cases.
|
|
IP Logged |
|
|
|
|