Find n | Python3 program to find n - th Fortunate number ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to Find primorial of order n ( product of first n prime numbers ) . ; Function to find next prime number greater than n ; Note that difference ( or m ) should be greater than 1. ; loop continuously until isPrime returns true for a number above n ; Ignoring the prime number that is 1 greater than n ; Returns n - th Fortunate number ; Driver Code
def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : return False NEW_LINE if ( n <= 3 ) : return True NEW_LINE if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i = 5 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i += 6 NEW_LINE DEDENT return True NEW_LINE DEDENT def primorial ( n ) : NEW_LINE INDENT p = 2 ; n -= 1 ; i = 3 NEW_LINE while ( n != 0 ) : NEW_LINE INDENT if ( isPrime ( i ) ) : NEW_LINE INDENT p = p * i NEW_LINE n -= 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return p NEW_LINE DEDENT def findNextPrime ( n ) : NEW_LINE INDENT nextPrime = n + 2 NEW_LINE while ( True ) : NEW_LINE INDENT if ( isPrime ( nextPrime ) ) : NEW_LINE INDENT break NEW_LINE DEDENT nextPrime += 1 NEW_LINE DEDENT return nextPrime NEW_LINE DEDENT def fortunateNumber ( n ) : NEW_LINE INDENT p = primorial ( n ) NEW_LINE return findNextPrime ( p ) - p NEW_LINE DEDENT n = 5 NEW_LINE print ( fortunateNumber ( n ) ) NEW_LINE