gibt es keine funktion oder möglichkeit die wurzel einer bigInteger zu ziehen?
Ich bin verdammt froh das es keine operatorenüberladung gibt!überhaupt finde ich es voll umständlich das man nicht einfach
a+b machen kann sondern a.add(b) machen muss .....
final private static BigInteger BigIntegerTWO = BigInteger.valueOf(2);
/**
* Calculates BigInteger square root of BigInteger n
* @param n BigInteger to calcuate squate root for
* @return BigInteger square root of BigInteger n
*/
private static BigInteger sqrt(BigInteger n) {
BigInteger result = n.divide(BigIntegerTWO).add(BigInteger.ONE);
BigInteger help = result.add(BigInteger.ONE);
//while (result*result > x || (result+1)*(result+1)< x)
while (result.multiply(result).compareTo(n) > 0 || help.multiply(help).compareTo(n) < 0) {
result = result.add(n.divide(result)).divide(BigIntegerTWO);
help = result.add(BigInteger.ONE);
}
return result;
}
BigInteger b = new BigInteger("1");
for (int x = 1, x < 100, x++)
{
b.add(BigInteger.ONE);
System.out.println(b);
}
import java.io.*;
import java.math.BigInteger;
public class primzerlegung
{
public static void main(String args[]) throws IOException
{
BigInteger produkt = new BigInteger((new BufferedReader(new InputStreamReader(System.in))).readLine());
BigInteger counter = new BigInteger("2");
BigInteger zwei = new BigInteger("2");
while (produkt.mod((((counter.negate()).divide(zwei)).add(wurzel(((counter.divide(zwei)).pow(2)).add(produkt))))) != BigInteger.ZERO)
{
counter.add(zwei);
System.out.println(zwei);
}
System.out.println(produkt.mod((((counter.negate()).divide(zwei)).add(wurzel(((counter.divide(zwei)).pow(2)).add(produkt))))));
}
public static BigInteger wurzel(BigInteger x)
{
BigInteger y = new BigInteger("1");
System.out.println("In Wurzel drin: "+x);
while (y.multiply(y) != x && y.max(x) == x)
{
y = y.add(BigInteger.ONE);
//System.out.println(y);
}
if (y.max(x) == x)
return y;
else
return BigInteger.ZERO;
}
}
0xdeadbeef hat gesagt.:Der obige Algorithmus liefert natürlich auch nur ganzzahlige Ergebnisse, allerdings dürfte er deutlich schneller konvergieren als die Brute-Force-Methode.
...
/**
* Calculates BigDecimal square root of BigDecimal x
* and with nd digits
* @param x BigDecimal to calcuate squate root for
* @param nd int number of digits for x
* @return BigDecimal square root of BigDecimal x
*/
private static BigDecimal sqrt(BigDecimal x, int nd)
{
BigDecimal zero = ZERO.setScale(nd + 10);
BigDecimal one = ONE.setScale(nd + 10);
BigDecimal two = TWO.setScale(nd + 10);
BigDecimal ten = TEN.setScale(nd + 10);
BigDecimal maxerr = one.movePointLeft(nd);
BigDecimal lower = zero;
BigDecimal upper = x.compareTo(one) <= 0 ? one : x;
BigDecimal mid;
while (true) {
mid = lower.add(upper).divide(two, BigDecimal.ROUND_HALF_UP);
BigDecimal sqr = mid.multiply(mid);
BigDecimal error = x.subtract(sqr).abs();
if (error.compareTo(maxerr) <= 0) {
break;
}
if (sqr.compareTo(x) < 0) {
lower = mid;
} else {
upper = mid;
}
}
return mid;
}
0xdeadbeef hat gesagt.:Eigentlich wollte er doch 'ne (Big-)Integer-Wurzel ziehen, oder? In diesem Fall ist die BigDecimal-Version vor allem eines: dramatisch (!) langsamer.
Unfd warum sollte bei meinem Algo 100mal eine 1 rauskommen? Merke: zitieren will auch gelernt sein :roll:
Aber naja, von mir aus: macht was ihr wollt.