Last active
December 31, 2015 03:49
-
-
Save joaojeronimo/7929772 to your computer and use it in GitHub Desktop.
CrowdProcess Tutorial
This file contains 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
// a brute force prime number finder | |
// interval should be something like | |
// {"lower": 1000, "upper": 2000} | |
function Run (interval) { | |
// we will walk through the following interval | |
// to look for prime numbers: | |
var lower = interval.lower; | |
var upper = interval.upper; | |
var primeNumbers = []; // we'll store them here | |
// looping through the interval | |
for (var i = lower; i < upper; i++) { | |
// the isPrime function checks if a number is prime. | |
// it's declared outside of the Run function, on line 26. | |
if (isPrime(i)) | |
primeNumbers.push(i); | |
} | |
// return all the prime numbers that we found | |
return primeNumbers; | |
} | |
function isPrime (i) { | |
var number = i; // number to check | |
var n = i // we'll iterate to all the numbers before it | |
var isPrime = true; // assume it's prime, we'll try to disprove | |
while (n--) { | |
// every number is divisible by itself and one, | |
// we don't want to divide it by zero | |
if (n === 0 || n === 1 || n === number) | |
continue; // continue actually means skip the rest | |
// of this iteration and keep looping | |
// if it's divisible by any other number | |
if (!(number % n)) | |
isPrime = false; // then it's not a prime number | |
} | |
return isPrime; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment