7
\$\begingroup\$

Challenge

Given a fraction with a surd (an irrational number) as the denominator, output the rationalised fraction.

Rationalising the Denominator

To rationalise the denominator, what you have to do is take the fraction and make one which is equal which does not have an irrational number in the denominator. Generally, the method is as follows:

Say the inputted fraction is:

(a + x * sqrt(b)) / (c + y * sqrt(d))

To rationalise the denominator, you need to multiply the numerator and the denominator by the conjugate of c + y * sqrt(d) (c - y * sqrt(d)):

(a + x * sqrt(b)) / (c + y * sqrt(d)) * (c - y * sqrt(d)) / (c - y * sqrt(d))

To get:

((a + x * sqrt(b))(c - y * sqrt(d))) / (c^2 + y * d)

Which you should then simplify completely

Example

Input:

(4 + 3 * sqrt(2))/(3 - sqrt(5))

Method:

(4 + 3 * sqrt(2))/(3 - sqrt(5)) * (3 + sqrt(5))/(3 + sqrt(5))
((4 + 3 * sqrt(2))(3 + sqrt(5)))/(9 - 5)
(12 + 4 * sqrt(5) + 9 * sqrt(2) + 3 * sqrt(10))/(4)

Output:

(12 + 4 * sqrt(5) + 9 * sqrt(2) + 3 * sqrt(10))/(4)

Input:

3 / sqrt(13)

Method:

3 / sqrt(13) * sqrt(13) / sqrt(13)
(3 * sqrt(13)) / 13

Output:

(3 * sqrt(13)) / 13

Input:

1 / (sqrt(5) + sqrt(8))

Method:

1/ (sqrt(5) + sqrt(8)) * (sqrt(5) - sqrt(8)) / (sqrt(5) - sqrt(8))
(sqrt(5) - sqrt(8)) / (sqrt(5) + sqrt(8))(sqrt(5) - sqrt(8))
(sqrt(5) - sqrt(8)) / (5 - 8)
(sqrt(5) - sqrt(8)) / -3

Output:

(sqrt(5) - sqrt(8)) / -3

Input:

2 / (3 * sqrt(20) + sqrt(50) + sqrt(2))

Method:

2 / (3 * sqrt(20) + sqrt(50) + sqrt(2)) * (63 * sqrt(20) - sqrt(50) - sqrt(2)) / (3 * sqrt(20) - sqrt(50) - sqrt(2))
2(3 * sqrt(20) - sqrt(50) - sqrt(2)) / (108)
(3 * sqrt(20) - sqrt(50) - sqrt(2)) / (54)
(3 * 2 * sqrt(5) - 5 * sqrt(2) - sqrt(2)) / (54)
(6 * sqrt(5) - 6 * sqrt(2)) / (54)

Output:

(6 * sqrt(5) - 6 * sqrt(2)) / (54)

If you want, you can simplify this further to:

(6 * (sqrt(5) - sqrt(2))) / (54)

or:

(sqrt(5) - sqrt(2)) / (9)

Rules

All input will be in its simplest form. For example, the surd 7*sqrt(12) will be simplified to 14*sqrt(3) in the input.

Your program only needs to support addition, subtraction, multiplication, division, square roots and brackets (including expansion of multiplied brackets).

Your input and output may be in any form that you wish as long as it is a fraction and, when pasted into Wolfram Alpha, the input equals the output.

There will never be variables in the input, nor will there be the square roots of negative numbers. There will only ever be one fraction in the input. There will always be an irrational number in the denominator.

Except b and y may not always be present.

Your output must be simplified. This refers to both the simplification of the algebraic expressions and also putting the lowest possible number in the square root. For example:

sqrt(72)

Which can be simplified like so:

sqrt(72) = sqrt(9 * 8)
         = sqrt(9) * sqrt(8)
         = 3 * sqrt(8)
         = 3 * sqrt(4 * 2)
         = 3 * sqrt(4) * sqrt(2)
         = 3 * 2 * sqrt(2)
         = 6 * sqrt(2)

Built-in functions and libraries to rationalise fractions are disallowed. Similarly, internet access is disallowed.

Winning

The shortest code in bytes wins.

\$\endgroup\$

closed as unclear what you're asking by xnor, Mego, Blue, Martin Ender Oct 4 '16 at 18:40

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ For fewer bytes, answers might want to use the Unicode symbol for sqrt instead: . Works in Wolfram Alpha. \$\endgroup\$ – wizzwizz4 Oct 3 '16 at 17:31
  • 2
    \$\begingroup\$ Internet access is a default loophole, no need to explicitly mention it. \$\endgroup\$ – orlp Oct 3 '16 at 18:17
  • 6
    \$\begingroup\$ @Aaron Popularity contests that ask to do X creatively have fallen out of scope. This challenge would get closed as too broad if it was a pop con. \$\endgroup\$ – Dennis Oct 3 '16 at 18:34
  • 3
    \$\begingroup\$ The example 2 / (3 * sqrt(20) + sqrt(50) + sqrt(2)) comes as a surprise because the explanation makes it seem like only one square root will appear in the numerator or denominator. You should make it clearer what kinds of inputs need to be handled. \$\endgroup\$ – xnor Oct 3 '16 at 19:01
  • 1
    \$\begingroup\$ The example 3 * sqrt(20) + sqrt(50) + sqrt(2) is not in its simplest form (6*sqrt(5)+6*sqrt(2)) \$\endgroup\$ – edc65 Oct 4 '16 at 11:02

Browse other questions tagged or ask your own question.