Earn extra honor and gain new allies!
Honor is earned for each new codewarrior who joins.
Learn more

Javascript version of fast code to get all the proper factors of a number

var sortNumber = function(a, b){return a - b;};

function getDiv(n){
    var factL= [], i;
    for (i = 1; i <= Math.floor(Math.sqrt(n)); i += 1) {
        if (n % i === 0) {
            factL.push(i);
            if (n / i !== i) factL.push(n / i);
        }
    }
    factL.sort(sortNumber);
    return factL;
}

We have n different groups of elements and we want to know all the possible combinations of n elements from these groups.
Each combinations should be formed having one element from each group.
The function generates all the possible different combinations.

function groupCombinations_() {
    var r = [], arg = arguments, max = arg.length-1;
    function helper(arr, i) {
        for (var j=0, l=arg[i].length; j<l; j++) {
            var a = arr.slice(0); 
            a.push(arg[i][j]);
            if (i==max)
                r.push(a);
            else
                helper(a, i+1);
        }
    }
    helper([], 0);
    return r;
}

function groupCombinations(arr2D) {
    var combL = groupCombinations_.apply(this, arr2D);
    return [combL.length, combL];
}

We have n different groups of elements and we want to know all the possible combinations of n elements from these groups.
Each combinations should be formed having one element from each group.
The function generates all the possible different combinations.

def groups_combination_(a)
	  a[1..-1].inject(a[0]){ |m,v| m = m.product(v).map(&:flatten) }
end

def groups_combinations(arr2D)
    res = groups_combination_(arr2D)
    return [res.length, res]
end

We have n different groups of elements and we want to know all the possible combinations of n elements from these groups.
Each combinations should be formed having one element from each group.
The function generates all the possible different combinations.

from itertools import *
def product_(arr2D):
    return product(*arr2D)

def groups_combinations(arr2D):
    res = []
    for comb in product_(arr2D):
        res.append(comb)
    return [len(res), res]

A probabilistic code for primality tests. There are some numbers that may give an incorrect result

import random
 
def decompose(n):
    exponentOfTwo = 0
    while n % 2 == 0:
        n = n/2
        exponentOfTwo += 1
    return exponentOfTwo, n
    
def isWitness(possibleWitness, p, exponent, remainder):
    possibleWitness = pow(possibleWitness, remainder, p)
    if possibleWitness == 1 or possibleWitness == p - 1:
        return False
    for _ in range(exponent):
        possibleWitness = pow(possibleWitness, 2, p)
        if possibleWitness == p - 1:
            return False
    return True
    
def is_prime(p, accuracy=100):
    if p == 2 or p == 3: return True
    if p < 2: return False
    exponent, remainder = decompose(p - 1)
    for _ in range(accuracy):
        possibleWitness = random.randint(2, p - 2)
        if isWitness(possibleWitness, p, exponent, remainder):
            return False
    return True

A very easy method importting the library prime in Ruby. It's a bit slower than the last code we've seen in Ruby

require 'prime'

def is_prime(n)
    Prime.prime?(n)
end

Again we can see that the runtime is less (more less than a half) than the previous version

def is_prime(n)
    return false if n < 2
    return true if n == 2
    return false if n % 2 == 0
    (3..Math.sqrt(n).round).step(2) do |x|
        return false if n % x == 0
    end
    return true
end

I received a fork to my version in python by the user Mc Code. The average runtime for different tries is under 500 ms. The half runtime that the previous version in Javascript, too (1000 ms)

function isPrime(n) {
    if (n < 2) return false;
    else if (n == 2) return true;
    else if (n % 2 === 0) return false;
    for (var x = 3; x <= Math.floor(Math.sqrt(n)); x += 2) {if (n % x === 0) return false;}
    return true;
}

A very fast code to test if a number is a prime. You can see its performance having 100 tests from 1000 to 10e12 in less than 1000 ms

def is_prime(n)
    return false if n < 2
    for x in 2.. Math.sqrt(n).round
        return false if n % x == 0
    end
    return true
end

A very fast code to test if a number is a prime. You can see its performance having 100 tests from 1000 to 10e12 in less than 1000 ms

function isPrime(n) {
    if (n < 2) return false;
    for (var x = 2; x <= Math.floor(Math.sqrt(n)); x++) {if (n % x == 0) return false;}
    return true;
}

A very fast code to test if a number is a prime. You can see its performance having 100 tests from 1000 to 10e12 in less than 3000 ms

from math import sqrt
def is_prime(n):
    if n < 2: return False
    for x in range(2, int(sqrt(n)) + 1):
        if n % x == 0: return False
    return True

A little help to calculate the distances between two points in a plane(2D cases) and in the space (3D cases)

function distance2D(pA, pB) {
    if (pA == pB) return 0;
    var xA = pA[0], yA = pA[1], xB= pB[0], yB = pB[1];
    return Math.sqrt((xA - xB)**2 + (yA - yB)**2)
}
    
function distance3D(pA, pB) {
    if (pA == pB) return 0;
    var xA = pA[0], yA = pA[1], zA = pA[2], xB= pB[0], yB = pB[1], zB = pB[2];
    return Math.sqrt((xA - xB)**2 + (yA - yB)**2 + (zA - zB) **2);
}

A little help for claculate the distances between two points in a plane(2D cases) and in the space (3D cases)

def distance2D(pA, pB)
    return 0 if pA == pB
    xA = pA[0]; yA = pA[1]; xB= pB[0]; yB = pB[1]
    return Math.sqrt((xA - xB)**2 + (yA - yB)**2)
end
    
def distance3D(pA, pB)
    return 0 if pA == pB
    xA = pA[0]; yA = pA[1]; zA = pA[2]; xB= pB[0]; yB = pB[1]; zB = pB[2]
    return Math.sqrt((xA - xB)**2 + (yA - yB)**2 + (zA - zB) **2)
end  

def range (min, max)
    rand * (max-min) + min
end

A little help for claculate the distances between two points in a plane(2D case) and in the space (3D case)

from math import sqrt

def distance2D(pA, pB):
    if pA == pB: return 0
    xA, yA = tuple(pA); xB, yB = tuple(pB)
    return sqrt((xA - xB)**2 + (yA - yB)**2)
    
def distance3D(pA, pB):
    if pA == pB: return 0
    xA, yA, zA = tuple(pA); xB, yB, zB = tuple(pB)
    return sqrt((xA - xB)**2 + (yA - yB)**2 + (zA - zB) **2)

With this one we close the trilogy Javascript-Python-Ruby to get the primes factor.

def prime_fac(i)
    factors = []
    check = proc do |p|
        while(q, r = i.divmod(p) 
        	   r.zero?)
            factors << p
            i = q
        end
    end
    check[2]
    check[3]
    p = 5
    while p * p <= i
        check[p]
        p += 2
        check[p]
        p += 4    # skip multiples of 2 and 3
    end
    factors << i if i > 1
    factors
end
Loading more items...