We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
importjava.io.*;importjava.util.*;publicclassSolution{privatestaticList<Long>sums=newArrayList<>();// Merge two numbers, "append their digits"privatestaticlongmerge(longa,longb){longshift=10;while(shift<=b)shift*=10;returna*shift+b;}// Check if a number is primeprivatestaticbooleanisPrime(longp){if(p<2)returnfalse;if(p<4)returntrue;if(p%2==0||p%3==0)returnfalse;for(longi=5;i*i<=p;i+=6){if(p%i==0||p%(i+2)==0)returnfalse;}returntrue;}// Check if two numbers can be merged in any way and the result is still primeprivatestaticbooleanmatch(longa,longb){returnisPrime(merge(a,b))&&isPrime(merge(b,a));}// Find all sets of primes with the specified sizeprivatestaticvoidfindSets(intsize,List<Integer>primes){intmaxPrime=Collections.max(primes);for(inti=0;i<primes.size();i++){intsmallPrime=primes.get(i);if(smallPrime==5)continue;List<Integer>candidates=newArrayList<>();for(intj=i+1;j<primes.size();j++){intlargePrime=primes.get(j);if(match(smallPrime,largePrime)){candidates.add(largePrime);}}if(size==3){checkTriple(smallPrime,candidates);}elseif(size==4){checkQuadruple(smallPrime,candidates);}elseif(size==5){checkQuintuple(smallPrime,candidates);}}}// Find and add sums for tripletsprivatestaticvoidcheckTriple(intfirst,List<Integer>candidates){for(inti=0;i<candidates.size();i++){for(intj=i+1;j<candidates.size();j++){if(match(candidates.get(i),candidates.get(j))){sums.add((long)first+candidates.get(i)+candidates.get(j));}}}}// Find and add sums for quadrupletsprivatestaticvoidcheckQuadruple(intfirst,List<Integer>candidates){for(inti=0;i<candidates.size();i++){for(intj=i+1;j<candidates.size();j++){if(!match(candidates.get(i),candidates.get(j))){continue;}for(intk=j+1;k<candidates.size();k++){if(match(candidates.get(i),candidates.get(k))&&match(candidates.get(j),candidates.get(k))){sums.add((long)first+candidates.get(i)+candidates.get(j)+candidates.get(k));}}}}}// Find and add sums for quintupletsprivatestaticvoidcheckQuintuple(intfirst,List<Integer>candidates){for(inti=0;i<candidates.size();i++){for(intj=i+1;j<candidates.size();j++){if(!match(candidates.get(i),candidates.get(j))){continue;}for(intk=j+1;k<candidates.size();k++){if(!match(candidates.get(i),candidates.get(k))||!match(candidates.get(j),candidates.get(k))){continue;}for(intl=k+1;l<candidates.size();l++){if(match(candidates.get(i),candidates.get(l))&&match(candidates.get(j),candidates.get(l))&&match(candidates.get(k),candidates.get(l))){sums.add((long)first+candidates.get(i)+candidates.get(j)+candidates.get(k)+candidates.get(l));}}}}}}publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);intmaxPrime=scanner.nextInt();intsize=scanner.nextInt();scanner.close();List<Integer>primes=newArrayList<>();primes.add(2);// Add 2 since it's not generated// Find all primes up to the specified maxPrimefor(inti=3;i<maxPrime;i+=2){booleanisPrime=true;for(intp:primes){if(p*p>i){break;}if(i%p==0){isPrime=false;break;}}if(isPrime){primes.add(i);}}findSets(size,primes);// Sort and print the sumsCollections.sort(sums);for(longsum:sums){System.out.println(sum);}}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #60: Prime pair sets
You are viewing a single comment's thread. Return to all comments →
JAva code
4, 9, 14 cash are not work