Skip to content

Instantly share code, notes, and snippets.

View farskid's full-sized avatar
🎯
Focusing

Farzad Yousefzadeh farskid

🎯
Focusing
View GitHub Profile
@farskid
farskid / JavaScript Sieve Of Atkin.js
Last active July 15, 2016 13:51 — forked from rizalp/JavaScript Sieve Of Atkin.js
return array of primes below limit using Sieve of Atkin Algorithmhttp://en.wikipedia.org/wiki/Sieve_of_Atkin#JavaScript #primes
function sieveOfAtkin(limit){
var limitSqrt = Math.sqrt(limit);
var sieve = [];
var n;
//prime start from 2, and 3
sieve[2] = true;
sieve[3] = true;
for (var x = 1; x <= limitSqrt; x++) {
@farskid
farskid / i18n-express4-cookie-example.js
Created January 10, 2017 20:57 — forked from mashpie/i18n-express4-cookie-example.js
i18n-express4-cookie-example

Keybase proof

I hereby claim:

  • I am farskid on github.
  • I am farskid (https://keybase.io/farskid) on keybase.
  • I have a public key ASDf3J3qhJ_y_QkaxGwL65icVgcJz4divVFQFf8VT58Qxgo

To claim this, I am signing this object:

@farskid
farskid / recursive-version.js
Last active October 24, 2017 07:54
Find the Greatest Common Divisor of two numbers
function gcd(n, m) {
if (!m) { return n; }
return (m, n % m);
}
console.log(gcd(512, 1024)); // 256
@farskid
farskid / recursive-version.js
Created October 24, 2017 08:00
Generate a range in Javascript
function recursiveRange(a, b) {
if (a + 1 === b) {
return [];
}
return [a + 1].concat(range(a + 1, b));
}
@farskid
farskid / recursive-version.js
Created October 24, 2017 08:07
Sum of an array of integers in Javascript
function sumOfIntArray(array) {
if (!array.length) {
return 0;
}
return array.pop() + sumOfIntArray(array);
}
@farskid
farskid / recursive-version.js
Created October 24, 2017 08:10
Calculate the exponential of an integer in Javascript
function expo(num, power) {
if (power === 1) return num;
return num * expo(num, power - 1);
}
@farskid
farskid / recursive-version.js
Last active October 26, 2017 00:12
Log first N numbers in Fibonacci sequence in Javascript
function fibo(n) {
if (n === 1) return 0;
if (n === 2) return 1;
return fibo(n - 1) + fibo(n - 2);
}
function fiboLogger(n) {
if (n === 0) return [];
return [fibo(n)].concat(fiboLogger(n-1));
}
@farskid
farskid / sort-asc.js
Created October 24, 2017 08:28
Sort array of integers in descending and ascending order in Javascript
function sortAsc(array) {
if (!array.length) {
return [];
}
return array.splice(
array.indexOf(
Math.min.apply(null, array)
), 1)
.concat(sortAsc(array));
}
@farskid
farskid / flatten.js
Created October 24, 2017 19:33
Flatten multi-dimensional arrays recursively in Javascript
function flattenArray(array) {
let result = [];
array.forEach(function(item, i) {
if (Array.isArray(item)) {
result = result.concat(flattenArray(array[i]));
}
else {
result = result.concat([item]);
}
});