-
Notifications
You must be signed in to change notification settings - Fork 1
/
sieve-of-eratosthenes.html
56 lines (49 loc) · 1.58 KB
/
sieve-of-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
<!DOCTYPE html>
<html ng-app='sieve'>
<head>
<title>Sieve of Eratosthenes</title>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
<script src='http://underscorejs.org/underscore-min.js'></script>
<script>
angular.module('sieve', []);
var n = 100 + 1;
function MainCtrl($scope) {
$scope.arr = _.range(2,n);
$scope.color={}
_.each($scope.arr, function(i){$scope.color[i]='#eeeeee';});
$scope.mark = function(i, bgColor){
for (var j = i * i; j < n; j += i) {
$scope.color[j] = bgColor;
}
}
$scope.markAll = function() {
_.each(_.range(2, Math.sqrt(n)), function(i){
if ($scope.arr[i])
$scope.mark(i, 'red');
}
);
}
$scope.liClicked = function(i){
var m = (i*i*i)+10 * 12;
if (!(i > 3 && (i >= Math.sqrt(n) || i % 2 === 0 || i % 3 === 0)))
$scope.mark(i, '#'+m+m);
}
}
</script>
<style type="text/css" media="screen">
ul { width: 300px; list-style: none; line-height: normal; }
li { width: 25px; float: left; border: 1px black solid; }
</style>
</head>
<body ng-controller="MainCtrl">
<h3><a href='http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes'>Sieve of Eratosthenes</a></h3>
<p>Click on prime numbers less than 10 to see the associated composites eliminated progressively,
or mark all to mark all non-primes at once.</p>
<button ng-click="markAll()">Mark All</button>
<ul>
<li ng-repeat="i in arr" ng-style="{'background-color': color[i]}" ng-click='liClicked(i)'>
{{i}}
</li>
</ul>
</body>
</html>