24
\$\begingroup\$

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.

\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – PhiNotPi Dec 1 '11 at 0:45
  • 50
    \$\begingroup\$ 100 rep bounty for anybody who can do this in Brainfuck. \$\endgroup\$ – Peter Olson Dec 1 '11 at 5:57
  • 3
    \$\begingroup\$ @Peter Olson Well, I guess BF is not turing complete without either + or -... \$\endgroup\$ – FUZxxl Dec 1 '11 at 10:21
  • 3
    \$\begingroup\$ 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 :-) \$\endgroup\$ – Tommy Dec 1 '11 at 19:39
  • \$\begingroup\$ @Tommy No, it does not. \$\endgroup\$ – PhiNotPi Dec 1 '11 at 21:58

59 Answers 59

0
\$\begingroup\$

Python

i=input
print eval('%d\x2b%d'%(i(),i()))

37 chars with a little bit of cheating :p

\$\endgroup\$
0
\$\begingroup\$

A very yucky C version

Has no +, -, / or any digits. One use of * for a pointer type. The others (()=,) are needed a lot in the language itself.

#include <malloc.h>
#include <string.h>
#include <stdio.h>
typedef unsigned char *ptr;

char
  y;

const size_t
  one = sizeof y;

unsigned char
  zero='a'^'a',
  a [] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  z [] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
  x [] = "xxxxxxxxxxxxxxxxx";

size_t plus_one (size_t i)
{
  ptr b=&a[one];
  memset (a,'x',sizeof a);
  b[i]=zero;
  return strlen(a);
}

size_t minus_one (size_t i)
{
  ptr b=&a[one];
  memset (a,'x',sizeof a);
  a[i]=zero;
  return strlen(b);
}

size_t minus_zero (size_t i)
{
  size_t p;
  for (p = zero ; z[p] ; p = plus_one (p))
  {
    i = minus_one(i);
  }
  return i;
}

unsigned char to_zero (unsigned char i)
{
  size_t p;
  for (p = one>>one ; x[p] ; p = plus_one (p))
  {
    i = (unsigned char) minus_one(i);
  }
  return i;
}

main (int argc, ptr argv [])
{
  ptr
    a = argv [argc>>one],
    b = argv [argc>>one<<one];

  size_t
    len_a = strlen (a),
    len_b = strlen (b),
    len = len_a > len_b ? len_a : len_b,
    o = plus_one(len),
    i,
    c=one>>one;

  ptr
    res=(ptr)malloc(plus_one(o));

  res[o]=zero;

  for (i = zero ; i < len ; i=plus_one(i))
  {
    size_t va;
    unsigned char r;

    va = (len_a > zero ? minus_zero (a[minus_one(len_a)]) <<one<<one<<one<<one : zero) | (len_b > zero ? minus_zero (b[minus_one (len_b)]) : zero);
    r = (c?"bcdefghijAuhjsadcdefghijABsadlkfdefghijABCvsxwerefghijABCDDNIWjsfghijABCDEOQJdccghijABCDEFIndDjlhijABCDEFGCnjndwijABCDEFGHsadXSkjABCDEFGHICSjshdABCDEFGHIJcbaCBA":"abcdefghijaswhHDbcdefghijAQWuciucdefghijABCOasdpdefghijABCCnjaskefghijABCDCIUdasfghijABCDEuoiDSAghijABCDEFCsalkjhijABCDEFGcapCPcijABCDEFGHCWEoerjABCDEFGHIkjIjIj") [va];
    c=r<'a';
    r&=~(one<<one<<one<<one<<one<<one);
    o=minus_one(o);
    res[o]=to_zero(r);

    if (len_a) len_a = minus_one (len_a);
    if (len_b) len_b = minus_one (len_b);
  }
  if(c) 
  {
    o=minus_one(o);
    res[o]=to_zero('B');
  }
  printf("%s\n",&res[o]);
  free (res);
}
\$\endgroup\$
0
\$\begingroup\$

C #, 26 tiebreakers

+/- : 0
=   : 2
.   : 6 
()  : 18
using System;
using System.Linq;
class P
{
    static void Main()
    {
        Func<int[]> f=()=>new int[int.Parse(Console.ReadLine())];
        Console.Write(f().Concat(f()).Count());
    }
}
\$\endgroup\$
0
\$\begingroup\$

VBA - No +/-, 7 tie-breakers

+,-,/,*,,,.,0-9 = 0
( = 3 (3 total pairs)
) = 3
= = 1

Function a(b)
    For Each c In b
        a = a & Space(c)
    Next
    MsgBox Len(a)
End Function

Takes an array of natural numbers as an argument. This will actually add more than just 2 values, if provided, as well as return the value of a single argument.

Older versions


+,-,/,*,=,.,0-9 = 0
( = 4 (4 total pairs)
) = 4
, = 1

Function a(b, c)
MsgBox Len(Space(b) & Space(c))
End Function

+,-,/,*,=,.,0-9 = 0
( = 4 (4 total pairs)
) = 4
, = 3

Sub a(b,c)
MsgBox Len(String(b," ") & String(c," "))
End Sub

+,-,/,*,. = 0
= = 4
( = 2 (2 total pairs)
) = 2
0-9 = 2
, = 1

Sub d(e,f)
For g=1 To e:h=h & " ":Next
For g=1 To f:h=h & " ":Next
MsgBox Len(h)
End Sub
\$\endgroup\$
  • \$\begingroup\$ Character count technically doesn't matter in this challenge. You have no -+, and you have 11 tie breakers. This puts you above any solution that has a -+ character, and below any solution with no -+ with fewer tie breakers. \$\endgroup\$ – PhiNotPi Apr 12 '12 at 15:43
  • \$\begingroup\$ So, I am tied with (as one example) Uri Goren's answer, correct? He has no +/-, but 11 tie-breakers. \$\endgroup\$ – Gaffi Apr 12 '12 at 15:45
  • \$\begingroup\$ Yes, you are tied with him. \$\endgroup\$ – PhiNotPi Apr 12 '12 at 15:50
0
\$\begingroup\$

PHP, 22 chars

echo array_sum($argv);

Documentation: array_sum() and $argv
Usage: php -r 'echo array_sum($argv);' 5 6 will output 11.

\$\endgroup\$
  • \$\begingroup\$ +/- and above mentioned tiebreakers are interesting, not chars. \$\endgroup\$ – user unknown May 30 '12 at 3:35
0
\$\begingroup\$

APL (10, 3 tie-breakers)

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

Common Lisp

Always surprising for such a verbose language.

((lambda(n m)(princ(length(append(make-list n)(make-list m)))))(read)(read))
\$\endgroup\$
0
\$\begingroup\$

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)
\$\endgroup\$
  • \$\begingroup\$ +/- tiebreakers are interesting, not characters. \$\endgroup\$ – user unknown May 30 '12 at 3:33
  • \$\begingroup\$ @userunknown: What's wrong with characters? \$\endgroup\$ – JAB May 30 '12 at 16:55
  • \$\begingroup\$ 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. \$\endgroup\$ – user unknown May 30 '12 at 20:41
  • \$\begingroup\$ @userunknown: Oh, right. \$\endgroup\$ – JAB May 30 '12 at 20:46
0
\$\begingroup\$

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());
\$\endgroup\$
0
\$\begingroup\$

Python 42

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

Seven tie breakers, but those are string operations

\$\endgroup\$
0
\$\begingroup\$

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), "")
    );
  }
}
\$\endgroup\$
0
\$\begingroup\$

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)

\$\endgroup\$
0
\$\begingroup\$

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:

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

Javascript (ES6) - 8 Tie-breaker characters

add: {
    let a = prompt``|false;
    let b = prompt``|false;
    while (a & b) {
        a = a ^ b;
        b = [b & ~a][false|false] << true
    }
    alert(a | b);
}
  • 2 * (
  • 2 * )
  • 4 * =
\$\endgroup\$
0
\$\begingroup\$

Runic Enchantments, (no +/-, 1 tie-breaker, 11 bytes)

'V2,k!$wi|;

Try it online!

Not allowed to have + characters? Fine, I'll create them myself with math and reflection. V has the byte value 86, which gets divided by 2 (the , and only tie-breaker character) converted to a character, and finally, reflectively written into the program under the instruction pointer. It then reads input, hits a reflector (|), reads input again, then reaches the space originally containing a w and adds them together before printing. The IP then performs an illegal action at , and is silently terminated.

Is this cheating? Probably. But the challenge says "don't use + characters" and the source code contains 0 of them.

Alternate method, (no +/-, 3 tie-breakers, 16 bytes)

"ab"1,i*}i*qul$;

Try it online!

Makes two strings, one of length x and one of length y, concatenates them together, then computes the length of the string. Does this count as a "sum-like function"? Perhaps. + on two strings does do the same thing.

\$\endgroup\$
0
\$\begingroup\$

APL (Dyalog Unicode), 4 bytesSBCS, 0 +/-, 0 tie breakers

Can sum any number of non-negative integers.

≢⎕⌿#

Try it online!

# reference to the root object

⎕/ replicate by the number(s) from STDIN
for each number, we get that many copies of the corresponding element on the right, but since there is only one, all numbers get paired up with that one, forming a single list of N1+N2+…+Nn references

 tally

Output is implicitly to STDOUT.

\$\endgroup\$
0
\$\begingroup\$

Gol><>, 12 bytes

IITM:{P}?trh

Thanks to JoKing for reminding me there was a Teleport Pad!!

1st version, 25 bytes

II!/M:{P}?\rh
   \     :/

Try it online!

This was alot easier than I thought it would be!!! I will be golfing this alot more!!!

\$\endgroup\$
  • \$\begingroup\$ 12 bytes using teleports, 10 bytes using put. Or 13 bytes without tie breaker characters \$\endgroup\$ – Jo King Feb 10 at 3:02
  • \$\begingroup\$ @JoKing Wow. Those use strategies I wasn't even thinking of!!!, nice! \$\endgroup\$ – KrystosTheOverlord Feb 10 at 3:44
0
\$\begingroup\$

MathGolf, 3 bytes, 0 +/-

α▓∞

Try it online!

While perhaps violating the spirit of the "no sum" rule, this solution wraps the inputs into an array, calculates the average of the values in that array, and duplicates the result. Of course, the avg operator uses a sum internally, but its behavior is inherently different compared to the sum operator Σ.

The output is printed as a float, with .0 at the end. If this is not acceptable, just add i to the end to convert it back into an integer.

Explanation

α     wrap last two elements in array
 ▓    get average of list
  ∞   pop a, push 2*a
\$\endgroup\$
0
\$\begingroup\$

Befunge-98 (FBBI), 4 bytes, one +.

&&+.

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Surely you could use p to place a + command? \$\endgroup\$ – negative seven Sep 19 at 15:30
  • \$\begingroup\$ @negativeseven I just made it for my Seed answer below. \$\endgroup\$ – Krzysztof Szewczyk Sep 19 at 15:31
0
\$\begingroup\$

Befunge-98 (PyFunge), 0 +/- characters, 0 tiebreaker characters, 47 bytes

brqzzzzzzzzzzzzzzzzzzzpyadgyacpyappyacybyabybay

Try it online!

Made all out letters, because why not?

More readable variant that does pretty much the same thing:

b<@                               &&pyapyabybay

Abuses the y GetSysInfo command, which can be used to retrieve the instruction pointer's current X coordinate. This is used to push the ASCII codes for + (add) and . (output), which later get put onto the field.

\$\endgroup\$
0
\$\begingroup\$

Ruby -na0, no +/-, no tiebreakers, 60 bytes

$F<<?a until~%r[
]&&$F::join[%r[a{#$`}a{#$'}]]
p$F::count ?a

Try it online!

Actually harder than I thought, I'm almost certainly missing a trick.

Reads in the input, then adds a characters to the argument array until the array, when joined into a string, matches a regular expression that uses the two input numbers as quantifiers, then counts how many it added.

Syntax tricks include using %r[] instead of // for a regexp, and :: instead . for a method call.

\$\endgroup\$
0
\$\begingroup\$

Zsh, 26 bytes, 7 tiebreakers

Positional parameters are pretty punishing

x=({1..$1} {1..$2})
<<<$#x

Try it online!


62 bytes, 0 tiebreakers

read a b
{repeat $a;printf x;repeat $b;printf x}|read x
<<<$#x

Try it online!

\$\endgroup\$
0
\$\begingroup\$

C++, () only for main

#include <iostream>

int main()
{
    unsigned int a, b;
    std::cin >> a;
    std::cin >> b;


    unsigned int p = a ^ b;
    unsigned int g = a & b;

    unsigned int gg {g | p & g << 1};
    unsigned int pp {p & p << 1};

    unsigned int ggg {gg | pp & gg << 2};
    unsigned int ppp {pp | pp << 2};

    unsigned int gggg {ggg | ppp & ggg << 4};
    unsigned int pppp {ppp & ppp << 4};

    unsigned int ggggg {gggg | pppp & gggg << 8};
    unsigned int ppppp {pppp | pppp << 8};

    unsigned int gggggg {ggggg | ppppp & ggggg << 16};

    unsigned int result {a ^ b ^ gggggg << 1};
    std::cout << result;
}

Assumes 32-bit unsigned ints. Very ugly code to remove tiebreak characters. Emulates a Kogge-Stone adder.

\$\endgroup\$
0
\$\begingroup\$

Ruby, 0 +/-, 0 tie-breakers, 39 bytes

And fairly unreadable.

p"#{"%#{"%ss%%%ss"%$F}"%%w[o o]}"::size

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Octave/MATLAB, 37 bytes

f=@()e^(input(''));disp(log(f()*f()))

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to the site! I'd recommend you split these up into two answers so that users can vote and improve them separately. \$\endgroup\$ – caird coinheringaahing Sep 20 at 14:49
0
\$\begingroup\$

Python 3

import numpy as n;i=lambda:n.e**int(input());print(int(n.log(i()*i())))

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ I was trying to remove the log and e but forgot about the original restrictions... :( Reverted now. \$\endgroup\$ – Hunaphu Sep 20 at 15:12
  • \$\begingroup\$ Okay it's working now. You should probably include the link in your answer: Try it online! \$\endgroup\$ – Grimy Sep 20 at 15:13
  • \$\begingroup\$ Welcome! Please consider adding an explanation or a link to an online interpreter. Code-only answers tend to be automatically flagged as low-quality. \$\endgroup\$ – mbomb007 Sep 20 at 15:43
0
\$\begingroup\$

Gol><>, 8 bytes

IRmIRmlh

Minus one byte!, I figured out that I didn't need to take both inputs first

Try it online!

Previous answer, 9 bytes

IIRfrRflh

Explanation below

II        take both inputs
  Rm      Pop and repeat that number of times, pushing -1
    r     Reverse stack (to get the first num on top)
     Rm   Pop and repeat that number of times, pushing -1
       lh Push length of stack, and output as a number halting

Basically what I did was count it all out in unary, it would only take a few more bytes to expand it to be able to handle more than two numbers

Try it online!

\$\endgroup\$
0
\$\begingroup\$

JavaScript (ES6), 48 bytes, 12 tie-breakers

a=>b=>"0".repeat(a).concat("0".repeat(b)).length
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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