There have been many "Do __ without __" challenges before, but I hope that this is one of the most challenging.

The Challenge

You are to write a program that takes two natural numbers (whole numbers > 0) from STDIN, and prints the sum of the two numbers to STDOUT. The challenge is that you must use as few + and - signs as possible. You are not allowed to use any sum-like or negation functions.

Examples

input

123
468

output

591

input

702
720

output

1422

Tie Breaker: If two programs have the same number of + and - characters, the winner is the person with fewer / * ( ) = . , and 0-9 characters.

Not Allowed: Languages in which the standard addition/subtraction and increment/decrement operators are symbols other than + or - are not allowed. This means that Whitespace the language is not allowed.

  • 1
    Perhaps this challenge was a lot easier than I thought it would be, especially in other languages, where there are sum() functions. I have to fix this. – PhiNotPi Dec 1 '11 at 0:45
  • 36
    100 rep bounty for anybody who can do this in Brainfuck. – Peter Olson Dec 1 '11 at 5:57
  • 3
    @Peter Olson Well, I guess BF is not turing complete without either + or -... – FUZxxl Dec 1 '11 at 10:21
  • 3
    Just to clarify, this challenge does not care about code length right? Only the number of +,- and tie breaker characters? ...or do you need to change the rules again :-) – Tommy Dec 1 '11 at 19:39
  • @Tommy No, it does not. – PhiNotPi Dec 1 '11 at 21:58

40 Answers 40

APL (10, 3 tie-breakers)

⎕←⍴(⍳⎕),⍳⎕
  • 10x +/-? Chars arn't interesting, just +/- and the tie-breakers. – user unknown May 30 '12 at 3:34

C#,

Program works on 1 line; separated on multiple lines to avoid horizontal scrolling.

using C=System.Console;
class A{
static void Main(){
int a,b,x,y;
a=int.Parse(C.ReadLine());
b=int.Parse(C.ReadLine());
do{x=a&b;y=a^b;a=x<<1;b=y;}while(x>0);
C.WriteLine(y);
}}

Common Lisp

Always surprising for such a verbose language.

((lambda(n m)(princ(length(append(make-list n)(make-list m)))))(read)(read))

Python 3 - 159 characters | ().,*/= count: 39

A little long, but I felt like doing it HDL-style.

a,b,c,d=int(input()),int(input()),0,0;l=max(a,b,key=int.bit_length)
for i in range(len(bin(l<<1)[2:])):
 e=a>>i&1;f=b>>i&1;c|=(e^f^d)<<i;d=e&f|d&(e^f)
print(c)
  • +/- tiebreakers are interesting, not characters. – user unknown May 30 '12 at 3:33
  • @userunknown: What's wrong with characters? – JAB May 30 '12 at 16:55
  • Characters are counted in CodeGolf tagged questions. If a reader doesn't read the question again, he will get the impression that character count is important and probably make a misguided vote based on that assumption. So the rules say: No (+|-). In Tiebreak digits, ().,*/= are important. You should include that number in your answer, so that not every visitor has to count them himself. – user unknown May 30 '12 at 20:41
  • @userunknown: Oh, right. – JAB May 30 '12 at 20:46

C# 141 characters 27 tiebreakers

+/- : 0
=   : 0
.   : 9 
()  : 18


Console.WriteLine(Enumerable.Range(1,int.Parse(Console.ReadLine()))
.Concat(Enumerable.Range(1,int.Parse(Console.ReadLine())))
.Count());

Python 42

a,b=input()
print eval("~-"*a+"~-"*b+"0")

Seven tie breakers, but those are string operations

C#, 12 tiebreakers (), 3 tiebreakers .

using System;
using System.Data;

class Program {
  static string ReadLine { get { return Console.ReadLine(); } }
  static void Main(string[] args) {
    Console.Out.Write(
     new DataTable().Compute(string.Join("\u002b", 
     ReadLine,
     ReadLine), "")
    );
  }
}

JavaScript 69

My attempt, using bitwise operators

for(x=(z=prompt().split(" "))[0],y=z[1];y;)x^=y,y=(y&x^y)<<1;alert(x)

R, 44 bytes

nchar(paste(strrep(" ",scan()),collapse=""))

Try it online!

Converts the two inputs to unary and "sums" via concatenation. Check out the other R answers:

C, score 0 with 8 tie-breakers

f(a,b) { return b ? f(a^b, a<<1 & b<<1) : a; }
 ^ ^ ^               ^   ^    ^      ^^

I've highlighted the tie-break chars.

Negative numbers are assumed to be 2s-complement.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.