Java - Error con BigInteger

 
Vista:
sin imagen de perfil

Error con BigInteger

Publicado por Lucho9964 (17 intervenciones) el 03/04/2020 03:33:34
Buenas noches chicos, les dejo aqui este codigo el cual marca error, el que sepa como solucionarlo, estare muy agradecido. Gracias.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.util.Scanner;
import java.math.BigInteger;
 
public class Primos{
public static void main(String args[]){
 
   Scanner usuario = new Scanner(System.in);
   BigInteger b;
   BigInteger c;
   BigInteger d = new BigInteger("1");
   BigInteger e = new BigInteger("2");
   BigInteger f = new BigInteger("3");
   BigInteger g = new BigInteger("5");
   BigInteger h = new BigInteger("7");
 
 
   System.out.println("********************************************************");
   System.out.println("*  Bienvenido al programa que le encuentra los primos  *\n*  de un rango que usted mismo determina               *");
   System.out.println("********************************************************");
 
   System.out.println("Digite el valor minimo del rango");
   b = usuario.nextBigInteger();
 
   System.out.println("Digite el valor maximo del rango");
   c = usuario.nextBigInteger();
 
   System.out.println("Los numeros primos que estan dentro de su rango son: ");
 
 
   while(b.compareTo(c)<=0){
   int i = b.compareTo(d);
   int j = b.compareTo(e);
   int k = b.compareTo(f);
   int l = b.compareTo(g);
   int m = b.compareTo(h);
 
 
    if(j==0 || k==0 || l==0 || m==0){
     System.out.print(b + " ");
   }else{
     e=b.mod(e);
     f=b.mod(f);
     g=b.mod(g);
     h=b.mod(h);
     i = e.compareTo(d);
     j = f.compareTo(d);
     k = g.compareTo(d);
     l = h.compareTo(d);
      if(i==0 && j>=0 && k>=0 && l>=0){
     System.out.print(b + " ");
    }else{
    }
   }
    b=b.add(d);
  }
 }
}

********
*error:*
********
El programa ejecuta todo, pero cuando le pongo un rango amplio me marca este error:

Exception in thread "main" java.lang.ArithmeticException: BigInteger: modulus not positive
at java.math.BigInteger.mod(Unknown Source)
at Primos.main(Primos.java:42)



solo muestra en pantalla los dos primeros numeros
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

Error con BigInteger

Publicado por Costero (148 intervenciones) el 03/04/2020 19:17:13
Cuando la variable "e" or otra variable llega a 0 (como ves en el codigo de abajo del BigInteger) te tira el error.



1
2
3
4
5
6
7
public BigInteger mod(BigInteger m) {
    if (m.signum <= 0)
        throw new ArithmeticException("BigInteger: modulus not positive");
 
    BigInteger result = this.remainder(m);
    return (result.signum >= 0 ? result : result.add(m));
}
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar