Last active
August 29, 2015 13:58
-
-
Save xrd/9965127 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app = proc do |env| | |
Rack::Directory.new('.').call(env) | |
end | |
run app |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
angular.module( 'e', [ 'ngAnimate' ] ).controller 'eCtrl', [ '$scope', '$timeout', ($scope, $timeout) -> | |
$scope.stepInt = 50 | |
markC = (n) -> $scope.numbers[n].composite = true | |
isCorP = (i) -> $scope.numbers[i].composite or $scope.numbers[i].prime | |
$scope.markThePs = ( n=2 ) -> | |
if ($scope.p * n) <= $scope.bounds | |
markC( $scope.p * n ) | |
$timeout ( () -> $scope.markThePs(n+1) ), $scope.stepInt | |
else | |
keepGoing = false | |
for i in [$scope.p+1...$scope.bounds] | |
if not isCorP( i ) | |
keepGoing = true | |
$timeout ( () -> | |
$scope.primes.push i | |
$scope.numbers[i].prime = true | |
$scope.p = i | |
$timeout $scope.markThePs, $scope.stepInt | |
), $scope.stepInt | |
break | |
$scope.done = true unless keepGoing | |
$scope.setupNumbersAndPrimes = -> | |
$scope.primes = [] | |
$scope.numbers[0].prime = true | |
$scope.numbers[1].prime = true | |
$scope.numbers[2].prime = true | |
$scope.SieveOfE = () -> | |
$scope.setupNumbersAndPrimes() | |
$scope.p = 2 | |
$scope.markThePs() | |
$scope.SieveOfEwoT = -> | |
$scope.setupNumbersAndPrimes() | |
$scope.p = 2 | |
$scope.n = 2 | |
while $scope.p < $scope.bounds | |
$scope.markThePsWoT() | |
$scope.markThePsWoT = () -> | |
if $scope.p*$scope.n < $scope.bounds | |
markC( $scope.p * $scope.n ) | |
$scope.n = $scope.n+1 | |
else | |
for i in [$scope.p+1...$scope.bounds] | |
unless isCorP( i ) | |
$scope.primes.push i | |
$scope.numbers[i].prime = true | |
$scope.p = i | |
$scope.n = 2 | |
break | |
$scope.generateNumbers = (bounds) -> | |
$scope.bounds = bounds | |
$scope.numbers = [] | |
if bounds > 2 | |
for i in [0..bounds] | |
$scope.numbers.push { prime: false, number: i } | |
] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.number { | |
display: inline-block; | |
border: 1px solid black; | |
padding: 1px; | |
margin: 1px; | |
width: 62px; | |
text-align: center; | |
} | |
.composite { | |
background-color: #8df; | |
} | |
.tiny { | |
font-size: 0.6em; | |
} | |
.holder { | |
display: inline-block; | |
} | |
.prime { | |
background-color: #dfa; | |
} | |
@-webkit-keyframes explode { | |
10% { | |
-webkit-transform: scale(1,1); | |
} | |
30% { | |
-webkit-transform: scale(3,3); | |
} | |
100% { | |
-webkit-transform: scale(1,1); | |
} | |
} | |
.prime-add { | |
-webkit-animation: explode 3s; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<link href="e.css" rel="stylesheet" /> | |
</head> | |
<body ng-app="e"> | |
<div class='algorithm' ng-controller='eCtrl' ng-init='generateNumbers(100)'> | |
<div class='item'> | |
Algorithm Complexity: O(n log log n) | |
</div> | |
<div class='item'> | |
Pseudo polynomial | |
</div> | |
<div class='item'> | |
Step duration: | |
<input ng-model='stepInt' placeholder='Enter step interval'> | |
</div> | |
<div class='item'> | |
<button ng-click='SieveOfE()'>Calculate using Sieve of Eratosthenes</button> | |
</div> | |
<div class='item' ng-show='p'> | |
<h3 ng-show='p'>P: {{ p }}</h3> | |
</div> | |
<div id='placeholder' ng-show='p' style='width: 700px;'> | |
<span class='number' ng-if='0 != $index' ng-repeat='n in numbers'> | |
<div class='holder' ng-class='{ "composite" : n.composite, "prime" : n.prime }'> | |
{{n.number}} | |
<div class='tiny'>{{n.prime&&'prime'||n.composite&&'composite'||' '}}</div> | |
</div> | |
</span> | |
</div> | |
<div class='item' ng-show='done'>Primes {{ primes }}</div> | |
</div> | |
<script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.4/angular.js'></script> | |
<script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.4/angular-animate.js'></script> | |
<script src='e.js'></script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ctrl = undefined | |
scope = undefined | |
timeout = undefined | |
beforeEach -> | |
module( "e" ) | |
beforeEach inject ($controller, $rootScope, $timeout ) -> | |
scope = $rootScope.$new() | |
timeout = (fn, time) -> | |
fn() | |
ctrl = $controller( 'eCtrl', { $scope: scope, $timeout: timeout } ) | |
describe "#SieveOfE", -> | |
it "calculates the first primes using the timeout function", -> | |
NUMBER_COUNT = 100 | |
scope.generateNumbers( NUMBER_COUNT ) | |
scope.SieveOfE() | |
expect( scope.numbers[5].prime ).toBeTruthy() | |
expect( scope.numbers[8].prime ).toBeFalsy() | |
expect( scope.primes[0] ).toEqual( 3 ) | |
expect( scope.primes[1] ).toEqual( 5 ) | |
expect( scope.primes[scope.primes.length-1] ).toEqual( 97 ) | |
it "calculates the first primes using the timeout-free function", -> | |
NUMBER_COUNT = 100 | |
scope.generateNumbers( NUMBER_COUNT ) | |
scope.SieveOfEwoT() | |
expect( scope.numbers[5].prime ).toBeTruthy() | |
expect( scope.numbers[8].prime ).toBeFalsy() | |
expect( scope.primes[0] ).toEqual( 3 ) | |
expect( scope.primes[1] ).toEqual( 5 ) | |
expect( scope.primes[scope.primes.length-1] ).toEqual( 97 ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Karma configuration | |
// Generated on Mon Mar 31 2014 12:51:29 GMT-0700 (PDT) | |
module.exports = function(config) { | |
config.set({ | |
// base path, that will be used to resolve files and exclude | |
basePath: '', | |
// frameworks to use | |
frameworks: ['jasmine'], | |
// list of files / patterns to load in the browser | |
files: [ | |
"https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.4/angular.js", | |
"https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.4/angular-animate.js", | |
"https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.4/angular-mocks.js", | |
"e.coffee", | |
"e.spec.coffee" | |
], | |
// test results reporter to use | |
// possible values: 'dots', 'progress', 'junit', 'growl', 'coverage' | |
reporters: ['progress'], | |
// web server port | |
port: 9876, | |
// enable / disable colors in the output (reporters and logs) | |
colors: true, | |
// level of logging | |
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG | |
logLevel: config.LOG_INFO, | |
// enable / disable watching file and executing tests whenever any file changes | |
autoWatch: true, | |
browsers: ['Chrome'], | |
// If browser does not capture in given timeout [ms], kill it | |
captureTimeout: 60000, | |
singleRun: false | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment