How to factorize the square root that is not exact in java? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 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 Resposta
+ 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