Shortest path to reach one prime to other by changing single digit at a time | Python3 program to reach a prime number from another by changing single digits and using only prime numbers . ; Finding all 4 digit prime numbers ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Forming a vector of prime numbers ; in1 and in2 are two vertices of graph which are actually indexes in pset [ ] ; Returns true if num1 and num2 differ by single digit . ; To compare the digits ; If the numbers differ only by a single digit return true else false ; Generate all 4 digit ; Create a graph where node numbers are indexes in pset [ ] and there is an edge between two nodes only if they differ by single digit . ; Since graph nodes represent indexes of numbers in pset [ ] , we find indexes of num1 and num2 . ; Driver code
import queue NEW_LINE class Graph : NEW_LINE INDENT def __init__ ( self , V ) : NEW_LINE INDENT self . V = V ; NEW_LINE self . l = [ [ ] for i in range ( V ) ] NEW_LINE DEDENT def addedge ( self , V1 , V2 ) : NEW_LINE INDENT self . l [ V1 ] . append ( V2 ) ; NEW_LINE self . l [ V2 ] . append ( V1 ) ; NEW_LINE DEDENT DEDENT def SieveOfEratosthenes ( v ) : NEW_LINE INDENT n = 9999 NEW_LINE prime = [ True ] * ( n + 1 ) 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 ( 1000 , n + 1 ) : NEW_LINE INDENT if ( prime [ p ] ) : NEW_LINE INDENT v . append ( p ) NEW_LINE DEDENT DEDENT def bfs ( self , in1 , in2 ) : NEW_LINE INDENT visited = [ 0 ] * self . V NEW_LINE que = queue . Queue ( ) NEW_LINE visited [ in1 ] = 1 NEW_LINE que . put ( in1 ) NEW_LINE while ( not que . empty ( ) ) : NEW_LINE INDENT p = que . queue [ 0 ] NEW_LINE que . get ( ) NEW_LINE i = 0 NEW_LINE while i < len ( self . l [ p ] ) : NEW_LINE INDENT if ( not visited [ self . l [ p ] [ i ] ] ) : NEW_LINE INDENT visited [ self . l [ p ] [ i ] ] = visited [ p ] + 1 NEW_LINE que . put ( self . l [ p ] [ i ] ) NEW_LINE DEDENT if ( self . l [ p ] [ i ] == in2 ) : NEW_LINE INDENT return visited [ self . l [ p ] [ i ] ] - 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT def compare ( num1 , num2 ) : NEW_LINE INDENT s1 = str ( num1 ) NEW_LINE s2 = str ( num2 ) NEW_LINE c = 0 NEW_LINE if ( s1 [ 0 ] != s2 [ 0 ] ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT if ( s1 [ 1 ] != s2 [ 1 ] ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT if ( s1 [ 2 ] != s2 [ 2 ] ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT if ( s1 [ 3 ] != s2 [ 3 ] ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT return ( c == 1 ) NEW_LINE DEDENT def shortestPath ( num1 , num2 ) : NEW_LINE INDENT pset = [ ] NEW_LINE SieveOfEratosthenes ( pset ) NEW_LINE g = Graph ( len ( pset ) ) NEW_LINE for i in range ( len ( pset ) ) : NEW_LINE INDENT for j in range ( i + 1 , len ( pset ) ) : NEW_LINE INDENT if ( compare ( pset [ i ] , pset [ j ] ) ) : NEW_LINE INDENT g . addedge ( i , j ) NEW_LINE DEDENT DEDENT DEDENT in1 , in2 = None , None NEW_LINE for j in range ( len ( pset ) ) : NEW_LINE INDENT if ( pset [ j ] == num1 ) : NEW_LINE INDENT in1 = j NEW_LINE DEDENT DEDENT for j in range ( len ( pset ) ) : NEW_LINE INDENT if ( pset [ j ] == num2 ) : NEW_LINE INDENT in2 = j NEW_LINE DEDENT DEDENT return g . bfs ( in1 , in2 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT num1 = 1033 NEW_LINE num2 = 8179 NEW_LINE print ( shortestPath ( num1 , num2 ) ) NEW_LINE DEDENT