JSFiddle - React, Tailwind, and code Playground

by jrab227

HTML

<div ng-app='calc'>

    <div ng-controller='application'>
        <input ng-model='seats'/>
        <br>
        Calculated Probability: {{probability}} <br>
        Total arrangements of seats: {{totalNumberOfWaysToMessUpSeats}} <br>
        Average number of safe seats: {{averageNumberOfSafeSeats}} <br>
        
    </div>
        
        
    <br>
    <div>
        <p>
        Some Background on the solution (Requires upper undergrad level knowledge of Combinatorics):
            <br>
            <br>
        Some assumptions: <br>
            1. The passengers are numbered 1-n where n is the number of seats. <br>
            2. The passenger that loses his boarding pass is selected at random. <br>
            3. The seat that the passenger without a boarding pass takes is selected at random. <br>
            4. If a passenger's seat is lost, he randomly takes an available seat. <br>
            5. Passengers who's seat is not taken will always take their own seat. <br>
            <br>
            Thoughts on the solution: <br>
            
            The first thing to note is what we're dealing with is permutations, particularly the permutation with only 1 cycle. The bijection is this: the passenger without a boarding pass, which I will call marked for the rest of the time, can select his own seat, or select another passengers seat. The set of passengers that take each other's seats form a cycle through Foata's correspondence. The rest of the passengers take their own seat. For example, if 1 is marked and 1 takes 2's seat, 2 can take 3's seat, then we can assume 3 takes 1's seat, forming the permutation <br> <br>
                (1 2 3) (4) (5) ... (other passenters)  <br> 
                
                So we can always form a 1 cycle permutation with the passengers who take the seats. Now the problem boils down to enumerating all possible cycles for n passengers, but we must take into account the fact that each passenger will be counted to lose his...

JavaScript

angular.module('calc',[])
.factory('dynamicFactorialStore', function(){
    return {
    
        'store':{},
        'choose':function(n, k){
          return this.bang(n) / ( this.bang(n-k) * this.bang(k)  )
        },
        'bang': function(k){
        
            if (k < 0){
                throw new RangeError("Negative factorial. Received"+ k)
            }
    
            if (k == 1 || k == 0){
                return 1
            } else if (this.store[k] == undefined){
                
                var computed = k * this.bang(k-1)
                this.store[k] = computed
                
                return computed
             
            } else if (this.store[k]) {
                return this.store[k]
            } else {
                throw new TypeError("Factorial input error. Received"+ k)
            }
        }
        
    }
})
.controller('application', ['$scope','dynamicFactorialStore', function($scope, FS){    
    
    $scope.$watch('seats', function(newval, oldval){
      
        if (newval) {
         
            var seats = Math.floor(newval)
            
            var totalNumberOfWaysToMessUpSeats = seats
            
            //Total number of ways to mess up seats
            for (var k = 1; k<seats; k++){
                totalNumberOfWaysToMessUpSeats = 
                    totalNumberOfWaysToMessUpSeats + (
                        
                        (seats-1) *
                            FS.choose(seats-1, k) * 
                                FS.bang(k) * seats
                        
                    )
            }
            
            
            
            //calculate average number of safe seats
            var averageNumberOfSafeSeats = (seats*seats) / 
                                            totalNumberOfWaysToMessUpSeats
            
            for (var k = 1; k<seats; k++){
                averageNumberOfSafeSeats = 
                    averageNumberOfSafeSeats + (
               ...