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.
importstaticjava.lang.System.in;classPrime{publicvoidcheckPrime(int...args){for(intarg:args){if(isPrime(arg))System.out.print(arg+" ");}System.out.println();}privatebooleanisPrime(intnum){if(num==2)returntrue;//2 is considered primeelseif(num==1||num%2==0)returnfalse;//1 not prime, divisible by 2 not primeelse{//now we can try only odd numbers//we don't need to try more than square root of numfor(inti=3;i*i<=num;i+=2){if(num%i==0)returnfalse;}returntrue;}}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Prime Checker
You are viewing a single comment's thread. Return to all comments →
This is my approach in Java.