Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Exam_Problem3_Biggest3PrimeNumbers {
public static void main(String[] args) {
//inputNumbers contains all numbers from the input
List<String> inputNumbers= new ArrayList<>();
int result=0;
Scanner scanner = new Scanner(System.in);
//input
String input = scanner.nextLine();
//find numbers from the input -> write them in inputNumbers
Pattern nums = Pattern
.compile("[0-9]+");
Matcher matcher = nums.matcher(input);
while (matcher.find()) {
inputNumbers.add(matcher.group());
}
List<Integer> intList=new ArrayList<>();
List<Integer> primeNums=new ArrayList<>();
//str to int -> intList contains all nums as integers, instead of strings
for(String inputNumber : inputNumbers) intList.add(Integer.valueOf(inputNumber));
//check if numbers in intList are prime
for (int len = intList.size(),i=0; i < len; i++) {
int n=intList.get(i);
//check for prime numbers
// 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
if((n==2)||(n==3)||(n==5)||(n==7)||(n==11)||(n==13)||(n==17)||(n==19)||(n==23)||(n==29)||(n==31)||(n==37)||(n==41)||(n==43)||(n==47)||(n==53)||(n==59)||(n==61)||(n==67)||(n==71)||(n==73)||(n==79)||(n==83)||(n==89)||(n==97)){
//all prime numbers go in primeNums
primeNums.add(n);
}
}
//
//for (int len = primeNums.size(), i = 0; i < len; i++) {
//int s = primeNums.get(i);
//}
//primes contains prime numbers w/o duplicates
Set<Integer> primes = new HashSet<Integer>(primeNums);
if(prime.size()>=3){
int len=prime.size();
//array of prime numbers - primeArr object ->arr
Object[] primeArr=prime.toArray();
//length of primeArr
int lenPrimeArr=primeArr.length;
int arr[]=new int[lenPrimeArr];
for (int i = 0; i < lenPrimeArr; i++) {
arr[i]=(int) of[i];
}
Arrays.sort(arr);
int num1=arr[len1-1];
int num2=arr[len1-2];
int num3=arr[len1-3];
int sum=num1+num2+num3;
//int sum=num1+num2+num3;
System.out.println(sum);
}else{
System.out.println("No");
}
}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.