/*
Online Java - IDE, Code Editor, Compiler
Online Java is a quick and easy tool that helps you to build, compile, test your programs
online.
*/

//Note I am using the term beads to refer to number of dots on each line

import java.util.*;

public class CentredHexagonalNumber
{
    static boolean hasExistingKeyinMap=false;
    static int nthTerm;
    static int k;
    static long result=0;
    static int finalTotal=0;
    static boolean firstExecution=true;
    static int retrievedValueInteger;
    static boolean hasProcessedRow=false;
    static Map <Integer, Integer> mp = new HashMap<>();
    
    
    public static void main(String[] args) 
    {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        int n=999179;
        int H=0;
        int N=1000;
        int M;
        int R;
        
        System.out.println("***CENTERED HEXAGONAL NUMBER***");
        
        for (int i=1;i<N;i++)
        {
            System.out.println("***************************************************");
            
            M=(i*2)-1;
                
            nthTerm = nthTerm+1;
            hasExistingKeyinMap=false;
            hasProcessedRow=false;
            result=0;
            
            System.out.println("\nThis is the Nth term: " + (nthTerm));
            System.out.println("Middle disector (beads): " + M);
                
            R=i-1;
            System.out.println("Number rows above or below: " + R);
         
            if (k==0)
            {
                finalTotal=1;
                    
                if (finalTotal==n && n!=0)
                {
                     System.out.println("A CENTRAL HEXAGON NUMBER: " + n);
                    
                     System.exit(0);
                }
            }
                
            if (k!=1)
            {
                for (k=R; k>=1;k--)
                {
                    System.out.println("CURRENT RUNNING TOTAL BEADS: " + result);
                    System.out.println("Total full count: " + CentredHexagonalNumber(mp,i,M,k,R,n));
                }
            }
        }
    }
    
    public static long CentredHexagonalNumber (Map mp, int i, int M, int k,int R,int n)
    {
        if (nthTerm==47)
        {
            System.out.println("*****Code will terminate due to recursion limits*******");
            System.exit(0);
        }
        
        if (k>=1)
        {
            if (result!=0)
            {
                System.out.println("Currently processed row " + retrievedValueInteger+": " + hasProcessedRow + "   with result: " + result);
                System.out.println("Has existing key" + "("+retrievedValueInteger+","+result+")" + " in map: " + hasExistingKeyinMap);
            }
            
            if(!hasExistingKeyinMap && hasProcessedRow)
            {
                System.out.println("No key entry"+"("+ retrievedValueInteger+")" + " in hashmap. To be added " + "("+result+")");
                mp.put(retrievedValueInteger,result);
                
                hasExistingKeyinMap=true;
                hasProcessedRow=false;
            }
            
            System.out.println("BEADS in: " + (k) + " row above disector is: " + (M-k));
            
            result=result + (M-k);
            System.out.println("CURRENT RUNNING TOTAL BEADS: " + result);
            
            if (R!=1)
            {
                retrievedValueInteger=M-k;
                
                System.out.println("Checking if key " + "("+retrievedValueInteger+")" + " in HashMap");
                
                if (mp.containsKey(retrievedValueInteger))
                {
                    System.out.println("Current key: " + retrievedValueInteger + " already in HashMap. Retrieving value: " + mp.get(retrievedValueInteger));
                    
                    Object retrievedValue = mp.get(retrievedValueInteger);
                    retrievedValueInteger = Integer.parseInt(retrievedValue.toString());
                    hasExistingKeyinMap = true;
                    
                    finalTotal  = (int)(result*2)+M;
                     
                    System.out.println("2******FINAL TOTAL: " + finalTotal + "     (" + result + "x" + 2+")" + "+"+M);
        
                    if (finalTotal==n && n!=0)
                    {
                        System.out.println("A CENTRAL HEXAGON NUMBER: " + n);
                        System.exit(0);
                    }
                }
                hasProcessedRow=true;
                
                result = retrievedValueInteger + (CentredHexagonalNumber (mp,i,M,(k-1),R,n));
                
                System.out.println("number provided: " + n);
                System.out.println("Final Total: " + finalTotal);
                
                if (finalTotal>n)
                {
                    System.out.println("NOT A CENTRAL HEXAGON NUMBER: " + n);
                    System.exit(0);
                }
                
                if (finalTotal==n && n!=0)
                {
                     System.out.println("A CENTRAL HEXAGON NUMBER: " + n);
                     System.exit(0);
                }
               
                M=M+2;
                
                System.out.println("This is FinalTotal: " + finalTotal);
                
                finalTotal=0;
                nthTerm=nthTerm+1;
                hasExistingKeyinMap=false;
                hasProcessedRow=false;
                result=0;
                
                System.out.println("\nThis is the Nth term: " + (nthTerm));
                
                firstExecution=true;
                
                R=nthTerm-1;
                
                System.out.println("Middle disector: " + M);
                
                result=0;
                
                k=R;
                
                if (k>=1)
                {   
                    if (firstExecution)
                    {
                        System.out.println("FIRST EXECUTION ONCE");
                        result = finalTotal+(CentredHexagonalNumber (mp,i,M,(k),R,n));
                        firstExecution=false;
                    }
                    else
                    {
                        result = finalTotal+(CentredHexagonalNumber (mp,i,M,(k-1),R,n));
                    }
                }
                
                if (k==0)
                {
                    return result;
                }
            }
            else
            {
                result = M-k;
            }
        }
        
        if (!mp.containsKey(retrievedValueInteger))
        {
            finalTotal  = (int)(result*2)+M;
        
            System.out.println("1******FINAL TOTAL: " + finalTotal + "     (" + result + "x" + 2+")" + "+"+M);
        
            if (finalTotal==n && n!=0)
            {
                System.out.println("A CENTRAL HEXAGON NUMBER: " + n);
                System.exit(0);
            }
        }
        return finalTotal;
    }
}