(Coderbyte) Prime Mover - Solução

Using the Java language, have the function PrimeMover(num) return the numth prime number. The range will be from 1 to 10^4. For example: if num is 16 the output should be 53 as 53 is the 16th prime number.

Solução:

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

class Function { 
  int PrimeMover(int num) {
    int numeroRequerido = 2;
    int contador = 1; // possuo 1 número primo

    int indice = 3;
    boolean primo = true;
    while (contador < num) {
      for (int i = 2; i < indice; i++) {
        if (indice%i == 0) {
          primo = false;
          break;
        }
      }
     
      if (primo) {
          numeroRequerido = indice;
        contador++;
      }
     
      primo = true;
      indice++;
    }
      
    return numeroRequerido;
   
  }
 
  public static void main (String[] args) { 
    // keep this function call here    
    Scanner  s = new Scanner(System.in);
    Function c = new Function();
    System.out.print(c.PrimeMover(s.nextLine()));
  }  
 
}

Comments

Popular posts from this blog

(Coderbyte) Dash Insert II - Solução

(Coderbyte) Run Length - Solução

(Coderbyte) Counting Minutes I - Solução