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.
importrandomdefisPrimeMillerRabin(n,k=3):ifn<=1:returnFalseifn==2orn==3:returnTrueifn%2==0:returnFalser,d=0,n-1whiled%2==0:r+=1d//= 2# Perform Miller-Rabin test k timesfor_inrange(k):a=random.randint(2,n-2)x=pow(a,d,n)ifx==1orx==n-1:continuefor_inrange(r-1):x=pow(x,2,n)ifx==n-1:breakelse:returnFalsereturnTrue#LikelytobeprimeN=float(input())/100count=0i=3spiral_size=1whileTrue:forjinrange(4):num=i**2-(i-1)*jifisPrimeMillerRabin(num):count+=1spiral_size+=4proportion=count/spiral_sizeifproportion<N:breaki+=2print(i)
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 →
Python