# Creating Distinct Sums

You should write a program or function which receives an integers as input and outputs or returns two integers whose sum is the first one.

There is one further requirement: no number can be part of the output for two different inputs.

## Details

• You should be able to handle inputs for at least the range -32768 .. 32767 (inclusive).
• If your datatype can't handle arbitrary whole numbers, that is fine but your algorithm should work for arbitrary large and small numbers in theory.

## Examples

Each block shows a part of a correct or incorrect solution in the format of input => output.

1 => 6 -5
2 => -2 4
15 => 20 -5

Incorrect, as -5 is used in two outputs.


-5 => -15 10
0 => 0 0
1 => 5 6
2 => -5 7

Incorrect, as 5 + 6 isn't 1.


-1 => -1 0
0 => 6 -6
2 => 1 1

Can be correct if other outputs doesn't collide.


This is code golf so the shortest entry wins.

• Can you restrict the input range to -32768 .. 32767 so we don't have to use 17 bit integers? – FUZxxl Jul 18 '15 at 17:27
• @FUZxxl My bad, that was the intention. Fixed. – randomra Jul 18 '15 at 17:29
• Can the output be a list/array/tuple/set/etc containing two integers? ( For example, f(1) => [2, -1] ) – laurencevs Jul 18 '15 at 17:34
• There seem to be several solutions which fundamentally rely on a restricted integer size--for example, by multiplying the input by a large positive and a large negative number. It seems to me that such solutions are failing the requirement that "your algorithm should work for arbitrary large and small numbers in theory". Am I misreading the question? – mathmandan Jul 18 '15 at 23:55

# Pyth, 8 bytes

_J^Q3+QJ


Demonstration. Equivalent to the Python 2 code:

Q=input()
J=Q**3
print -J
print Q+J


So, the output has form (-n**3, n+n**3)

Some outputs:

-5 (125, -130)
-4 (64, -68)
-3 (27, -30)
-2 (8, -10)
-1 (1, -2)
0 (0, 0)
1 (-1, 2)
2 (-8, 10)
3 (-27, 30)
4 (-64, 68)
5 (-125, 130)


These are distinct because cubes are far enough spaced that adding n to n**3 is not enough to cross the gap to the next cube :n**3 < n+n**3 < (n+1)**3 for positive n, and symmetrically for negative n.

• You don't need the , at the beginning, two lines appears to be allowed. – Maltysen Jul 18 '15 at 20:49
• @Maltysen I tried removing it, but only the second number prints. Maybe the J assignment suppresses printing? – xnor Jul 18 '15 at 20:51
• Oh yeah you're right, sorry. – Maltysen Jul 18 '15 at 20:51
• - in pyth is not the unary negation operator, its _, so _J^Q3+QJ works as expected. – Maltysen Jul 18 '15 at 20:56
• @Maltysen Actually, that works, I just need the J to not be on the outside. Thanks for poking me about this. – xnor Jul 18 '15 at 20:56

# Snowman 0.1.0, 101 chars

}vg0aa@@*45,eQ.:?}0AaG0NdE;:?}1;bI%10sB%nM2np*%.*#NaBna!*+#@~%@0nG\]:.;:;bI~0-NdEnMtSsP" "sP.tSsP


Input on STDIN, space-separated output on STDOUT.

This uses the same method as isaacg's answer.

Commented version with newlines, for "readability":

}vg0aa          // get input, take the first char
@@*45,eQ.       // check if it's a 45 (ASCII for -) (we also discard the 0 here)
// this is an if-else
:               // (if)
?}0AaG        // remove first char of input (the negative sign)
0NdE        // store a -1 in variable e, set active vars to beg
;
:               // (else)
?}1           // store a 1 in variable e, set active vars to beg
;bI             // active variables are now guaranteed to be beg
%10sB           // parse input as number (from-base with base 10)
%nM             // multiply by either 1 or -1, as stored in var e earlier
2np*          // raise to the power of 2 (and discard the 2)
%.              // now we have the original number in b, its square in d, and
//   active vars are bdg
*#NaBna!*+#     // add abs(input number) to the square (without modifying the
//   input variable, by juggling around permavars)
@~%@0nG\]       // active vars are now abcfh, and we have (0>n) in c (where n is
//   the input number)
:.;:;bI         // if n is negative, swap d (n^2) and g (n^2+n)
~0-NdEnM        // multiply d by -1 (d is n^2 if n is positive, n^2+n otherwise)
tSsP            // print d
" "sP           // print a space
.tSsP           // print g


Commentary on the very first Snowman solution on PPCG: I think my design goal of making my language as confusing as possible has been achieved.

This actually could have been a lot shorter, but I'm an idiot and forgot to implement negative numbers for string -> number parsing. So I had to manually check whether there was a - as the first character and remove it if so.

• Way better than Brainfuck. – phase Jul 18 '15 at 19:34

# Pyth, 15 11 bytes

4 bytes thanks to @Jakube

*RQ,hJ.aQ_J


Demonstration.

This maps as follows:

0  -> 0, 0
1  -> 2, -1
-1 -> -2, 1
2  -> 6, -4
-2 -> -6, 4


And so, on, always involving n^2 and n^2 + n, plus or minus.

# APL, 15 bytes

{(-⍵*3)(⍵+⍵*3)}


This creates an unnamed monadic function that returns the pair -n^3 (-⍵*3), n+n^3 (⍵+⍵*3).

You can try it online.

# Pyth - 11 10 bytes

Just multiplies by 10e10 and -10e10+1 Thanks to @xnor for showing me that I could use CG for the number.

*CGQ_*tCGQ

• You can make a suitably huge number as CG. – xnor Jul 18 '15 at 20:44
• @xnor adding to tips list. – Maltysen Jul 19 '15 at 1:47

# O, 17 15 9 bytes

Uses some new features of O.

Q3^.Q+p_p

Older Version

[i#.Z3^*\Z3^)_*]o
• I'm starting to enjoy these O answers, though I would like it more if the interpreter wasn't written in Java... ;) – kirbyfan64sos Jul 19 '15 at 18:30
• @kirbyfan64sos It's not as small as Pyth, but it can beat CJam & GolfScript in some instances. It defiantly can beat anything having to do with arrays, as they're so powerful. – phase Jul 19 '15 at 19:21

# Python 3, 29 27

## Edit: doesn't meet the requirement in the 2nd "Details" bullet point

Bonus: it works from -99998 to 99998 inclusive

lambda n:[99999*n,-99998*n]


This creates an anonymous function*, which you can use by enclosing in brackets and then placing the argument in brackets afterwards like this:

(lambda n:[99999*n,-99998*n])(arg)


*Thanks to @vioz- for suggesting this.

Example input / output:

>>> (lambda n:[99999*n,-99998*n])(1)
[99999, -99998]
>>> (lambda n:[99999*n,-99998*n])(2)
[199998, -199996]
>>> (lambda n:[99999*n,-99998*n])(0)
[0, 0]
>>> (lambda n:[99999*n,-99998*n])(-1)
[-99999, 99998]
>>> (lambda n:[99999*n,-99998*n])(-2)
[-199998, 199996]
>>> (lambda n:[99999*n,-99998*n])(65536)
[6553534464, -6553468928]

• Nice post! Just so you know, you can remove the f= and leave it as an anonymous function, which is still a valid answer. Then you can take down your byte count to 27 :) – Kade Jul 18 '15 at 19:35
• "...your algorithm should work for arbitrary large and small numbers in theory." Obviously (lambda n:[99999*n,-99998*n])(99999) and (lambda n:[99999*n,-99998*n])(-99998) will collide in theory (and in practice). – mathmandan Jul 18 '15 at 23:36
• @mathmandan You're right, I'll edit my post to make it clear that it doesn't meet the requirements. I would try to write & test new code but I'm on mobile away from my computer. – laurencevs Jul 19 '15 at 0:03

f x=(-x^3,x^3+x)