简化Java中的基本表达 [英] Simplify radical expression in java

查看:104
本文介绍了简化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;
}

我使用的格式是result[0] = number outside radicalresult[1] = number inside radical.到目前为止,我的方法获得了number的所有因子(这是部首中的初始UNSIMPLFIED数).因此,如何将初始number除以完美的平方,得到平方根,然后将其乘以我的result[0]变量.然后继续循环,直到找不到理想的正方形为止.很抱歉,如果这个问题让人难以理解,那么写起来肯定会令人困惑.如果您需要任何澄清,请在下面发表评论.
更新:
因此从数学上讲,我将sqrt(50)转换为5 sqrt(2),因为sqrt(50) = sqrt(25 * 2)和25是5的完美平方,因此:5 sqrt(2)形成了.

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.

推荐答案

如果我对您的理解正确,那么您想简化一个基本部分.例如,可以将99的平方根表示为3 x 11的平方根.

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. 取n的平方根.如果n是一个完美的平方(即n的平方根没有十进制值),那么我们只返回基根下不带任何平方根值(或1)的值.否则...

  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...

在n的平方根之间向下舍入为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--) 

如果计数器的平方均匀地分为n(即沿着n % Math.pow(i,2)==0的线),则返回计数器,将其移至部首之外,将n除以计数器的平方在部首内(例如,如果n = 99且计数器位于3,则您将3放在外面,将99/9或11放在里面).或在代码中,一旦确定i为2的幂,则将i均分为n:

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

其中s等于i等于2的幂.

where s equals i to the power of two.

如果您遍历循环,找不到完美的均分正方形,那么您的部首就无法简化.

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

  1. 查找一个数字的所有素数(例如,99的素数为3、3、11)(您可以找到一个C实现示例,用于查找一个数的素数

  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).

对于列表中的每对素数因子(例如3,3),将根号之外的数字乘以该素因数(因此,对于3,3,您将外部值乘以3).

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).

对于每个不适合成对的素数(例如11),将根号内的数字乘以该素数.

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.

这篇关于简化Java中的基本表达的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆