Print numbers such that no two consecutive numbers are co | Python3 implementation of the above approach ; Function to generate Sieve of Eratosthenes ; If prime [ p ] is not changed , then it is a prime ; Add the prime numbers to the array b ; Function to return the gcd of a and b ; Function to print the required sequence of integers ; Including the primes in a series of primes which will be later multiplied ; This is done to mark a product as existing ; Maximum number of primes that we consider ; For different interval ; For different starting index of jump ; For storing the numbers ; Checking for occurrence of a product . Also checking for the same prime occurring consecutively ; Including the primes in a series of primes which will be later multiplied ; Driver Code
limit = 1000000000 NEW_LINE MAX_PRIME = 2000000 NEW_LINE MAX = 1000000 NEW_LINE I_MAX = 50000 NEW_LINE mp = { } NEW_LINE b = [ 0 ] * MAX NEW_LINE p = [ 0 ] * MAX NEW_LINE j = 0 NEW_LINE prime = [ True ] * ( MAX_PRIME + 1 ) NEW_LINE def SieveOfEratosthenes ( n ) : NEW_LINE INDENT global j NEW_LINE p = 2 NEW_LINE while p * p <= n : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * p , n + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT for p in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( prime [ p ] ) : NEW_LINE INDENT b [ j ] = p NEW_LINE j += 1 NEW_LINE DEDENT DEDENT DEDENT def gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return gcd ( b , a % b ) NEW_LINE DEDENT def printSeries ( n ) : NEW_LINE INDENT SieveOfEratosthenes ( MAX_PRIME ) NEW_LINE ar = [ 0 ] * ( I_MAX + 2 ) NEW_LINE for i in range ( j ) : NEW_LINE INDENT if ( ( b [ i ] * b [ i + 1 ] ) > limit ) : NEW_LINE INDENT break NEW_LINE DEDENT p [ i ] = b [ i ] NEW_LINE mp [ b [ i ] * b [ i + 1 ] ] = 1 NEW_LINE DEDENT d = 550 NEW_LINE flag = False NEW_LINE k = 2 NEW_LINE while ( k < d - 1 ) and not flag : NEW_LINE INDENT m = 2 NEW_LINE while ( m < d ) and not flag : NEW_LINE INDENT for l in range ( m + k , d , k ) : NEW_LINE INDENT if ( ( ( b [ l ] * b [ l + k ] ) < limit ) and ( l + k ) < d and p [ i - 1 ] != b [ l + k ] and p [ i - 1 ] != b [ l ] and ( ( b [ l ] * b [ l + k ] in mp ) and mp [ b [ l ] * b [ l + k ] ] != 1 ) ) : NEW_LINE INDENT if ( mp [ p [ i - 1 ] * b [ l ] ] != 1 ) : NEW_LINE INDENT p [ i ] = b [ l ] NEW_LINE mp [ p [ i - 1 ] * b [ l ] ] = 1 NEW_LINE i += 1 NEW_LINE DEDENT DEDENT if ( i >= I_MAX ) : NEW_LINE INDENT flag = True NEW_LINE break NEW_LINE DEDENT DEDENT m += 1 NEW_LINE DEDENT k += 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT ar [ i ] = p [ i ] * p [ i + 1 ] NEW_LINE DEDENT for i in range ( n - 1 ) : NEW_LINE INDENT print ( ar [ i ] , end = " ▁ " ) NEW_LINE DEDENT g = gcd ( ar [ n - 1 ] , ar [ n - 2 ] ) NEW_LINE print ( g * 2 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 NEW_LINE printSeries ( n ) NEW_LINE DEDENT