Skip to content

Instantly share code, notes, and snippets.

@vicneanschi
Last active March 11, 2016 19:30
Show Gist options
  • Save vicneanschi/4ceed82f619042c96fed to your computer and use it in GitHub Desktop.
Save vicneanschi/4ceed82f619042c96fed to your computer and use it in GitHub Desktop.
function primes(num){
var result = [2, 3];
for(var i = 5; i<= num; i+=2){
if ( result.every( x => i % x ) ) {
result.push(i);
}
}
return result;
}
function gcdArr(arr){
return arr.reduce((prev, curr) => gcd(prev, curr));
}
function gcd(a, b) {
var t;
while (b !== 0) {
t = b;
b = a % b;
a = t;
}
return a;
}
function lcm(a, b){
return a * b / gcd(a, b);
}
function getRotations(x){
var result = [];
x = x.toString().split('');
for(var i=0; i< x.length-1; i++){
// rotate
x.unshift(x.pop());
result.push(x.join(''));
}
return result;
}
function formatNumber(x){
// Matches a zero-width non-word boundary, such as between two letters or between two spaces.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
//return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return x.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment