[2017-08-7] Challenge #326 [Easy] Nearest Prime Numbers

Hi, first time poster. Used Python 3.6.2 I'm experimenting with comments right now - hence big comments. IMO, it makes it easier to read.

'''

GAMEPLAN - First, we must get to the nearest odd

then iterate based off which odd it is.  If current number ends with a 3, jump 4 (so that the next number ends with a 7).

if current is any other odd, just jump 2) - if oddnum not 3 jump 2, else jump 4



'''
###########
#
#   FUNCTION DEFINITIONS
#
###########



###############################################################################################################################################################################
##
##  RETURNS BOOLEAN
##
def prime_check( x ) :
    if ( last_digit(x) == 5 or x % 2 == 0) :  #Don't bother checking, it isn't prime if either is true
        return False    
    else :


        count = 1
        while count < x / 3 :  #Holy magic number batman!
                                  #Theory being a number I only ever need to check numbers equal to 1/3rd the value of the number I'm checking (x).
                                  #Why? who the fuck knows, but it makes a sick sort of sense, doesn't it?


            if last_digit(count) == 3 :           ###################################################################
                count += 4                        #                                                                 #
            else :                                #  ITERATE SO ONLY ODD NUMBERS THAT CAN BE PRIME ARE CHECKED.     #
                count += 2                        #                                                                 #
                                                           ###################################################################


            if ( x % count == 0) :  #if a number completely goes into the number we are checking...return, because it's not a prime
                return False

        return True       #ITS A PRIME!  YAY CONFETTI GOLD MONEY HACKS!
##############################################################################################################################################################################                                 




##############################################################################################################################################################################  
##
##  RETURN LAST DIGIT OF A NUMBER
##    
def last_digit( number ) :
    shadow = str( number )                       # Convert To String               
    return int(shadow[ len(shadow)-1 ])             # Get & Return Last Digit              
##############################################################################################################################################################################  


##############################################################################################################################################################################
#
#  NUDGE TOOL FOR ITERATION
#

def nudge(x, direction) :
    if direction == "up" :                    
        if last_digit(x) == 3 :                 
            x += 4                         
        elif last_digit(x) == 4 :
            x += 3                          #  ENSURES WE NUDGE CORRECT VALUES FOR NEXT GREATER PRIME
        elif last_digit(x) % 2 == 0 :               
            x += 1                          
        else:                                     
            x += 2

        return x

    elif direction == "down" :
        if last_digit(x) == 7 :                   #     
            x -= 4                            #
        elif last_digit(x) == 6 :                 #         
            x -= 3                            #   ENSURES WE NUDGE CORRECT VALUES FOR NEXT LESSER PRIME
        elif last_digit(x) % 2 == 0 :             #                  
            x -= 1                            #
        else:                                     #
            x -= 2

############################################################################################################################################################################

#
#
#                                                                                                    MAIN {
#
#

##################
#                          
#       TEST CASES
#                          
##################

challenge_input = [270, 541, 993, 649, 2010741]# 1425172824437700148]   This number causes problems. (Slow solve rate)




for num in challenge_input :


    if ( prime_check( num ) )  :            #
        print(str(num) + " is a prime")     #  start loop with next number when number checked is prime
        continue                            #
    print(" {0} was not a prime, now we enter while loop...".format(num))

    ##################
    #                          
    #   FIND NEXT GREATER VALUE PRIME       
    #  
    ##################

    while ( prime_check( num ) == False ) :
        num = nudge(num, "up")
        print("We are currently counting up: {0}".format(num))

    else:
        print(num)
        num = nudge(num, "down")

    ##################
    #                          
    #   FIND NEXT LESSER VALUE PRIME       
    #  
    ##################

    while ( prime_check( num ) == False ) :
        num = nudge(num, "down")

        print("We are currently counting down: {0}".format(num))
    else:
        print(num)
/r/dailyprogrammer Thread