Simplify radical expression in java

霸气de小男生 提交于 2019-12-11 12:01:51

问题


Here is my method so far:

public static int[] simplifyRadical(int number) {
    int[] result = new int[2];
    for (int i = 1; i < number / 2; i++) {
        if ((i % number == 0)) {
            //IS a factor of the number in the radical
        }
    }
    return result;
}

The format I'm using is result[0] = number outside radical and result[1] = number inside radical. So far, my method gets all the factors of number (which is the initial UNSIMPLFIED number in the radical). So how can I divide the initial number by the perfect square, get the square root of that and multiply that to my result[0] variable. Then keep looping until no perfect squares are found. I'm sorry if this question is confusing to read, it was definitely confusing to write. If you need any clarification, please comment below.
UPDATE:
So mathematically I am turning: sqrt(50) into 5 sqrt(2) because sqrt(50) = sqrt(25 * 2) and 25 is a perfect square of 5, thus: 5 sqrt(2) is formed.


回答1:


If I understand you correctly, you want to simplify a radical. Like, for example, the square root of 99 can be expressed as 3 x the square root of 11.

I'd recommend going about this one of two ways:


    1. Take the square root of n. If n is a perfect square (i.e. the square root of n has no decimal value), then we just return the square root value with nothing (or a 1) under the radical. Else...

    2. Loop down between the square root of n rounded down to 2. Something like:

      double nSquareRoot = Math.sqrt(n);
      int squareRootRounded = (int)nSquareRoot;
      //Here goes the first step of the algorithm
      //...
      for (int i = squareRootRounded; i>1; i--) 
      

      If the counter squared divides evenly into n (i.e. something along the lines of n % Math.pow(i,2)==0), then return with the counter outside your radical and n divided by counter squared inside the radical (for example, if n = 99 and the counter is at 3, you'd place 3 outside, and 99/9, or 11, inside). Or in code, once you've determined that i, to the power of two, divides evenly into n:

      result[0] = i; //Set outside the radical to the counter
      result[1] = n/s; //Set inside the radical to the n divided by s
      

      where s equals i to the power of two.

    3. If you go through the loop and can't find a perfect square that divides evenly, then your radical can't be simplified.


    1. Find all the prime factors of a number (for example, 99's prime factors are 3,3,11) (you can find a sample C implementation for finding the prime factors of a number here, which shouldn't be hard at all to adapt to Java).

    2. For every pair of prime factors in your list (like 3,3), multiply the number outside the radical by that prime factor (so for 3,3, you'd multiply your outside value by 3).

    3. For every prime factor that doesn't fit into a pair (like 11), multiply the the number inside the radical by that prime factor.

Hope this helps. If this is completely not what you want at all, sorry.

PS

Even if you go with the first algorithm, you should still take a look at how the second algorithm works, since it is uses prime factorization, a useful technique for doing this by hand.




回答2:


Also if you are using result[0] and result[1] then your declaration should be:

double[] result = new double[2];

instead of

double[] result = new double[1];


来源:https://stackoverflow.com/questions/5363686/simplify-radical-expression-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!