-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsieve_eratosthenes.html
339 lines (262 loc) · 9.98 KB
/
sieve_eratosthenes.html
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
<html>
<head>
<title>Animation</title>
</head>
<style>
* {
font-family: helvetica;
font-size: 12px;
}
body {
margin: 0;
padding: 0;
}
p.title {
border: 1px solid #ccc;
background-color: #dedede;
padding: 5px;
text-align: center;
color: #552222;
font-weight: bold;
font-size: 14px;
}
div.col {
float: left;
display: block;
margin-right: 10px;
margin-left: 10px;
}
table.grid {
border: 1px solid #ccc;
border-collapse: separate;
}
table.grid td {
color: #444;
padding: 10px 8px;
text-align: center;
border: 5px solid #fff;
}
table.grid td.even {
color: #444;
padding: 10px 8px;
text-align: center;
background-color: #DCA7A7;
border: 5px solid #fff;
}
table.grid td.prime {
color: #444;
padding: 10px 8px;
text-align: center;
background-color: #AAAA55;
}
div.logger {
border: 1px solid #ccc;
width: 300px;
}
div.activity-title {
border: 1px solid #ccc;
background-color: #dedede;
padding: 5px;
text-align: center;
color: #552222;
font-weight: bold;
font-size: 13px;
}
div.event {
padding: 5px 5px;
color: #444;
}
div.event ul {
list-style: none;
margin: 0;
padding: 0;
}
div.event ul li.event-name {
display: inline-block;
width: 75%;
border-right: 1px dotted #ccc;
color: inherit;
}
div.event ul li.status {
display: inline-block;
text-transform: uppercase;
color: green;
font-weight: bold;
text-align: center;
padding-left: 5px;
}
div.event-col {
float: left;
display: block;
border: 1px solid red;
}
</style>
<body>
<p id="op-title" class="title">Sieve of Eratosthenes Animation</p>
<div class="col">
<div id="container"></div>
</div>
<div class="col">
<div class="logger" id="event-log">
<div class="activity-title">Activity</div>
<div class="event">
<ul>
<li class="event-name">Showing first 120 Natural Numbers</li>
<li class="status">Done</li>
</ul>
</div>
</div>
</div>
</body>
<script type="text/javascript">
// Number of natural numbers in the grid
var limit = 120;
var table = document.createElement( 'table' );
table.setAttribute( 'cellpadding', '0' );
table.setAttribute( 'cellspacing', '0' );
table.setAttribute( 'border', '0' );
table.setAttribute( 'class', 'grid' );
// First Loop determines row count
// Each row contains 10 numbers, and hence we increment by 10
for( var i = 1; i < limit; i+=10 ) {
var tr = document.createElement( 'tr' );
// Second Loop determines data in each row
for ( var j = 0; j < 10; j++ ) {
var num = i+j;
// Create td
var td = document.createElement( 'td' );
var id = 'num-' + num;
td.setAttribute( 'id', id );
td.innerHTML = num;
// Add the "td" to the "tr"
tr.appendChild( td );
}
// Add the "tr" to the "table"
table.appendChild( tr );
}
// Add "table" to the container div
var container_div = document.getElementById( "container" );
container_div.appendChild( table );
// Determine even numbers, we wait (sleep) 1 seconds before executing the function
var timer = 1 * 1000;
var next_timer;
setTimeout( function() {
next_timer = show_even_numbers( timer );
}, timer );
// Waiting 8 seconds
setTimeout( function() {
next_timer = mark_primes( timer );
}, 8 * timer );
function show_even_numbers( wait ) {
var logger = document.getElementById( 'event-log' );
// For logging
var event = document.createElement( 'div' );
event.setAttribute( 'class', 'event' );
var ul = document.createElement( 'ul' );
var li_desc = document.createElement( 'li' );
li_desc.setAttribute( 'class', 'event-name' );
li_desc.innerHTML = 'Hightlighting Even Numbers';
// Add li to ul
ul.appendChild( li_desc );
var li_stat = document.createElement( 'li' );
li_stat.setAttribute( 'class', 'status' );
// Add li to ul
ul.appendChild( li_stat );
// Add ul to event
event.appendChild( ul );
// Add event to event-log
logger.appendChild( event );
var sleep = wait;
var lastsleep;
for( var i = 1; i < limit; i += 10 ) {
for( var j = 0; j < 10; j++ ) {
// Highlight Even numbered "td"
if( (i+j) % 2 == 0 ) {
var fnc = function( y ) {
return function() {
var id = 'num-' + (y);
var td = document.getElementById( id );
td.setAttribute( 'class', 'even' );
if( y == limit ) {
li_stat.innerHTML = 'Done';
}
}
}(i+j);
var t = setTimeout( fnc, sleep );
sleep = sleep + 100;
if( (i+j) == limit ) {
lastsleep = sleep;
}
}
}
}
return lastsleep;
}
function mark_primes( wait ) {
var logger = document.getElementById( 'event-log' );
// For logging
var event = document.createElement( 'div' );
event.setAttribute( 'class', 'event' );
var ul = document.createElement( 'ul' );
var li_desc = document.createElement( 'li' );
li_desc.setAttribute( 'class', 'event-name' );
li_desc.innerHTML = 'Finding prime Numbers';
// Add li to ul
ul.appendChild( li_desc );
var li_stat = document.createElement( 'li' );
li_stat.setAttribute( 'class', 'status' );
// Add li to ul
ul.appendChild( li_stat );
// Add ul to event
event.appendChild( ul );
// Add event to event-log
logger.appendChild( event );
var sleep = wait;
var lastsleep;
for( var i = 1; i < limit; i += 10 ) {
for( var j = 0; j < 10; j++ ) {
// Highlight prime "td"
if( is_prime(i+j) == 1 ) {
var fnc = function( y ) {
return function() {
var id = 'num-' + (y);
var td = document.getElementById( id );
td.setAttribute( 'class', 'prime' );
}
}(i+j);
var t = setTimeout( fnc, sleep );
sleep = sleep + 100;
}
setTimeout( function() {
li_stat.innerHTML = 'Done';
lastsleep = sleep;
}, 4000);
}
}
return lastsleep;
}
// Function to check a number is prime or not
function is_prime( num ) {
// Assuming initially the num is prime
var prime = 1;
// return false if num is less than 2
if( num < 2 ) {
return false;
}
// return true if num is 2
if( num == 2 ) {
return true;
}
// Check if each number starting from 2 is multiple of num
// if it is a multiple, change the flag (mark as not prime)
// and break from the loop
for( var c = 2; c < (num-1); c++) {
if( num % c == 0 ) {
prime = 0;
break;
}
}
return ( prime == 0 ) ? false : true;
}
</script>
</html>