21
\$\begingroup\$

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 enter image description here 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

\$\endgroup\$
  • 9
    \$\begingroup\$ This is essentially "factor a quadratic of form x^2 + bx + c," correct? \$\endgroup\$ – Doorknob Dec 28 '15 at 15:47
  • 1
    \$\begingroup\$ b = -(x+y), c = (x*y) \$\endgroup\$ – TheNumberOne Dec 28 '15 at 15:50
  • \$\begingroup\$ 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). \$\endgroup\$ – Doorknob Dec 28 '15 at 15:51
  • \$\begingroup\$ @Doorknob冰 yeah you are correct. I am guessing I'm about to be marked as a duplicate. :( \$\endgroup\$ – Ashwin Gupta Dec 28 '15 at 15:52
  • \$\begingroup\$ 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. \$\endgroup\$ – Doorknob Dec 28 '15 at 15:52

17 Answers 17

18
\$\begingroup\$

Jelly, 15 11 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

formula

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             Left link, dyadic. Arguments: s p

H             Halve the left input.

     ðH²_½    Right link, dyadic. Arguments: s p

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

 ð+,_         Middle link, dyadic. Arguments: (results of the previous links)

  +           Compute the sum of the results.
    _         Compute the difference of the results.
   ,          Pair.
\$\endgroup\$
  • 1
    \$\begingroup\$ That is a very nice online compiler. I wish they had one like that for java. \$\endgroup\$ – Ashwin Gupta Dec 28 '15 at 16:04
  • 2
    \$\begingroup\$ @AshwinGupta Dennis made that himself, actually ;) For Java, there's always Ideone. \$\endgroup\$ – Doorknob Dec 28 '15 at 16:09
  • \$\begingroup\$ @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... \$\endgroup\$ – Ashwin Gupta Dec 28 '15 at 16:10
  • \$\begingroup\$ Unless someone can beat 14, I think you won. \$\endgroup\$ – Ashwin Gupta Dec 28 '15 at 16:18
  • 3
    \$\begingroup\$ OK, Jelly is now officially insanely short. I've got to learn this language right after I finish wrapping my head around Pyth... \$\endgroup\$ – ETHproductions Dec 28 '15 at 21:14
14
\$\begingroup\$

Unicorn, 4650 2982 1874 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:

enter image description here


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

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

JavaScript ES6, 45 39 37* bytes

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

* Thanks to Dennis!

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

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

\$\endgroup\$
  • \$\begingroup\$ "unicorn shortcuts" I like the sound of that! ;) \$\endgroup\$ – ETHproductions Dec 28 '15 at 19:04
  • \$\begingroup\$ @ETHproductions haha, that was autocorrect :p though unicorn shortcuts could be interesting... \$\endgroup\$ – Downgoat Dec 28 '15 at 19:06
3
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ Out of curiosity, would the program in "explanation" form actually run on a MATL compiler? The syntax is awfully split. \$\endgroup\$ – Ashwin Gupta Dec 28 '15 at 16:17
  • \$\begingroup\$ 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? \$\endgroup\$ – Luis Mendo Dec 28 '15 at 16:27
3
\$\begingroup\$

Julia, 46 44 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

\$\endgroup\$
  • \$\begingroup\$ 40 bytes: f(b,c)=b/2+√(b^2/4-c),b/2-√(b^2/4-c) \$\endgroup\$ – Alex A. Dec 28 '15 at 19:31
  • \$\begingroup\$ @AlexA. - You're going to make a meme happen by suggesting improvements to this one... \$\endgroup\$ – Darrel Hoffman Dec 28 '15 at 19:34
  • \$\begingroup\$ 34 bytes: f(b,c)=b/2+√(x=b^2/4-c),b/2-√x \$\endgroup\$ – Alex A. Dec 28 '15 at 19:34
  • \$\begingroup\$ 32 bytes: f(b,c)=(x=b+√(b^2-4c))/2,b-x/2 \$\endgroup\$ – Dennis Dec 28 '15 at 20:23
  • 2
    \$\begingroup\$ It's just like the regular quadratic formula that's been beaten into our heads since we were but wee babbies. \$\endgroup\$ – Alex A. Dec 28 '15 at 20:32
3
\$\begingroup\$

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.

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

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.

\$\endgroup\$
  • \$\begingroup\$ Beaten you by one byte :P You should consider switching ;D \$\endgroup\$ – nicael Dec 28 '15 at 19:58
  • \$\begingroup\$ This explanation may not be 100% correct...??? \$\endgroup\$ – Digital Trauma Dec 29 '15 at 3:54
  • \$\begingroup\$ @DigitalTrauma I'm not 100% certain that's how it works (specifically the J), but it's what I gathered from reading the docs. \$\endgroup\$ – ETHproductions Dec 29 '15 at 4:03
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Dennis Dec 29 '15 at 4:44
3
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ 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 \$\endgroup\$ – Ashwin Gupta Dec 30 '15 at 17:01
  • 1
    \$\begingroup\$ Java beat unicorn :( I'd +1 but I've used my daily vote limit \$\endgroup\$ – Downgoat Dec 30 '15 at 18:17
2
\$\begingroup\$

Japt, 28 22 21 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
\$\endgroup\$
  • \$\begingroup\$ 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... \$\endgroup\$ – ETHproductions Dec 28 '15 at 19:12
  • \$\begingroup\$ @Eth Ok, ¬ is a great thing. \$\endgroup\$ – nicael Dec 28 '15 at 19:57
2
\$\begingroup\$

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!

\$\endgroup\$
2
\$\begingroup\$

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.
\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
  • \$\begingroup\$ The sign of the difference is incorrect; it should be s/2-d. Also, d=(s*s/4-p)**.5 saves a few bytes. \$\endgroup\$ – Dennis Dec 29 '15 at 18:40
  • \$\begingroup\$ @Dennis Oops, you're right. Fixed and golfed. \$\endgroup\$ – Sherlock9 Dec 29 '15 at 18:53
1
\$\begingroup\$

MathCAD 15. 38 Bytes

enter image description here

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.

\$\endgroup\$
1
\$\begingroup\$

𝔼𝕊𝕄𝕚𝕟, 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.

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

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.

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

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
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Ashwin Gupta Jan 26 '16 at 1:29
  • \$\begingroup\$ 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. \$\endgroup\$ – Conor O'Brien Jan 26 '16 at 1:30
  • \$\begingroup\$ 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? \$\endgroup\$ – Ashwin Gupta Jan 26 '16 at 1:33
  • \$\begingroup\$ 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. \$\endgroup\$ – Conor O'Brien Jan 26 '16 at 1:35
  • \$\begingroup\$ Ah okay I got you. Cool. I just was curious. \$\endgroup\$ – Ashwin Gupta Jan 26 '16 at 1:35

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.