Created
October 15, 2016 15:01
-
-
Save washingtonsoares/52e84ac84de24bd1309f5144c61a4c5e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package javaapplication1; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
while (sc.hasNextLine()) { | |
Map<String, Integer> mapa = new HashMap<>(); | |
String alfabeto = "abcdefghijklmnopqrstuvxwyzABCDEFGHIJKLMNOPQRSTUVXWYZ"; | |
char[] aux = alfabeto.toCharArray(); | |
for (int i = 0; i < alfabeto.length(); i++) { | |
mapa.put("" + aux[i], i + 1); | |
} | |
String palavra = sc.nextLine(); | |
char[] aux2 = palavra.toCharArray(); | |
Integer soma = 0; | |
for (int i = 0; i < palavra.length(); i++) { | |
String letra = "" + aux2[i]; | |
System.out.println(mapa.get(letra)); | |
soma += mapa.get(letra); | |
} | |
if (ehPrimo(soma)) { | |
System.out.println("It is a prime word."); | |
} else { | |
System.out.println("It is not a prime word."); | |
} | |
} | |
} | |
static boolean ehPrimo(int n) { | |
int qtdDivisores = 0; | |
int i; | |
for (i = 1; i <= n; i++) { | |
if (n % i == 0) { | |
qtdDivisores++; | |
} | |
} | |
if (qtdDivisores == 2 || n == 1) { | |
return true; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment