Balanced Prime | Python3 code to find Nth Balanced Prime ; Return the Nth balanced prime . ; Sieve of Eratosthenes ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; storing all primes ; Finding the Nth balanced Prime ; Driven Program
MAX = 501 NEW_LINE def balancedprime ( n ) : NEW_LINE INDENT prime = [ True ] * ( MAX + 1 ) NEW_LINE p = 2 NEW_LINE while p * p <= MAX : NEW_LINE INDENT if prime [ p ] == True : NEW_LINE INDENT i = p * 2 NEW_LINE while i <= MAX : NEW_LINE INDENT prime [ i ] = False NEW_LINE i = i + p NEW_LINE DEDENT DEDENT p = p + 1 NEW_LINE DEDENT v = list ( ) NEW_LINE p = 3 NEW_LINE while p <= MAX : NEW_LINE INDENT if prime [ p ] : NEW_LINE INDENT v . append ( p ) NEW_LINE DEDENT p = p + 2 NEW_LINE DEDENT count = 0 NEW_LINE i = 1 NEW_LINE for i in range ( len ( v ) ) : NEW_LINE INDENT if v [ i ] == ( v [ i + 1 ] + v [ i - 1 ] ) / 2 : NEW_LINE INDENT count += 1 NEW_LINE DEDENT if count == n : NEW_LINE INDENT return v [ i ] NEW_LINE DEDENT DEDENT DEDENT n = 4 NEW_LINE print ( balancedprime ( n ) ) NEW_LINE