-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjosephusProblem.html
More file actions
162 lines (115 loc) · 4.65 KB
/
josephusProblem.html
File metadata and controls
162 lines (115 loc) · 4.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<!DOCTYPE html>
<html>
<head>
<title>Josephus Algorithm</title>
<!--Custom Theme files-->
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="keywords" content="Register Login Widget template Responsive, Login form web template,Flat Pricing tables,Flat Drop downs Sign up Web Templates, Flat Web Templates, Login Signup Responsive web template, SmartPhone Compatible web template, free WebDesigns for Nokia, Samsung, LG, SonyEricsson, Motorola web design" />
<script type="application/x-javascript"> addEventListener("load", function() { setTimeout(hideURLbar, 0); }, false); function hideURLbar(){ window.scrollTo(0,1); } </script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<style>
body {padding: 100px;}
</style>
<script type="text/javascript">
var bla;
var form = document.getElementById('login')
var form = document.getElementById('login')
var startArray = [];
function calcwinner() {
var numberOfPeopletoKill = document.getElementById('numberofpeople').value;
numberOfPeopletoKill = parseInt(numberOfPeopletoKill, 10);
while (document.getElementById("killlist").firstChild) {
document.getElementById("killlist").removeChild(document.getElementById("killlist").firstChild);
}
function calcwrounds(numberOfPeopletoKill) {
var actualvalue=numberOfPeopletoKill;
var startvalue=numberOfPeopletoKill;
var tinesDivided=0;
for (var i=0; i<numberOfPeopletoKill/2;i++)
{
if (actualvalue>2)
{
actualvalue=actualvalue/2;
tinesDivided++;
}
}
console.log("needed Rounds to kill all:"+tinesDivided);
return tinesDivided;
}
document.getElementById("killlist").de
var roundsToCalc=calcwrounds(numberOfPeopletoKill);
for (var i = 1; i <= numberOfPeopletoKill; i++) {
var person = new Object();
person.startzahl = i;
startArray.push(person);
console.log("Startnummer " + person.startzahl);
var r=startArray[i-1];
}
var arraylength = startArray.length;
console.log(arraylength + " arraylength");
var endarray = [];
var endarrayTemp = [];
for (var i=0; i<roundsToCalc;i++){
for (var fuckyou = 0; fuckyou < arraylength; fuckyou++) {
var temp = fuckyou * 2;
if (temp < arraylength - 1) {
// console.log(temp +"temp");
var person = startArray[temp];
endarray.push(person);
// console.log("startArray[2].startzahl;"+startArray[2].startzahl);
// console.log("person.startzahl "+person.startzahl);
var node = document.createElement("LI");
var killtext= "Soldier #" + (person.startzahl ) + " killed soldier #"+(startArray[temp+1].startzahl);
// Create a <li> node
var textnode = document.createTextNode(killtext); // Create a text node
node.appendChild(textnode); // Append the text to <li>
document.getElementById("killlist").appendChild(node);
console.log("Startzahl Betrachtung Person" + (person.startzahl ) + " killed Person" + (startArray[temp+1].startzahl));
} else if (temp < arraylength) {
endarray.unshift(startArray[temp]);
}
}
endarrayTemp=endarray;
startArray=endarrayTemp;
endarray = [];
arraylength=arraylength/2;
}
document.getElementById("result").innerHTML="Soldier on position " + startArray[0].startzahl +" has survived.";
console.log("Person mit der Startnummer " + startArray[0].startzahl +" hat überlebt.") ;
startArray=[];
}
</script>
<!--//web-fonts-->
</head>
<body>
<h1>Josephus Algorithm </h1>
<p>Based on the Josephus Problem</p>
<p>Assumed x soldiers stand in a circle, beginning at the first soldier by soldier will kill his neighbour. </p>
<p>On which position will you survive as last soldier? </p>
<!-- main -->
<div class="main">
<!--login-profile-->
<!--login-profile-->
<!--signin-form-->
<div class="w3">
<div class="signin-form profile">
<br>
<h3>How many soldiers shall kill each other one by one?</h3>
<div class="login-form">
<input type="text" id="numberofpeople" placeholder=" number of soldiers" required="">
<div class="tp">
<button style="width:200px;height=100px" class="btn btn-success" value="Calculate Winner" onclick="calcwinner();">Calculate Winner-Position </button>
</div>
</div>
</div>
</div>
<div class="clear"></div>
<!--//signin-form-->
</div>
<br>
<h3 id="result" placeholder="result" ></h3>
<ul id="killlist">
</ul>
</body>
</html>