简体   繁体   English

简化Java中的基本表达

[英]Simplify radical expression in java

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 . 我使用的格式是result[0] = number outside radicalresult[1] = number inside radical So far, my method gets all the factors of number (which is the initial UNSIMPLFIED number in the radical). 到目前为止,我的方法获得了所有number因子(这是部首中的初始UNSIMPLFIED数量)。 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. 因此,如何将初始number除以完美的平方,求出平方根,然后将其乘以我的result[0]变量。 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. 所以在数学上我要把: sqrt(50)变成5 sqrt(2)因为sqrt(50) = sqrt(25 * 2)且25是5的理想平方,因此:形成了5 sqrt(2)

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. 例如,可以将99的平方根表示为3 x 11的平方根。

I'd recommend going about this one of two ways: 我建议采用以下两种方法之一:


    1. Take the square root of n. 取n的平方根。 If n is a perfect square (ie 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. 如果n是一个完美的平方(即n的平方根没有十进制值),那么我们只返回基根下不带任何平方根值(或1)的值。 Else... 其他...

    2. Loop down between the square root of n rounded down to 2. Something like: 在n的平方根四舍五入到2之间循环。类似:

       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 (ie 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). 如果计数器的平方均匀地除以n(即沿着n % Math.pow(i,2)==0的线的东西),则返回计数器,将其移至部首之外,将n除以计数器的平方在部首内(例如,如果n = 99且计数器为3,则将3放在外面,将99/9或11放在里面)。 Or in code, once you've determined that i, to the power of two, divides evenly into n: 或在代码中,一旦确定i为2的幂,则将i平均除以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. 其中s等于i等于2的幂。

    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). 查找号码的所有主要因素(例如,99的首要因素是3,3,11)(你可以找到找到一个号码的主要因素样品C实现这里 ,这不应该是不难的,以适应到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,3),将根号之外的数字乘以该素因数(因此,对于3,3,您将外部值乘以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. 对于每个不适合成对的素数(例如11),将根号内的数字乘以该素数。

Hope this helps. 希望这可以帮助。 If this is completely not what you want at all, sorry. 如果这根本不是您想要的,抱歉。

PS 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. 即使您采用第一种算法,您仍然应该看看第二种算法的工作原理,因为它使用素数分解 ,这是一种手工完成的有用技术。

Also if you are using result[0] and result[1] then your declaration should be: 另外,如果您使用result[0]result[1]则声明应为:

double[] result = new double[2];

instead of

double[] result = new double[1];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM