Puzzle - Amount of digits needed to display all possible scores.

Amount of digits needed to display all possible scores.

by Laurens Maneschijn

HTML

<h1>Puzzle</h1>
<h2>Amount of digits needed to display all possible scores.</h2>

<ul>
<li><input type="number" value="6" id="number_of_teams" oninput="update_rules();"> teams.
<li>each playing <input type="number" value="7" id="number_of_rounds" oninput="update_rules();"> rounds.
<li>1 to <span class="number_of_teams">6</span> points are divided per round.
    <br><small>(winner of a round gets <span class="number_of_teams">6</span> points, ..., last gets 1)</small>
</ul>

<p>
If at the end of every round you put up the score of each team, what are the max amount of digits you need?<br>
e.g. At some point each team has 11 points, that means you need 12 times the digit of 1.
</p>

<div class="links">
	<a target="_blank" href="https://jsfiddle.net/ElMoonLite/bpmhfks1/show" title="Fullscreen result. Shareable link, available for everyone, with minor jsfiddle wrapper elements on screen.">fullscreen</a>
	<a target="_blank" href="https://fiddle.jshell.net/ElMoonLite/bpmhfks1/show/?editor_console=" title="Fullscreen result. Only works when logged into jsfiddle. Shows only the result.">fullscreen 2</a>
</div>

<hr>

Run all rouns and show results:<br>
(start small, running above 10000x might take long or crash)<br>
<button onclick="reset();">reset</button><br>
<button onclick="run(1);">run 1x</button>
<button onclick="run(10);">run 10x</button>
<button onclick="run(100);">run 100x</button>
<button onclick="run(1000);">run 1000x</button>
<button onclick="run(10000);">run 10000x</button>
<button onclick="run(100000);">run 100000x</button>

<pre id="results"></pre>

CSS

h1,h2,h3,h4,h5,h6 {
	font-size: 100%;
	margin: 2px 0 0 0;
}

ul {
	padding-left: 1em;
}

input[type="number"] {
	width: 4ch;
}

.links {
	float: right;	
}
.links::after {
	clear: both;
}

JavaScript

var number_of_teams = 6;
var number_of_rounds = 7;
var max_tries = 1000;

var total_runs_completed = 0;
var max_digits_needed = [0,0,0,0,0,0,0,0,0,0];
var max_score_in_last_round = 0;
var min_score_in_last_round = Infinity;

function update_rules() {
	number_of_teams = parseInt(document.querySelector('#number_of_teams').value) || 1;
	number_of_rounds = parseInt(document.querySelector('#number_of_rounds').value) || 1;
	document.querySelectorAll('.number_of_teams').forEach((el) => {el.innerText = number_of_teams;});
	document.querySelectorAll('.number_of_rounds').forEach((el) => {el.innerText = number_of_rounds;});
	reset();
}
update_rules();

function reset() {
	var results = document.querySelector('#results');
	results.innerHTML = '';
	results.innerText = '';
	total_runs_completed = 0;
	max_digits_needed = [0,0,0,0,0,0,0,0,0,0];
	max_score_in_last_round = 0;
	min_score_in_last_round = Infinity;
}
function run(repeat) {
	var results = document.querySelector('#results');
	results.innerHTML = '';
	results.innerText = '';
	repeat = repeat || 1;
	for(n = 0 ; n < repeat ; n++) {
		team_points_per_round = run_all_rounds();
	}
	total_runs_completed += repeat;

	results.innerText += 'Total runs completed: ' + total_runs_completed;
	results.innerText += '\nMax amount of digits needed at any point (0, 1, ..., 9): ' + max_digits_needed;
	for (var i=0; i < max_digits_needed.length ; i++) {
		results.innerText += '\n' + i + ' : ' + max_digits_needed[i];
	}

	results.innerText += '\n\n';
	results.innerText += '\nmax score in last round: ' + max_score_in_last_round;
	results.innerText += '\nmin score in last round: ' + min_score_in_last_round;

	results.innerText += '\n\n';
	results.innerText += '\nLast run team points per round: ';
	for (var i=0; i < team_points_per_round.length ; i++) {
		results.innerText += '\nRound ' + (i+1) + ': ' + team_points_per_round[i];
	}
}

function run_all_rounds() {
	var i, round, distributable_points = [], team_points = [];
	
//	var...