Skip to content

Instantly share code, notes, and snippets.

@NargiT
Created February 6, 2018 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NargiT/9cfcf8d395646d9e627e73e0d01fb444 to your computer and use it in GitHub Desktop.
Save NargiT/9cfcf8d395646d9e627e73e0d01fb444 to your computer and use it in GitHub Desktop.
Prime number
package fr.nargit.prime;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int[] vector = null;
Integer result = -1;
try (Scanner scanner = new Scanner(new File(args[0]))) {
String[] numberTokens = scanner.nextLine().split(",");
vector = new int[numberTokens.length];
for (int i = 0; i < numberTokens.length; i++) {
vector[i] = Integer.parseInt(numberTokens[i]);
}
int minPrime = Integer.MAX_VALUE;
for (int i = 0; i < vector.length; i++) {
if (isPrime(vector[i]) && minPrime > vector[i] && ((i+1) % 2 == 0)) {
minPrime = vector[i];
result = vector[i];
}
}
} catch (FileNotFoundException ex) {
throw new RuntimeException(ex);
}
/* YOUR CODE HERE */
if (result != null) {
System.out.println(result > 0 ? result : "NULL");
}
}
static boolean isPrime(int n) {
//check if n is a multiple of 2
if (n % 2 == 0) return false;
//if not, then just check the odds
for (int i = 3; i * i <= n; i += 2) {
if (n % i == 0)
return false;
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment