29
\$\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\$
10
  • 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
  • 58
    \$\begingroup\$ 100 rep bounty for anybody who can do this in Brainfuck. \$\endgroup\$ Dec 1 '11 at 5:57
  • 4
    \$\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
  • 2
    \$\begingroup\$ Next challenge would be "Comparing two numbers without any of ><+-*/%&|" \$\endgroup\$
    – Naruyoko
    Sep 19 '19 at 23:41

72 Answers 72

1 2
3
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\$
2
  • \$\begingroup\$ 12 bytes using teleports, 10 bytes using put. Or 13 bytes without tie breaker characters \$\endgroup\$
    – Jo King
    Feb 10 '19 at 3:02
  • \$\begingroup\$ @JoKing Wow. Those use strategies I wasn't even thinking of!!!, nice! \$\endgroup\$ Feb 10 '19 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\$
2
  • \$\begingroup\$ Surely you could use p to place a + command? \$\endgroup\$ Sep 19 '19 at 15:30
  • \$\begingroup\$ @negativeseven I just made it for my Seed answer below. \$\endgroup\$ Sep 19 '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\$

C (gcc), 3 tiebreakers

I take no credit for this, as this is not an original answer, but merely an amalgamation of the following answers to this same challenge:

https://codegolf.stackexchange.com/a/5701/97511, https://codegolf.stackexchange.com/a/4613/97511

#include <stdio.h>

#define OP (
#define CP )
#define CO ,

int main OP ac CO av CP int ac; typeof OP "" CP av; {
    scanf OP "%d\n%d" CO &ac CO &av CP;
    return printf OP "%d\n" CO &av[ac] CP;
}

Try it online!

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

brainfuck, 11 bytes, 2 +-, 3 tie breaker

,>,[-<+>]<.

Sadly i can’t think up anyway of manipulating number without +-

Try it online!

\$\endgroup\$
4
  • \$\begingroup\$ If + and - are used, it is simply invalid. \$\endgroup\$
    – Razetime
    May 11 '21 at 2:35
  • \$\begingroup\$ @Razetime It's mentioned in question that use as few +- a possible \$\endgroup\$
    – okie
    May 11 '21 at 6:07
  • \$\begingroup\$ ah, it isn't too clear or exact about what's banned. I suppose it's ok \$\endgroup\$
    – Razetime
    May 11 '21 at 6:09
  • \$\begingroup\$ @Razetime This answer is bad anyway, I just can't think up anyway to use 1 +- only. \$\endgroup\$
    – okie
    May 11 '21 at 6:11
0
\$\begingroup\$

Raku, 0 tie-breaker characters

say ½−½−$_ given ½−½−get−get

Try it online!

here is the Unicode character MINUS SIGN at codepoint 8722, rather than the ASCII/Unicode character -, HYPHEN-MINUS, at codepoint 45. The two are synonymous in Raku, but the former is by no stretch the "standard" subtraction operator.

½−½− is how I'm negating numbers without using any of the tiebreaking characters. It could be done with just a single leading MINUS SIGN, but that could arguably be considered a "negation function."

\$\endgroup\$
1 2
3

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.