# TITLE: palindromic_pentagon_number >> solution_palindromic_pentagon_number.py
# AUTHOR: Chalmer Lowe
# DESCRIPTION:

# In this puzzle, your objective is to find the largest pentagon
#     number based on an input 'n' between 10 and 10000000 that is also
#     a palindromic number. 
# 
# Palidromes are sequences that are the same forward and backwards
#     12321 is a palindromic number
#     12345 is NOT a palindromic number

# Pentagon numbers are numbers generated by the following formula:
#     Pn = n(3n − 1)/2
#
#     P1 = 1(3 * 1 - 1)/2 = 1
#     P2 = 2(3 * 2 - 1)/2 = 5
#     P3 = 3(3 * 3 - 1)/2 = 12
#     P4 = 4(3 * 4 - 1)/2 = 22
#     P5 = 5(3 * 5 - 1)/2 = 35
#     P6 = 6(3 * 6 - 1)/2 = 51

# Thus, the fourth pentagon number (where n = 4) is 22.
# Because 22 is the same forward and back, it is also palindromic, 
#     whereas 35 and 51 are not palindromic.

# Find the largest palidromic pentagon number where n is between 10 and 10000000 
# NOTE: you are looking for the outcome of the formula, not the value of n.

# ==============================================================
# Your code goes here:

def p(n):
    return (n * (3 * n - 1))/2

def palindrome(word):
    if not isinstance(word, str):
        word = str(word)
    if word == word[::-1]:
        return word
    
for num in range(10, 10000000):
    result = palindrome(int(p(num)))
    if result != None:
        print(num, result) 
        
# Answer: 10810300301801

#     NOTE: n is 2684561