Adding without using a + or - sign
R (24 characters)
length(sequence(scan()))
What this does:
scan
reads input from STDIN (or a file)sequence
generates integer sequences starting from 1 and concatenates the sequences. For example,sequence(c(2, 3))
results in the vector1 2 1 2 3
length
calculates the number of elements in the concatenated vector
Example 1:
> length(sequence(scan()))
1: 123
2: 468
3:
Read 2 items
[1] 591
Example 2:
> length(sequence(scan()))
1: 702
2: 720
3:
Read 2 items
[1] 1422
Perl (no +/-, no tie-breakers, 29 chars)
s!!xx!;s!x!$"x<>!eg;say y!!!c
As a bonus, you can make the code sum more than two numbers by adding more x
s to the s!!xx!
.
Alternatively, here are two 21-char solutions with 1 and 3 tie-breakers respectively
say length$"x<>.$"x<>
say log exp(<>)*exp<>
Note: These solutions use the say
function, available since Perl 5.10.0 with the -E
command line switch or with use 5.010
. See the edit history of this answer for versions that work on older perls.
How does the solution with no tie-breakers work?
s!!xx!
is a regexp replacement operator, operating by default on the$_
variable, which replaces the empty string with the stringxx
. (Usually/
is used as the regexp delimiter in Perl, but really almost any character can be used. I chose!
since it's not a tie-breaker.) This is just a fancy way of prepending"xx"
to$_
— or, since$_
starts out empty (undefined, actually), it's really a way to write$_ = "xx"
without using the equals sign (and with one character less, too).s!x!$"x<>!eg
is another regexp replacement, this time replacing eachx
in$_
with the value of the expression$" x <>
. (Theg
switch specifies global replacement,e
specifies that the replacement is to be evaluated as Perl code instead of being used as a literal string.)$"
is a special variable whose default value happens to be a single space; using it instead of" "
saves one char. (Any other variable known to have a one-character value, such as$&
or$/
, would work equally well here, except that using$/
would cost me a tie-breaker.)The
<>
line input operator, in scalar context, reads one line from standard input and returns it. Thex
before it is the Perl string repetition operator, and is really the core of this solution: it returns its left operand (a single space character) repeated the number of times given by its right operand (the line we just read as input).y!!!c
is just an obscure way to (ab)use the transliteration operator to count the characters in a string ($_
by default, again). I could've just writtensay length
, but the obfuscated version is one character shorter. :)
D
main(){
int a,b;
readf("%d %d",&a,&b);
while(b){a^=b;b=((a^b)&b)<<1;}
write(a);
}
bit twiddling for the win
as a bonus the compiled code doesn't contain a add operation (can't speak for the readf call though)