Olá! Galera… Estou com dúvida. E já pesquisei em vários fórum ou … e não consigo entender o pq na utilização do bigDecimal, não tem o mesmo desenvolvimento utilizando long ou int e etc…
Já fiz essa pergunta antes, não tive a resposta. Mas sei q foi pela forma que apresentei o programa… Refiz e agora acho que posso ter uma resposta. Agradeço! Veja os dois exemplos. Um utilizando long e outro bigDecimal … até certo número os dois tem o mesmo desenvolvimento… Depois complica! …
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package raizlong;
import java.math.BigDecimal;
import java.util.Scanner;
/**
*
*
*/
public class RaizLong {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner scanner = new Scanner(System.in);
System.out.println("Digite um numero:");
long b = scanner.nextLong();
long sqr = (long) Math.sqrt(b);
System.out.println("testeraiz" + sqr );
for (long i = 2; i <= sqr; i++) {
while (b % i == 0) {
// System.out.println( " divisores"+i);
System.out.println(b+" divisores"+i+" and "+b/i);
break;
}
}
}
}
bigdecimal utilizando long e double ... por não saber qual método substitui o Math.sqrt em bigDecimal.
package raizbig1;
import java.math.BigDecimal;
import java.util.Scanner;
/**
*
*
*/
public class Raizbig1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Scanner scanner = new Scanner(System.in);
System.out.println("Digite um numero:");
BigDecimal b = scanner.nextBigDecimal();
double b1 = b.doubleValue();
BigDecimal two = new BigDecimal(2);
long sqr = (long) Math.sqrt (b1);
System.out.println("sqr" +sqr);
System.out.println( " divisores"+b);
for (long i = 2; i <= sqr; i++) {
BigDecimal ii = ((BigDecimal.valueOf(i)));
while ( b.remainder(ii).compareTo(BigDecimal.ZERO)==0) {
// System.out.println( " divisores"+i);
System.out.println(b+" divisores"+ii+" and "+b.divideToIntegralValue(ii));
break;
}
}
}
}
Utilizei o seguinte decimal como ex: 243101957363429877
Primeiro Exemplo: RaizLong fez em
CONSTRUÍDO COM SUCESSO (tempo total: 8 segundos)
Com bigD… Em:
CONSTRUÍDO COM SUCESSO (tempo total: 6 minutos 1 segundo)
Sei que a uma diferença entre inteiros e bigDecimal… São duas coisas distintas. A forma que processam os dados ou alocam na memoria ou …
Só gostaria de ter uma forma de calcular no mesmo tempo… Pois está fazendo muita diferença no restante do meu Programa…
Obrigado!