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.
I never use import static before, this problem gives me good insight.
Also, searching for math equation for checkPrime and adapt it to java is fun, and it remind me that (int / int( will automatically truncate decimal point !!
here is my solution:
importstaticjava.lang.System.in;classFactorial{publicstaticintcalFactorial(intin){intcal=1;while(in>0){cal=cal*in;in--;}returncal;}}classPrime{publicvoidcheckPrime(int...args){Stringres="";for(inti:args){if(i!=0&&i!=1){// this is Willans' formula doubleresult=Math.pow(Math.cos(Math.PI*((Factorial.calFactorial(i-1)+1)/(double)i)),2);if(result>=1){if(res.length()>0){res=res+" "+i;}else{res=res+i;}}}}if(res.isEmpty()){System.out.println("");}else{System.out.println(res);}}}
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 →
I never use
import static
before, this problem gives me good insight. Also, searching for math equation for checkPrime and adapt it to java is fun, and it remind me that (int / int( will automatically truncate decimal point !!here is my solution: