# Diamond Puzzles!

Explanation:

Last year in math class, on homework we would occasionally get these extremely simple, although equally annoying questions called diamond puzzles. These were basically questions where we would be given a sum, and a product then were asked to find the two numbers which when multiplied give the product, and when added give the sum. These drove me crazy, since the only way I knew how to solve them (in Algebra I) was to just list the factors of the product then see which ones added to make the sum. (Since I didn't know how to use Quadratics at the time) Not to mention, they weren't exactly challenging math. However, It just occured to me that I should have just written a program. So that is your challenge today! Write a program that can solve a diamond puzzle.

Examples Apologies for the blurry image, its the best I could find. Also, ignore the numbers in bubbles.The top of the diamond is the product, the bottom is the sum, the right and left are the two numbers. Answers are as follows: (These are also your test cases)

1. 9, -7
2. -2, -1
3. 5, 8
4. -9, -9

Rules:

• You may not use any pre-defined functions or classes which accomplish this for you.
• Your code must be a complete program, or function which either returns or prints the answers once it finds them
• The input is the sum and product, which are inputted as a function parameters or user input

Specifications:

• Assume that the two numbers, the sum, and the product will always be an integer.
• The two answers will both be between -127 to 127.
• Your input will be two integers (Sum and Product).

Remember this is code-golf, so shortest byte count wins. Please title your answer with the standard ##Language Name, Byte Count

Edit: Also, Doorknob pointed out that this is essentially "factor a quadratic of form x^2 + bx + c,". That is another way to think about and approach this challenge. :D

• This is essentially "factor a quadratic of form x^2 + bx + c," correct? – Doorknob Dec 28 '15 at 15:47
• b = -(x+y), c = (x*y) – TheNumberOne Dec 28 '15 at 15:50
• Simplifying (x + n)(x + m) gives you x^2 + (n+m)x + (n*m), so factoring said quadratic is basically equivalent to this question (if I'm understanding it correctly). – Doorknob Dec 28 '15 at 15:51
• @Doorknob冰 yeah you are correct. I am guessing I'm about to be marked as a duplicate. :( – Ashwin Gupta Dec 28 '15 at 15:52
• Well, I don't think we have a "factor x^2+bx+c" question yet anyway. Just pointing out that the problems are very similar. – Doorknob Dec 28 '15 at 15:52

# Jelly, 1511 10 bytes

Hð+,_ðH²_½


Try it online!

The following binary code works with this version of the Jelly interpreter.

0000000: 48 98 2b 2c 5f 98 48 8a 5f 90  H.+,_.H._.


### Idea

This is based on the fact that

### Code

Hð+,_ðH²_½    Left input: s -- Right input: p

ð   ð        This is a link fork. We define three links, call the left and right
link with the input as arguments, then the middle link with the
results as arguments.

H             Halve the left input.

H       Halve the left input.
²      Square the result.
_     Hook; subtract the right input from the result.
½    Apply square root to the difference.

+           Compute the sum of the results.
_         Compute the difference of the results.
,          Pair.

• That is a very nice online compiler. I wish they had one like that for java. – Ashwin Gupta Dec 28 '15 at 16:04
• @AshwinGupta Dennis made that himself, actually ;) For Java, there's always Ideone. – Doorknob Dec 28 '15 at 16:09
• @Doorknob冰 yeah I know his online compiler is so cool. I have been on Ideone, its good and all but it doesn't have multiclass support which is a problem. I have no way to test my more complex programs at school D:. Plus, it doesn't work on an iPad browser either which believe it or not has to be a requirement since that is the computing device we have been provided with... – Ashwin Gupta Dec 28 '15 at 16:10
• Unless someone can beat 14, I think you won. – Ashwin Gupta Dec 28 '15 at 16:18
• OK, Jelly is now officially insanely short. I've got to learn this language right after I finish wrapping my head around Pyth... – ETHproductions Dec 28 '15 at 21:14

# Unicorn, 465029821874 1546

[ ✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨ 🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄 ( 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈 🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈 ✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨ 🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤 🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈 ( ) ) 🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄 2 🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄 🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄 🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈 🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐🐐 🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄 🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄 ✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨ 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈 ( 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈 ✨✨✨✨✨✨✨ 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈 🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄 4 🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈 ✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨ 🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤 🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄🦄 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈 ( ) ) 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈 🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤🌤 ✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨✨ ]


Now with goats, rainbows, and sparkles!

Hopefully shorter than Java.

Uses a custom encoding which can be applied with ApplyEncoding

## Explanation

How does this work??? With the magic of unicorns (and a little code).

Unicorn is compiled into JavaScript

Each section is separated by a space, and each section represents a character in the JavaScript code.

If the section contains unicorns, the section's character is the section's length, converted to a char code (e.g. 32 unicorns would be a space)

If the section contains goats, the section's length is doubled and then converted to a char code.

If the program's special chars don't show, here's a picture:

This is non competing because Unicorn was made after this challenge was posted.

• What on earth is going on here? I just see boxes. ;-; – Lynn Dec 28 '15 at 20:03
• @Mauris aww :( I'll post an image of the program so everyone can see it's glory – Downgoat Dec 28 '15 at 20:03
• LOL okay this is my favorite even though I can't accept it since it is so darn long. – Ashwin Gupta Dec 28 '15 at 20:15
• "Hopefully shorter than Java." You deserve a +10 just for that ;) – ETHproductions Dec 29 '15 at 2:52
• @Doᴡɴɢᴏᴀᴛ, sorry to disappoint you. Java is about 18 times shorter than that. – Tamoghna Chowdhury Dec 30 '15 at 6:44

# JavaScript ES6, 4539 37* bytes

(q,p)=>[x=p/2+Math.sqrt(p*p/4-q),p-x]


* Thanks to Dennis!

• I'm +1 ing you because this is the first answer I can actually understand (sort of). LOL – Ashwin Gupta Dec 28 '15 at 16:22

# dc, 16

?ddd*?4*-v+2/p-p


Reads sum then product from separate lines of STDIN. -ve numbers must be entered with an underscore instead of a minus sign. e.g.

${ echo 2; echo _63; } | dc -e'?ddd*?4*-v+2/p-p' 9 -7$


### Explanation:

Same basic quadratic solution for sum = a + b and product = a * b. This calculates solution a as:

a = [ sum + √( sum² - 4 * product ) ] / 2


And calculates solution b as:

b = sum - a


### Expanded:

?                   # push sum to stack
ddd                # duplicate 3 times (total 4 copies)
*               # sum squared
?              # push product to stack
4*            # multiply by 4
-           # subtract (4 * product) from (sum squared)
v          # take square root of (sum squared) - (4 * product)
+         # add sum to square root of (sum squared) - (4 * product)
2/       # divide by 2 to give solution a
p      # print with newline and without pop
-     # subtract solution a from sum to give solution b
p    # print with newline and without pop


Dividing by 2 is done late to prevent loss precision. It is possible to divide by 2 earlier, but this requires fractional precision which needs more characters.

• Wait, what? This ended up shorter than APL, Pyth and CJam??? Woohoo! – Digital Trauma Dec 29 '15 at 3:24

# TeaScript, 22 bytes 30 31

[d=y/2+$s(y*y/4-x),y-d  Not that bad. Would be much shorter if I could of gotten some golfing features finished earlier such as unicorn unicode shortcuts Try it online • "unicorn shortcuts" I like the sound of that! ;) – ETHproductions Dec 28 '15 at 19:04 • @ETHproductions haha, that was autocorrect :p though unicorn shortcuts could be interesting... – Downgoat Dec 28 '15 at 19:06 # MATL, 33 bytes -100:100t!2$t+i=bb*i=&2#2$1fv101-  Outputs the two numbers in two different lines. If no solution exists it produces no output. If several solutions exist it produces only the pair of numbers corresponding to one solution. ### Example The following was run in Octave with the current GitHub commit of the compiler. >> matl -r '-100:100t!2$t+i=bb*i=&2#2$1fv101-' > 2 > -63 9 -7  ### Explanation -100:100 % row vector -100, -99, ..., 100 t! % duplicate and transpose into column vector 2$t                % duplicate the two vectors
+                  % sum with singleton expansion (all combinations)
i=                 % does it equal input? Produces logical matrix
bb                 % move the two vectors to top
*                  % multiply with singleton expansion (all combinations)
i=                 % does it equal input? Produces logical matrix
&                  % logical "and"
2#2$1f % find row and column of the first "true" value in logical matrix v101- % concatenate vertically and subtract 101  • Out of curiosity, would the program in "explanation" form actually run on a MATL compiler? The syntax is awfully split. – Ashwin Gupta Dec 28 '15 at 16:17 • Yes, you can paste the program in separate lines, for example copying the listing from the explanation. You type matl and press "enter"; then paste the program and finish with a blank line. What do you mean the syntax is awfully split? MATL uses reverse polish (postfix) notation, perhaps that's confusing you? – Luis Mendo Dec 28 '15 at 16:27 # Julia, 4644 32 bytes f(b,c)=(x=b+√(b^2-4c))/2,b-x/2  An function f that takes the sum and then the product. My first Julia answer. @AlexA., you should be proud of me. Thanks @Dennis and @Alex A. for all the help. I got to cross out the 44. :P • 40 bytes: f(b,c)=b/2+√(b^2/4-c),b/2-√(b^2/4-c) – Alex A. Dec 28 '15 at 19:31 • @AlexA. - You're going to make a meme happen by suggesting improvements to this one... – Darrel Hoffman Dec 28 '15 at 19:34 • 34 bytes: f(b,c)=b/2+√(x=b^2/4-c),b/2-√x – Alex A. Dec 28 '15 at 19:34 • 32 bytes: f(b,c)=(x=b+√(b^2-4c))/2,b-x/2 – Dennis Dec 28 '15 at 20:23 • It's just like the regular quadratic formula that's been beaten into our heads since we were but wee babbies. – Alex A. Dec 28 '15 at 20:32 # Pyth, 21 18 bytes Saved 3 bytes thanks to @Dennis ,J/+@-^Q2*4E2Q2-QJ  Test suite My second Pyth program ever, so it can probably be golfed with built-ins. Suggestions are welcome! ### How it works ,J/+@-^Q2*4E2Q2-QJ Implicit: Q = first line of input , Create and output a list of these items: J Set variable J to the result of these operations: ^Q2 Square Q. - *4E Subtract 4*(next line of input). @ 2 Take the square root. + Q Add Q. / 2 Divide by 2. The list is currently [J]. -QJ Push Q-J; the list is now [J, Q-J]. EOF; list is sent to output.  (This explanation may not be 100% correct; I'm not very familiar with Pyth.) Note that / is integer division. By replacing it with c, we could make this work with non-integer inputs as well. • Beaten you by one byte :P You should consider switching ;D – nicael Dec 28 '15 at 19:58 • This explanation may not be 100% correct...??? – Digital Trauma Dec 29 '15 at 3:54 • @DigitalTrauma I'm not 100% certain that's how it works (specifically the J), but it's what I gathered from reading the docs. – ETHproductions Dec 29 '15 at 4:03 • Yes, J is an auto-assigning variable and gets set the first time it is used. The only part that doesn't seem quite right is The list is currently [J]. , takes exactly two arguments and combines them in a list. – Dennis Dec 29 '15 at 4:44 # Java , 82 (69 λ) bytes with quadratic formula (127 (114 λ) bytes brute-force) Brute-Force: (Vanilla, Java 7) int[] n(int s,int p){for(int a=-100;a<=100;a++)for(int b=-100;b<=100;b++) if(a+b==s&&a*b==p)return new int[]{a,b};return null;}  λ-enhanced: (Java 8) (s,p)->{for(int a=-100;a<=100;a++)for(int b=-100;b<=100;b++) if(a+b==s&&a*b==p)return new int[]{a,b};return null;}  Assign lambda to java.util.function.BiFunction<Integer, Integer, int[]> and call apply(). Plain old brute-force approach. Just the working function is here, and since Java can't return multiple values, we return a 2-element int array with the required numbers. The full brute-force approach based program can be found here on ideone.com, with λ version here. Golfing this involved removing all unnecessary braces. Ungolfed: int[] n(int s,int p){//sum and product as function parameters,in that order for(int a=-100;a<=100;a++){//iterate first no. from -100 to 100 for(int b=-100;b<=100;b++){//iterate second no. from -100 to 100 //if the 2 nos. satisfy the diamond-puzzle condition, //pack them in an int array and return them if(a+b==s&&a*b==p)return new int[]{a,b};} }//if no such pair exists, return null return null;}  Quadratic Approach: (Vanilla, Java 7) int[] n(int s,int p){int x=s+(int)Math.sqrt(s*s-4*p);return new int[]{x/2,s-x/2};}  λ-enhanced: (Java 8) (s,p)->{int x=s+(int)Math.sqrt(s*s-4*p);return new int[]{x/2,s-x/2};} (Usage as for brute-force λ above). Parameters and return criteria are the same as the brute-force solution above. Uses the good old quadratic formula used by almost all other answers here, and cannot be golfed down much further unless someone helps me out here. It's pretty clear so I'm not including an ungolfed version. The full quadratic approach based program is here on ideone.com, with λ version here. • AHHA yes thank you. I was waiting for the Java answer. Also, I was waiting for someone to use for loops and brute force with the factors. I guess I got the two for one deal. +1 – Ashwin Gupta Dec 30 '15 at 17:01 • Java beat unicorn :( I'd +1 but I've used my daily vote limit – Downgoat Dec 30 '15 at 18:17 ## Japt, 282221 20 bytes [X=V/2+(V²/4-U ¬V-X]  Input is made in the form of -63 2. Explanation: • U and V are the two inputs (-63 and 2 in the first case) • ² squares the number • q extracts the square root • I was just writing up an answer for this; that is, until I saw yours. Nice work! By rearranging the first part and using a couple more Unicode shortcuts, we can achieve 21 bytes: [X=ºV²-4*U ¬+V)/2V-X] Without the shortcuts: [X=((V²-4*U q +V)/2V-X] I should really make the trailing ] unnecessary in the next version... – ETHproductions Dec 28 '15 at 19:12 • @Eth Ok, ¬ is a great thing. – nicael Dec 28 '15 at 19:57 # APL, 27 21 bytes h(+,-).5*⍨⊣-⍨h×h←.5×⊢  This is a dyadic function train that accepts integers on the right and left and returns an array. To call it, assign it to a variable. Ungolfed:  h←.5×⊢ ⍝ Define a train h for halving the input h× ⍝ b^2/4 ⊣-⍨ ⍝ b^2/4 - c .5*⍨ ⍝ sqrt(b^2/4 - c) h(+,-) ⍝ Return the halved pair  Try it online Saved 6 bytes thanks to Dennis! # CJam, 18 bytes q~2d/_2#@-mq_2$+p-


Try it online!

### How it works

q~                  e# Read an evaluate all input. STACK: product sum
2d/               e# Divide the sum by 2.0.
_              e# Push a copy of the result.
2#            e# Square the copy.
@-          e# Rotate the product on top and subtract it from the square.
mq        e# Apply square root.
_2$e# Push copies of the root and the halved sum. +p e# Add and print. - e# Subtract the originals.  # Python 3, 49 44 bytes There are probably some ways to golf this down even further, but this looks pretty good as is. def f(s,p):s/=2;d=(s*s-p)**.5;return s+d,s-d  • The sign of the difference is incorrect; it should be s/2-d. Also, d=(s*s/4-p)**.5 saves a few bytes. – Dennis Dec 29 '15 at 18:40 • @Dennis Oops, you're right. Fixed and golfed. – Sherlock9 Dec 29 '15 at 18:53 MathCAD 15. 38 Bytes With a mathematical formula, programming in MathCAD is easy. The language is even designed to handle complex numbers with ease. However there are shorter languages that can solve the problem. # 𝔼𝕊𝕄𝕚𝕟, 21 chars / 30 bytes [x=í/2+√ í²/4-î⦆,í-x]  Try it here (Firefox only). Meh. This should be visual enough for y'all to get the idea; however, if you must, î = input1, í = input2. # J, 12 bytes 1-@>@{p.@,&1  Try it online! # APL (Dyalog Unicode), 14 bytesSBCS ⊣(⊢,-)(⊢×-)⍣¯1  Try it online! A tacit function whose left arg is the sum and the right is the product. Finds one solution to the quadratic equation using the built-in numerical solver, and then gives both solutions based on the result. The "power operator" f⍣n runs the function f n times, but if n is negative, runs the inverse of f -n times. The inverse of f is, in the numerical point of view, equivalent to solving the equation f x = y with a given value of y. Dyadic form a(f⍣n)y solves (a f x) = y. ### How it works ⊣(⊢,-)(⊢×-)⍣¯1 ⍝ left: sum s, right: product p (⊢×-) ⍝ A function that, when left-bound with s, computes x*(s-x) ⍣¯1 ⍝ Find one solution to x*(s-x)=p, i.e. x^2-sx+p=0 ⊣( ) ⍝ With s as left and x as right arg, ⊢,- ⍝ Give (x,s-x)  ## PHP, 62 bytes <?=($x=($p=$_GET[p])/2+sqrt($p*$p/4-$q=$_GET[q]))." ".($p-$x);


This could be pretty long but being full-featured PHP web-"program". Accepts the arguments via the "get" request.

Demo.

# TI-BASIC, 20 bytes

Takes Q from Ans, and P from Prompt. Call like P:prgmNAME.

Prompt P
P/2+√(P²/4-Ans
{Ans,P-Ans

• Very nice. Too bad for that Prompt statement being so long lol. I don't know TI-BASIC, but it could be shorter if you were to put the code in a function and pass P as a parameter. – Ashwin Gupta Jan 26 '16 at 1:29
• Actually, TI-BASIC is divided into "tokens"; Each of Prompt , P, /, 2, +, √(, ², 4, -, Ans, and { are one token, and each of these tokens is one byte. Also, TI-BASIC does not have functions. This is probably the shortest method. – Conor O'Brien Jan 26 '16 at 1:30
• Oh well thats intresting, I didn't realize that. But isn't each char a byte still in a technical sense? Like if I were to copy paste this into a text doc, I get 36 bytes. Or does this use special Unicode characters? – Ashwin Gupta Jan 26 '16 at 1:33
• TI-BASIC is a calculator language, so this is what one sees. It is keyed into the calculator, not a text document or the like. Here are the one-byte tokens. – Conor O'Brien Jan 26 '16 at 1:35
• Ah okay I got you. Cool. I just was curious. – Ashwin Gupta Jan 26 '16 at 1:35

## wxd, 14 12 bytes

Just W with a few instructions added.

○Äπ√akû▀╧Θ°v


Decompressed:

a*S4*-Q+2/:aS-