SAP Community Coding Challenge 2

Enter the number under which you want to find out the longest Collatz chain, in the input box and press the Start Processing button. It is defaulted to 1 million as per the challenge question.

by Deepak Tewari

HTML

<!DOCTYPE html>
<title>SAPUI5</title>
<script src="https://sapui5.hana.ondemand.com/resources/sap-ui-core.js" id="sap-ui-bootstrap" data-sap-ui-theme="sap_bluecrystal" data-sap-ui-libs="sap.m" data-sap-ui-bindingSyntax="complex" data-sap-ui-compatVersion="edge" data-sap-ui-preload="async"></script>

<script id="myView" type="ui5/xmlview">
  <mvc:View controllerName="MyController" xmlns="sap.m" xmlns:core="sap.ui.core" xmlns:mvc="sap.ui.core.mvc"
  xmlns:l="sap.ui.layout">
            <Shell id="shell">
		<App id="app">
			<pages>
				<Page id="page" title="Coding Challenge 2">
					<content>
						<l:VerticalLayout>
            <l:HorizontalLayout>
            <Label text="Submitted By:"/>
            <Label text="Deepak Tewari (https://people.sap.com/deepakt5)" 
            design="Bold"/>
            </l:HorizontalLayout>
						<Input id="_idNumber" type="Number" width="auto" value="1000000"/>
						<Button text="Start Processing" press="process"/>
							<l:HorizontalLayout>
								<Label text="Number having maximum steps "/>
								<Label id="maxNum" design="Bold"/>
							</l:HorizontalLayout>
							<l:HorizontalLayout>
								<Label text="Maximum Steps "/>
								<Label id="maxSteps" design="Bold"/>
							</l:HorizontalLayout>
							<l:HorizontalLayout>
								<Label text="Time Taken (using Hashing) "/>
								<Label id="timeTaken" design="Bold"/>
							</l:HorizontalLayout>
						</l:VerticalLayout>
					</content>
				</Page>
			</pages>
		</App>
	</Shell>
  </mvc:View>
</script>

<body class="sapUiBody">
  <div id="content"></div>
</body>

JavaScript

sap.ui.getCore().attachInit(function() {
   "use strict";
   sap.ui.controller("MyController", {
     onInit: function() {
     	//The answer to the challenge is 837799, which is calculated by below
      //function
       this.process();
     },

     process: function() {
       var startTime = window.performance.now();
       var result = this.getMaxCollatzUnder(this.getView().byId("_idNumber").getValue());
       var endTime = window.performance.now();
       var data = result.split('/');
       var perf = endTime - startTime;
       this.getView().byId("maxNum").setText(" : " + data[0]);
       this.getView().byId("maxSteps").setText(" : " + data[1]);
       this.getView().byId("timeTaken").setText(" : " + perf + "  ms");
     },

     getMaxCollatzUnder: function(k) {
       //Find which is the max number below 1000000, which has max steps for Collatz sequence
       var numOfSteps = 1;
       var maxSteps = 0,
         maxNumber = 1;
			var hashTabUsed = 0;
       var cacheHashTab = new Object(); //Hash table to keep caching data
       var num = 1;
       var currNum = 1;

       //For each number from 1 till input
       while (currNum <= k) {
         num = currNum;
         numOfSteps = 1;

         //calculate Collatz Steps
         while (num !== 1) {
           //Check if cache already knows the number of steps for this number
           if (cacheHashTab.hasOwnProperty(num)) {
             numOfSteps = numOfSteps + cacheHashTab[num] - 1;
             hashTabUsed++;
             break;
           }
           if (num % 2 === 0) {
             num = num / 2;
           } else {
             num = (3 * num + 1);
           }
           numOfSteps++;
         }

         //Keep caching the number of steps for further use
         cacheHashTab[currNum] = numOfSteps;
         if (numOfSteps > maxSteps) {
           maxSteps = numOfSteps;
           maxNumber = currNum;
         }
         currNum++;
       }
       var result = maxNumber + '/' +...