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

import java.util.*;

public class Permutation
{
    static int k;
    static long result=0;
    static int finalTotal=0;
    
    public static void main(String[] args) 
    {
        
        System.out.println("Welcome to Online IDE!! Happy Coding :)");
        int n=35;
        Map <Long, Long> mp = new HashMap<>();
        int H=0;   //this will be calculated total beads
        int N=4;   //need to figure limit out pripoerly later
        int M;  //length middle dissector
        int R; //number rows above or below to the dissector
        
        
        System.out.println("***CENTERED HEXAGONAL NUMBER***");
        System.out.println("P(n,r) = n! / (n−r)!");
        
        //do
    //    {
            
            //we know n value is too high, but safe measure set above for now
            //since do not know the termination
            
            for (int i=1;i<N;i++)
            {
                M=(i*2)-1;
                
            
                //if (i!=0)
                //{
                    System.out.println("\nMiddle disector: " + M);
                    
                //}
                
                
         R=i-1;
         System.out.println("Number rows above or below: " + R);
                
                
                //go each row above or below disector
                //we expect the value to be
                
                if (k!=1)
                {
                
                for (k=R; k>=1;k--)
                {
                    System.out.println("val R: " + R);
                    System.out.println("1current result: " + result);
                    System.out.println("Total full count: " + Permutations(mp,i,M,k,R,n));
                    
                    System.out.println("value in k: " + k);
                    System.out.println("value in R: " + R);
                    
                    if (k==1)
                    {
                        result=0;
                        System.out.println("TERMINATE");
                    }
                    
                }
                
            }
                
            }
            
       // }while (H<=n);
    }
    
    public static long Permutations (Map factorialResults, int i, int M, int k,int R,int n)
    {
        
        
        if (R==0)
        {
            return result;
        }
        
        if (k>=1)
        {
            System.out.println("BEADS in: " + (k) + " row above disector is: " + (M-k));
            System.out.println("CURRENT RESULT: " + result);
            result=result + (M-k);
            System.out.println("R:" +R);
            if (R!=1)
            {
                System.out.println("HERE");
                System.out.println("HERE RESULTS: " + result);
                result = ((M-k)+(Permutations (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);
                }
                
                
                
                System.out.println("RE ENTER BACK*****");
                
                //now expecting M to get bigger by two
                M=M+2;
                
                System.out.println("IS IT!!!!!");
                System.out.println("This is M: " + M);
                System.out.println("This is k:" + k);
                System.out.println("This is R:" + R);
                System.out.println("This is FinalTotal: " + finalTotal);
                
                finalTotal=0;
                
                //we know that R is based on the nth sequence  (R=N-1)
                //we know that the current nth sequence is based on  the for
                //loop of i  traversing N    (which is in the other class).
                //but since we hae the i value passed in the method,
                //it can be used to calculat the new R first
                //then k can be calculated.
                
                
                System.out.println("\nthis is the nth sequence: " + (i+1));
                R=(i+1)-1;
                System.out.println("Middle disector: " + M);
                
                //we know that k is traversing through the Rows R
                //so since we can not get back out of this method to make any changes to that for loop,
                //it has to be dealt with internally
                
                k=R;
                
                //and we know that k will get smaller in the recursive call...
                
                 if (k>=1)
                {
                    System.out.println("k hopefullt being reduced:" + k);
                    //but issue is it can not be reduced by 1 yet.
                    //since it has not performed any calculation on the current row
                    //so if its here first time, it needs to mantain size of k
                    
                    if (firstExecution)
                    {
                        
                        
                    }
                    
                        }
                
                
                System.out.println(M);
                System.out.println(k);
                
                
                if (k==0)
                {
                    return result;
                    //System.out.println("TERMINATE!!!!!!");
                }
                
            }
            else
            {
                
                result = M-k;
            }
            
            //factorialResults.put((M+k),result);
            
            //return result;
        }
        System.out.println("RWAC: " + result);
        System.out.println("LEAVE THE IF LOOP: ");
        
        System.out.println(result*2);
        System.out.println(M);
        System.out.println(k);
        System.out.println(R);
        
        finalTotal  = (int)(result*2)+M;
        System.out.println("******FINAL TOTAL: " + finalTotal);
        return finalTotal;
    }
}