(URI) Número Primo - Solução

import java.io.*;
import java.util.*;
import java.lang.*;

class Main  {
    public static void main(String[] args) throws NumberFormatException, IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       
        int qte = leitor(br);
       
        for (int i = 0; i < qte; i++) {
            boolean primo = true;
            int numero = leitor(br);
            for (int j = 2; j < numero; j++) {
                if (numero%j == 0) {
                    primo = false;
                    break;
                }
            }
            if (primo) {
                System.out.println(numero + " eh primo");
            }
            else {
                System.out.println(numero + " nao eh primo");
            }
        }
    }
   
    static int leitor(BufferedReader br) throws NumberFormatException, IOException {
       int n;
       int resp = 0;
       int sinal = 1;
       while (true) {
           n = br.read();
           if (n >= '0' && n <= '9') break;
           if (n == '-') sinal = -1;
           if (n == '+') sinal = 1;
       }
       while (true) {
           resp = resp*10 + n-'0';
           n = br.read();
           if (n < '0' || n > '9') break;
       }

       return resp*sinal;
    }
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução