# 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"

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. ## 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-