+ 19

How to factorize the square root that is not exact in java?

I need my program to do the following: TRANSFORM THE SQUARE ROOT OF EIGHT IN 2 ROOT OF 2

11th Jan 2018, 12:02 AM
Giovane de Macedo Borges
Giovane de Macedo Borges - avatar
1 Answer
+ 3
√(a * b²) = (√a) * b, right? So what you want to do is split your number into all its prime factors, and see which ones appear twice. Those ones you can "pull out" of the square root. For example: 252 = 2 * 2 * 3 * 3 * 7 = 2² * 3² * 7, so √252 = √(2² * 3² * 7) = 2 * 3 * √7 = 6 * √7 Another way to do it which might be simpler in code (but does the same thing), is to try all the square numbers smaller than √x and see if they divide. For example: √252 = 15.874... so we try all the square numbers smaller than 15.874..., from largest to smallest. Does 9 = 3² divide 252? Yes. So we put 3 into a list. We continue with 252/9 = 28. Does 4 = 2² divide 28? Yes. So we put 2 into the list. We continue with 28/4 = 7. (We skip 1 = 1², everything is divisible by 1) Our list is [2, 3]. 7 is left over. So we have √252 = 2 * 3 * √7 = 6 * √7 Hope that helps.
11th Jan 2018, 1:25 AM
Schindlabua
Schindlabua - avatar