简化java,简化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. 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.

解决方案

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:

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

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.

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

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

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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值