Foro de elhacker.net

Programación => Java => Mensaje iniciado por: Jota1948 en 31 Marzo 2014, 18:15 pm



Título: Problema con enteros muy grandes en Java
Publicado por: Jota1948 en 31 Marzo 2014, 18:15 pm
Hola a todos

Estoy haciendo un ejercicio en Java que solicita calcular si un número de 25 cifras o mayor es primo o no, funciona correctamente hasta 18 cifras, pero de ahí en adelante no deja hacer nada, agradezco quien me pueda dar una manita a ver que puede estar faltando

Código:
import javax.swing.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class NP
{
    public static void main(String args[]) throws NumberFormatException, IOException, FileNotFoundException
    {      
      
        long A=0;
        int div=0;
        int B = 1;
        float raiz;
        
        BufferedReader leer = new BufferedReader(new InputStreamReader(System.in));
        
        System.out.println("");
System.out.print("Digite el Número:  ");
System.out.print("");
A = (long) Double.parseDouble(leer.readLine());
raiz = (float) Math.sqrt(A);

        if( A > 0 )
        {
          
            for ( B = 1; B <= raiz; B++)              
            {
          
                if(A % B == 0)
                {
                    div++;
                }
            }
            
            if(div<=2)
            {
                
             System.out.println("");
         System.out.print(A+" Si es un número Primo");
         System.out.print("");
            }
            else
            {
             System.out.println("");
         System.out.print(A+" No es un número Primo");
         System.out.print("");
            }
        }
          
    }
}

Muchas gracias por su ayuda


Título: Re: Problema con enteros muy grandes en Java
Publicado por: 1mpuls0 en 31 Marzo 2014, 18:57 pm
e.e quien rayos querría hacer eso.

Posiblemente BigDecimal puede ayudarte.

Ejemplos:
http://www.tutorialspoint.com/java/math/bigdecimal_max.htm
http://www.java2s.com/Code/JavaAPI/java.math/BigDecimal.htm


Documentación
http://docs.oracle.com/javase/6/docs/api/java/math/BigDecimal.html

Citar
Immutable, arbitrary-precision signed decimal numbers. A BigDecimal consists of an arbitrary precision integer unscaled value and a 32-bit integer scale. If zero or positive, the scale is the number of digits to the right of the decimal point. If negative, the unscaled value of the number is multiplied by ten to the power of the negation of the scale. The value of the number represented by the BigDecimal is therefore (unscaledValue × 10-scale)


Título: Re: Problema con enteros muy grandes en Java
Publicado por: Jota1948 en 31 Marzo 2014, 19:29 pm
e.e quien rayos querría hacer eso.

Posiblemente BigDecimal puede ayudarte.

Ejemplos:
http://www.tutorialspoint.com/java/math/bigdecimal_max.htm
http://www.java2s.com/Code/JavaAPI/java.math/BigDecimal.htm


Documentación
http://docs.oracle.com/javase/6/docs/api/java/math/BigDecimal.html


Gracias por tu respuesta, a nuestro profesor le dio por poner eso de tarea y la verdad no es que seamos muy fuertes programando.


Título: Re: Problema con enteros muy grandes en Java
Publicado por: 1mpuls0 en 31 Marzo 2014, 19:46 pm
De nada.

Comenta si tienes dudas.

Por cierto por qué tu algoritmo lo tienes así?.
Así les pidieron específicamente el método?