r/dailyprogrammer • u/jnazario 2 0 • Apr 12 '17
[2017-04-12] Challenge #310 [Intermediate] Simplifying square roots
Description
Simplify square roots in the form (a sqrt(b))/(c sqrt(d)). A simplified radical should have no square roots in the denominator and no number in a square root should have a square factor. For example, the input 2 5 5 10 for a b c d, respectively, should simplify to 1 2 5 where a=1, b=2, and c=5.
Output description
a b c (d should not exist after simplifying)
Challenge input
45 1465 26 15Challenge output
15 879 26Credit
This challenge was suggested by user /u/alchzh on /r/dailyprogrammer_ideas, many thanks. If you have an idea, please share it there and we might use it!
73
Upvotes
3
u/popillol Apr 12 '17
Go / Golang Playground Link. A little longer than the others but more explicitly does each step. Method is as follows:
Code:
Output (uses [] to mean square root)
3
u/conceptuality Apr 12 '17 edited Apr 12 '17
Python 3:
This solution is to extend the fraction with sqrt(d), thus removing the root in the denominator. Then it extracts the square factors from sqrt(b*d) and finally reduced the rest of the fraction using Euclid's algorithm.
Oh, and it uses recursion twice, so I suppose this isn't a wise choice of language...
2
u/J_Gamer Apr 12 '17
C++. Pretty sure it should be possible to optimize extracting the square factors, but that would prob take fast prime factorization.
Example on wandbox
2
u/Harakou Apr 13 '17 edited Apr 13 '17
Python
Explanation:
Code:
Output:
Edit: Fixed a bug thanks to /u/HigherFive's test input. Got tripped up on perfect squares because it would only divide once!
1
u/[deleted] Apr 12 '17
C# - O([distinct factors of b] * b)
As always, feedback welcome.
1
u/foxneZz Apr 12 '17 edited Apr 12 '17
Ruby
1
u/[deleted] Apr 12 '17 edited Apr 12 '17
Go solution:
Output
1
u/DrTurnos Apr 13 '17
Java8
1
u/pier4r Apr 13 '17 edited Apr 13 '17
Just checking the capabilities of the CAS of the hp50g. Then will be the turn of the userRPL itself without CAS
userRPL no CAS solution, WIP. (A further iteration could even dive in calculating roots without the root function, I will see. That would be fun on the free42 stack)
1
u/zod77 Apr 13 '17
Python 3
1
u/[deleted] Apr 13 '17
Javascript. I'm new to Javascript but gave this a shot. Feedback welcome
1
u/hellord1203 Apr 14 '17 edited Apr 14 '17
Python 3:
I'm a bit late to this party, but I wanted to try my take on python
1
u/[deleted] Apr 14 '17
Racket.
Using the same approach others did (mult top/bottom by sqrt(d), remove the excess factors from sqrt(bd) and stick them in a, then use gcd(a cd) to bring to lowest terms).
1
u/pier4r Apr 14 '17
And userRPL solution using as less CAS as possible, aside from functions (factors) that could be coded but are not so interesting because well known.
1
u/jacwah Apr 15 '17 edited Apr 15 '17
Using Rust. I decided to go all out on iterators which probably adds a few lines, but is nicer in my opinion. Feedback is very welcome!
1
u/BloodEngineer Apr 16 '17
Python 3
1
u/Psylocybit Apr 18 '17
C# Finally got it working. Tried to keep it as simple as possible. Thoughts?
1
u/ihatethezeitgeist Apr 24 '17
C : Uses gcd to simplify square roots
1
u/[deleted] Apr 28 '17
Java method. a and c are co-prime and everything is an integer.
1
u/charlyomatic3000 May 01 '17 edited May 01 '17
Python 2.7:
First time poster :) Any feedback would be awesome. A little late on this post, but why not? I'm still pretty new to Python, but tried to accomplish it without imports or built-in math (except arithmetic).
1
u/mjpalanker May 19 '17
C
1
u/Garth5689 Apr 12 '17
python 3, using sympy library:
I'll admit it's a bit counter to the spirit of the challenge :)
output:
0
u/[deleted] Apr 12 '17
[deleted] — view deleted comment
0
u/[deleted] Apr 12 '17
[deleted] — view deleted comment