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{publicstaticvoidmain(String[]args){/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */Scannerscanner=newScanner(System.in);intn=scanner.nextInt();scanner.close();intprimesCount=0;inti=3;intlastNumber=1;while(true){for(intj=0;j<4;j++){lastNumber+=i-1;if(j!=3&&isPrime(lastNumber)){primesCount++;}}doubleprimesPercentage=(double)primesCount/(1+2*(i-1))*100;if(primesPercentage<n){System.out.println(i);break;}i+=2;}}publicstaticbooleanisPrime(intn){if(n<=1){returnfalse;}if(n<=3){returntrue;}if(n%2==0||n%3==0){returnfalse;}intd=n-1;ints=0;while(d%2==0){s++;d/=2;}Randomrandom=newRandom();for(inti=0;i<4;i++){inta=random.nextInt(n-2)+2;intx=modPow(a,d,n);if(x!=1&&x!=n-1){booleanactive=true;for(intr=0;r<s;r++){x=modPow(x,2,n);if(x==n-1){active=false;break;}}if(active){returnfalse;}}}returntrue;}publicstaticintmodPow(intbase,intexponent,intmod){intresult=1;base%=mod;while(exponent>0){if(exponent%2==1){result=(int)((long)result*base%mod);}base=(int)((long)base*base%mod);exponent/=2;}returnresult;}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #58: Spiral primes
You are viewing a single comment's thread. Return to all comments →
JAva code
7- 8 cash are not pass