-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
427 lines (356 loc) · 10.7 KB
/
index.js
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
var chart;
var series_array = [{
name: 'Placehold',
data: [5, 3, 4, 7, 2]
}];
var scatter_series_array = [{
name: 'Female',
color: 'rgba(223, 83, 83, .5)',
data: [[161.2, 51.6], [167.5, 59.0], [159.5, 49.2], [157.0, 63.0], [155.8, 53.6],
[170.0, 59.0], [159.1, 47.6], [166.0, 69.8], [176.2, 66.8], [160.2, 75.2],
[172.5, 55.2], [170.9, 54.2], [172.9, 62.5], [153.4, 42.0], [160.0, 50.0],
[147.2, 49.8], [168.2, 49.2], [175.0, 73.2], [157.0, 47.8], [167.6, 68.8],
[159.5, 50.6], [175.0, 82.5], [166.8, 57.2], [176.5, 87.8], [170.2, 72.8],
[176.5, 71.8], [164.4, 55.5], [160.7, 48.6], [174.0, 66.4], [163.8, 67.3]]
}];
$(document).ready(function() {
toastMessageFromClass('.error-box','error', 'toast-bottom-left');
/* toastMessageFromClass('.error','error', 'toast-bottom-left'); */
toastMessageFromClass('.notice-box','warning', 'toast-bottom-right');
toastMessageFromClass('.success-box','success', 'toast-top-right');
ko.applyBindings(new CongViewModel());
//buildChart();
//buildScatterChart();
});
function buildScatterChart(){
chart = new Highcharts.Chart({
chart: {
renderTo: 'scatterDiv',
type: 'scatter',
zoomType: 'xy'
},
title: {
text: 'Distributed Values'
},
xAxis: {
title: {
enabled: true,
text: ' '
},
startOnTick: true,
endOnTick: true,
showLastLabel: true,
min: 0,
max: 100
},
yAxis: {
title: {
text: 'Solutions'
},
min: 0,
labels:{
style: {
color: '#FFFFFF',
fontWeight: 'bold'
}
}
},
tooltip: {
formatter: function() {
return this.x;
}
},
legend: {
enabled: false,
layout: 'vertical',
align: 'right',
verticalAlign: 'top',
x: 0,
y: 30,
floating: true,
backgroundColor: '#FFFFFF',
borderWidth: 1
},
plotOptions: {
scatter: {
marker: {
radius: 5,
states: {
hover: {
enabled: true,
lineColor: 'rgb(100,100,100)'
}
}
},
states: {
hover: {
marker: {
enabled: false
}
}
}
}
},
series: scatter_series_array
});
}
function buildChart(){
chart = new Highcharts.Chart({
chart: {
renderTo: 'chartDiv',
type: 'column'
},
title: {
text: 'Stacked Remainders by Congruence'
},
yAxis: {
min: 0,
title: {
text: 'Remainders'
},
stackLabels: {
enabled: true,
style: {
fontWeight: 'bold',
color: (Highcharts.theme && Highcharts.theme.textColor) || 'gray'
}
}
},
xAxis:{
min:0,
max:100
},
legend: {
layout: 'vertical',
align: 'left',
verticalAlign: 'top',
x: 60,
y: 20,
floating: true,
backgroundColor: '#FFFFFF',
borderWidth: 1
},
tooltip: {
formatter: function() {
return '<b>'+ this.x +'</b><br/>'+
this.series.name +': '+ this.y +'<br/>'+
'Total: '+ this.point.stackTotal;
}
},
plotOptions: {
column: {
stacking: 'normal',
dataLabels: {
enabled: true,
color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white'
}
}
},
series: series_array
});
}
// Class to represent a row in the seat reservations grid
function Cong(thisRemainder, thisMod) {
var self = this;
self.remainder = ko.observable(thisRemainder);
self.mod = ko.observable(thisMod);
// self.remainder = thisRemainder;
// self.mod = thisMod;
}
// Overall viewmodel for this screen, along with initial state
function CongViewModel() {
var self = this;
// Non-editable catalog data - would come from the server
/*
self.availableMeals = [
{ mealName: "Standard (sandwich)", price: 0 },
{ mealName: "Premium (lobster)", price: 34.95 },
{ mealName: "Ultimate (whole zebra)", price: 290 }
];
*/
// this.remainder = ko.observable("2");
// this.mod = ko.observable("5");
self.congs = ko.observableArray([
new Cong(1, 2),
new Cong(2, 3)
]);
self.addCong = function() {
//var myMod = getRandom(3, 8) * 2 + 1; // odd number
var primeArray = [5,7,11,13,17,19,23,29];
var myIndex = getRandom(0, (primeArray.length - 1) ); // odd number
var myMod = primeArray[myIndex];
var myRemain = getRandom(0, 4); // odd number
toastr.warning(0 + " mod " + myMod + " added");
self.congs.push(new Cong(myRemain, myMod));
}
self.removeCong = function(cong) {
toastr.warning(cong.remainder() + " mod " + cong.mod() + " removed");
self.congs.remove(cong);
}
self.lcm = ko.computed(function() {
var mod_array = [];
var remainder_array = [];
var count = self.congs().length;
var myLcm = 1;
if(count){
for (var i = 0; i < count; i++){
var thisMod = self.congs()[i].mod();
myLcm = lcm(myLcm,thisMod);
}
}
return myLcm;
});
self.answer = ko.computed(function() {
var mod_array = [];
var remainder_array = [];
var count = self.congs().length;
if(count){
series_array = new Array();
scatter_series_array = new Array();
for (var i = 0; i < count; i++){
var thisMod = self.congs()[i].mod();
var thisRemainder = self.congs()[i].remainder();
if(thisMod == 0){
toastr.error(thisRemainder + " mod " + thisMod + " error! Cannot mod zero");
}
mod_array[i] = thisMod;
remainder_array[i] = thisRemainder;
// Build our series here for the chart
var thisSeries = new Object();
var thisScatterSeries = new Object();
thisSeries.name = thisRemainder + " mod " + thisMod;
thisScatterSeries.name = thisSeries.name;
var thisSeriesData = new Array();
var thisScatterSeriesData = new Array();
for(var k = 0; k < 100; k++){
var toAdd = (k - thisRemainder ) % thisMod;
thisSeriesData[k] = toAdd;
}
var realVal = 0;
for(var k = 0; realVal <= 100; k++){
var toAddScatter = k * thisMod + thisRemainder;
thisScatterSeriesData[k] = [toAddScatter, i];
realVal = toAddScatter;
}
thisSeries.data = thisSeriesData;
thisScatterSeries.data = thisScatterSeriesData;
//thisSeries.color = '#333333';
series_array[i] = thisSeries;
scatter_series_array[i] = thisScatterSeries;
}
buildChart();
buildScatterChart();
var thisMult = mod_array[0];
var thisRemain = remainder_array[0];
for(var j = 0; j < 100000; j++){
var match = true;
var testNum = thisMult * j + thisRemain;
for(var k = 1; k < count && match; k++){
var congRemainder = parseInt(testNum % mod_array[k] - remainder_array[k]);
if( congRemainder == 0){
match = true;
}else{
match = false;
}
}
if(match && testNum > 0){
toastr.success("<b><span style='font-size:30px'>" + testNum + " </span></b>is the first solution greater than zero");
return testNum;
}
}
}
toastr.error("System of congruences was not solved");
return 0;
});
/*
self.answer = ko.computed(function() {
var thisAnswer = 0;
var modMult = 1;
var e = new Array ();
var tmp;
var x = 0;
var count = self.congs().length;
for (var i = 0; i < count; i++){
modMult *= parseInt( self.congs()[i].mod);
}
for (var i = 0; i < count; i++){
var thisMod = self.congs()[i].mod;
tmp = extended_gcd(thisMod, modMult/thisMod);
e[i] = tmp[1]*modMult/thisMod;
}
for (i=0; i<count; i++) {
var thisRemainder = self.congs()[i].remainder;
x += e[i]*thisRemainder;
}
if (x >= modMult) {
x = x % modMult;
}
while (x < 0) {
x = x + modMult;
}
thisAnswer = x;
return thisAnswer;
});
*/
}
function getRandom(min, max) {
return min + Math.floor(Math.random() * (max - min + 1));
}
/*
* Returns the GCD of the given integers. Each input must be non-negative.
*/
function gcd(x, y) {
while (y != 0) {
var z = x % y;
x = y;
y = z;
}
return x;
}
/*
* Tests whether the given string represents an integer.
*/
function isInteger(str) {
return /^-?\d+$/.test(str);
}
function lcm(t1,t2){
var cm=1;
var f=gcd(t1,t2);
cm=t1*t2/f;
return cm;
}
function add_congruence(){
// toastr.warning("we should add now");
}
function extended_gcd(a,b) {
if (a%b==0) {
var temp = new Array(0,1);
return temp;
} else {
var temp = extended_gcd(b,a%b);
var temp2 = new Array(temp[1], temp[0]-temp[1]*(Math.floor(a/b)));
return temp2;
}
}
var toastMessageFromClass = function(className, toastType, position){
var $noticebox = $(className);
if($noticebox.length){
var message = $noticebox.html();
$noticebox.html('');
var delay = function(ms, func) {
return setTimeout(func, ms);
};
toastr.options = {
positionClass: position
};
delay(500, function() {
if(toastType == 'warning'){
return toastr.warning(message);
}else if(toastType == 'success'){
return toastr.success(message);
}if(toastType == 'error'){
return toastr.error(message);
}
});
}
}