/*
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 int nthTerm;
    static int k;
    static long result=0;
    static int finalTotal=0;
    static boolean firstExecution=true;
    
    public static void main(String[] args) 
    {
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        int n=999179;
        Map <Long, Long> mp = new HashMap<>();
        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;
            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 factorialResults, int i, int M, int k,int R,int n)
    {
        if (nthTerm==66)
        {
            System.out.println("*****Code will terminate due to recursion limits*******");
            System.exit(0);
        }
        
        if (k>=1)
        {
            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)
            {
                result = ((M-k)+(CentredHexagonalNumber (factorialResults,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;
                
                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 (factorialResults,i,M,(k),R,n));
                        firstExecution=false;
                    }
                    else
                    {
                        result = finalTotal+(CentredHexagonalNumber (factorialResults,i,M,(k-1),R,n));
                    }
                }
                if (k==0)
                {
                    return result;
                }
            }
            else
            {
                result = M-k;
            }
        }
        
        finalTotal  = (int)(result*2)+M;
        System.out.println("******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;
    }
}