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;
}
function getDivCheck(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;
}
describe("Static Cases", function(){
it("n = 200", function(){
Test.assertSimilar(getDiv(200), [1, 2, 4, 5, 8, 10, 20, 25, 40, 50, 100, 200]);
});
it("n = 560", function(){
Test.assertSimilar(getDiv(560), [ 1, 2, 4, 5, 7, 8, 10, 14, 16, 20, 28, 35, 40, 56, 70, 80, 112, 140, 280, 560 ]);
});
it("n = 755", function(){
Test.assertSimilar(getDiv(755), [ 1, 5, 151, 755 ]);
});
});
function randint(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
describe("Random Cases", function(){
it("More than 1000 Random Tests with challenging values up to 1000000000(10e9)" , function(){
for (var i = 0; i <= 1000; i++) {
var n = randint(1000, 1000000000);
var result = getDivCheck(n), res = getDiv(n);
it("Testing for n = " + n.toString(), function(){
Test.assertSimilar(res,result);
})
}
})
})
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];
}
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 groupCombinationsCheck(arr2D) {
var combL = groupCombinations_.apply(this, arr2D);
return [combL.length, combL];
}
describe("Basic Tests", function(){
it("Simple Cases", function(){
var arr2D = [[1,2,3,4], [1,2,3,4,5,6]];
Test.assertSimilar(groupCombinations(arr2D), [24, [[1, 1], [1, 2], [1, 3], [1, 4],
[1, 5], [1, 6], [2, 1], [2, 2],[2, 3], [2, 4], [2, 5], [2, 6], [3, 1], [3, 2],
[3, 3], [3, 4], [3, 5], [3, 6], [4, 1], [4, 2], [4, 3], [4, 4],[4, 5], [4, 6]]]);
arr2D = [[1,2,3,4,5,6], [1,2,3,4,5,6]];
Test.assertSimilar(groupCombinations(arr2D), [36, [[1, 1], [1, 2], [1, 3], [1, 4],
[1, 5], [1, 6], [2, 1], [2, 2], [2, 3], [2, 4], [2, 5], [2, 6], [3, 1], [3, 2],
[3, 3], [3, 4], [3, 5], [3, 6], [4, 1], [4, 2], [4, 3], [4, 4], [4, 5], [4, 6],
[5, 1], [5, 2], [5, 3], [5, 4], [5, 5], [5, 6], [6, 1], [6, 2], [6, 3], [6, 4],
[6, 5], [6, 6]]]);
});
});
function range(start, stop){
var a=[start], b=start;
while(b<stop){b++;a.push(b)}
return a;
}
function randint(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function choice(items) {
var item = items[Math.floor(Math.random()*items.length)];
return item;
}
describe("Random Tests", function(){
it("Challenging Cases", function(){
var ranges = [range(1, 4), range(1,6), range(1,8), range(1,10), range(1,12), range(1,20)];
for (var h = 1; h <= 20 ; h ++) {
var n = randint(2, 3);
var arr2D = [];
while (true){
arr2D.push(choice(ranges))
if (arr2D.length == n) break;
}
var result = groupCombinationsCheck(arr2D);
var res = groupCombinations(arr2D);
it("Testing for arr2D = " + arr2D.toString(), function(){
Test.assertSimilar(res, result);
})
}
})
})
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
def groups_combination_(a)
a[1..-1].inject(a[0]){ |m,v| m = m.product(v).map(&:flatten) }
end
def groups_combinations_check(arr2D)
res = groups_combination_(arr2D)
return [res.length, res]
end
describe "Basic Tests" do
it "Simple Cases" do
arr2D = [[1,2,3,4], [1,2,3,4,5,6]]
Test.assert_equals(groups_combinations(arr2D),[24, [[1, 1], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [2, 1], [2, 2],[2, 3], [2, 4], [2, 5], [2, 6], [3, 1], [3, 2], [3, 3], [3, 4], [3, 5], [3, 6], [4, 1], [4, 2], [4, 3], [4, 4],[4, 5], [4, 6]]])
arr2D = [[1,2,3,4,5,6], [1,2,3,4,5,6]]
Test.assert_equals(groups_combinations(arr2D), [36, [[1, 1], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [2, 1], [2, 2], [2, 3], [2, 4], [2, 5], [2, 6], [3, 1], [3, 2], [3, 3], [3, 4], [3, 5], [3, 6],
[4, 1], [4, 2], [4, 3], [4, 4], [4, 5], [4, 6], [5, 1], [5, 2], [5, 3], [5, 4], [5, 5], [5, 6], [6, 1], [6, 2], [6, 3], [6, 4], [6, 5], [6, 6]]])
end
end
describe "Random Tests" do
it "Challenging Cases" do
ranges = [(1..3).to_a, (1..6).to_a, (1..8).to_a, (1..10).to_a,
(1..12).to_a, (1..20).to_a]
for h in 1..10
n = rand(2..3)
arr2D = []
while true
arr2D << ranges.sample
break if arr2D.length == n
end
result = groups_combinations_check(arr2D)
res = groups_combinations(arr2D)
it "Testing for arr2D: " + arr2D.to_s do
Test.assert_equals(res, result)
end
end
end
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]
from itertools import *
def product_(arr2D):
return product(*arr2D)
def groups_combinations_check(arr2D):
res = []
for comb in product_(arr2D):
res.append(comb)
return [len(res), res]
test.describe("Basic Tests")
arr2D = [[1,2,3,4], [1,2,3,4,5,6]]
test.assert_equals(groups_combinations(arr2D), [24, [(1, 1),
(1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 1), (2, 2),
(2, 3), (2, 4), (2, 5), (2, 6), (3, 1), (3, 2), (3, 3),
(3, 4), (3, 5), (3, 6), (4, 1), (4, 2), (4, 3), (4, 4),
(4, 5), (4, 6)]])
arr2D = [[1,2,3,4,5,6], [1,2,3,4,5,6]]
test.assert_equals(groups_combinations(arr2D),[36, [(1, 1),
(1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (2, 1), (2, 2),
(2, 3), (2, 4), (2, 5), (2, 6), (3, 1), (3, 2), (3, 3),
(3, 4), (3, 5), (3, 6), (4, 1), (4, 2), (4, 3), (4, 4),
(4, 5), (4, 6), (5, 1), (5, 2), (5, 3), (5, 4), (5, 5),
(5, 6), (6, 1), (6, 2), (6, 3), (6, 4), (6, 5), (6, 6)]])
test.describe("Random Tests")
from random import randint, choice
ranges = [range(1,5), range(1,7), range(1,9), range(1,12),
range(1,13), range(1,21)]
for h in range(20):
n = randint(2, 6)
arr2D = []
while True:
arr2D.append(choice(ranges))
if len(arr2D) == n: break
result = groups_combinations_check(arr2D)
res = groups_combinations(arr2D)
test.it("Testing for arr2D: " + str(arr2D))
test.assert_equals(res, result)
test.it("It gives " + str(res[0]) + " combinations")
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
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_check(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
test.describe("Basic Tests")
test.assert_equals(is_prime(1), False)
test.assert_equals(is_prime(2), True)
test.assert_equals(is_prime(3), True)
test.assert_equals(is_prime(5), True)
test.assert_equals(is_prime(7), True)
test.assert_equals(is_prime(11), True)
test.assert_equals(is_prime(13), True)
test.assert_equals(is_prime(15), False)
test.assert_equals(is_prime(18), False)
test.assert_equals(is_prime(21), False)
test.describe("Random Tests")
from random import randint
for h in range(100):
n = randint(1000, 1000000000001)
result = is_prime_check(n)
res = is_prime(n)
test.assert_equals(res, result)
test.it("Testing for n = " + str(n) + " Result: " + str(result))
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
def is_prime_check(n)
Prime.prime?(n)
end
describe "Basic Tests" do
it "Low values of n" do
Test.assert_equals(is_prime(1), false)
Test.assert_equals(is_prime(2), true)
Test.assert_equals(is_prime(3), true)
Test.assert_equals(is_prime(5), true)
Test.assert_equals(is_prime(7), true)
Test.assert_equals(is_prime(11), true)
Test.assert_equals(is_prime(13), true)
Test.assert_equals(is_prime(15), false)
Test.assert_equals(is_prime(18), false)
Test.assert_equals(is_prime(21), false)
end
end
describe "Random Tests" do
it "Values of n between 1000 and 10e12" do
for h in 1..100
n = rand(1000..1000000000000)
result = is_prime_check(n)
res = is_prime(n)
it "Testing for n = " + n.to_s do
Test.assert_equals(res, result)
end
end
end
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
def is_prime_check(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
describe "Basic Tests" do
it "Low values of n" do
Test.assert_equals(is_prime(1), false)
Test.assert_equals(is_prime(2), true)
Test.assert_equals(is_prime(3), true)
Test.assert_equals(is_prime(5), true)
Test.assert_equals(is_prime(7), true)
Test.assert_equals(is_prime(11), true)
Test.assert_equals(is_prime(13), true)
Test.assert_equals(is_prime(15), false)
Test.assert_equals(is_prime(18), false)
Test.assert_equals(is_prime(21), false)
end
end
describe "Random Tests" do
it "Values of n between 1000 and 10e12" do
for h in 1..100
n = rand(1000..1000000000000)
result = is_prime_check(n)
res = is_prime(n)
it "Testing for n = " + n.to_s do
Test.assert_equals(res, result)
end
end
end
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;
}
function isPrimeCheck(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;
}
describe("Basic Tests", function(){
it("Low values of n", function(){
Test.assertEquals(isPrime(1), false)
Test.assertEquals(isPrime(2), true)
Test.assertEquals(isPrime(3), true)
Test.assertEquals(isPrime(5), true)
Test.assertEquals(isPrime(7), true)
Test.assertEquals(isPrime(11), true)
Test.assertEquals(isPrime(13), true)
Test.assertEquals(isPrime(15), false)
Test.assertEquals(isPrime(18), false)
Test.assertEquals(isPrime(21), false)
});
});
function randint(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
describe("Random Tests", function(){
it("Values of n between 1000 and 10e12", function(){
for (var h = 1; h <= 100; h++) {
var n = randint(1000, 1000000000001);
var result = isPrimeCheck(n), res = isPrime(n);
it("Testing for n = " + n.toString(), function(){
Test.assertEquals(res, result);
})
}
})
})
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
def is_prime_check(n)
return false if n < 2
for x in 2.. Math.sqrt(n).round
return false if n % x == 0
end
return true
end
describe "Basic Tests" do
it "Low values of n" do
Test.assert_equals(is_prime(1), false)
Test.assert_equals(is_prime(2), true)
Test.assert_equals(is_prime(3), true)
Test.assert_equals(is_prime(5), true)
Test.assert_equals(is_prime(7), true)
Test.assert_equals(is_prime(11), true)
Test.assert_equals(is_prime(13), true)
Test.assert_equals(is_prime(15), false)
Test.assert_equals(is_prime(18), false)
Test.assert_equals(is_prime(21), false)
end
end
describe "Random Tests" do
it "Values of n between 1000 and 10e12" do
for h in 1..100
n = rand(1000..1000000000000)
result = is_prime_check(n)
res = is_prime(n)
it "Testing for n = " + n.to_s do
Test.assert_equals(res, result)
end
end
end
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;
}
function isPrimeCheck(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;
}
describe("Basic Tests", function(){
it("Low values of n", function(){
Test.assertEquals(isPrime(1), false)
Test.assertEquals(isPrime(2), true)
Test.assertEquals(isPrime(3), true)
Test.assertEquals(isPrime(5), true)
Test.assertEquals(isPrime(7), true)
Test.assertEquals(isPrime(11), true)
Test.assertEquals(isPrime(13), true)
Test.assertEquals(isPrime(15), false)
Test.assertEquals(isPrime(18), false)
Test.assertEquals(isPrime(21), false)
});
});
function randint(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
describe("Random Tests", function(){
it("Values of n between 1000 and 10e12", function(){
for (var h = 1; h <= 100; h++) {
var n = randint(1000, 1000000000001);
var result = isPrimeCheck(n), res = isPrime(n);
it("Testing for n = " + n.toString(), function(){
Test.assertEquals(res, result);
})
}
})
})
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
from math import sqrt
def is_prime_check(n):
if n < 2: return False
for x in range(2, int(sqrt(n)) + 1):
if n % x == 0: return False
return True
test.describe("Basic Tests")
test.assert_equals(is_prime(1), False)
test.assert_equals(is_prime(2), True)
test.assert_equals(is_prime(3), True)
test.assert_equals(is_prime(5), True)
test.assert_equals(is_prime(7), True)
test.assert_equals(is_prime(11), True)
test.assert_equals(is_prime(13), True)
test.assert_equals(is_prime(15), False)
test.assert_equals(is_prime(18), False)
test.assert_equals(is_prime(21), False)
test.describe("Random Tests")
from random import randint
for h in range(100):
n = randint(1000, 1000000000001)
result = is_prime_check(n)
res = is_prime(n)
test.assert_equals(res, result)
test.it("Testing for n = " + str(n) + " Result: " + str(result))
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);
}
function distance2DCheck(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 distance3DCheck(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);
}
function assertFuzzyEquals(actual, expected, msg){
var inrange = Math.abs(actual - expected) <= 1e-10;
Test.expect(inrange, msg || "At 1e-10: Expected value must be " + expected.toExponential(10) +", but got " + actual.toExponential(10));
}
function randint(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
describe("Basic Tests for 2D Cases", function(){
it("Low Values of x, y", function(){
var pA = [1, 1], pB = [2, 2];
assertFuzzyEquals(distance2D(pA, pB), 1.41421356237);
pA = [0, 0], pB = [5, 6];
assertFuzzyEquals(distance2D(pA, pB), 7.81024967591);
pA = [-5, 4], pB = [-10, 6];
assertFuzzyEquals(distance2D(pA, pB), 5.38516480713);
pA = [-5.1, 4.0], pB = [10.2, -6.3];
assertFuzzyEquals(distance2D(pA, pB), 18.4439692041);
});
});
describe("Basic Tests for 3D Cases", function(){
it("Low Values of x, y, z", function(){
var pA = [1, 1, 1], pB = [2, 2, 2];
assertFuzzyEquals(distance3D(pA, pB), 1.73205080757);
pA = [0, 0, 0], pB = [7, 8, 9];
assertFuzzyEquals(distance3D(pA, pB), 13.9283882772);
pA = [-5, 4, 7], pB = [-10, 6, -18];
assertFuzzyEquals(distance3D(pA, pB), 25.5734237051);
pA = [-5.1, 4.0, 1], pB = [10.2, -6.3, -1];
assertFuzzyEquals(distance3D(pA, pB), 18.5520888312);
});
});
describe ("Random Tests for 2D Cases", function(){
it ("Integer Values for x, y", function(){
for (var h = 0; h <= 20; h++) {
var pA = [randint(-100, 100), randint(-100,100)];
var pB = [randint(-100, 100), randint(-100,100)];
var result = distance2DCheck(pA, pB);
var res = distance2D(pA, pB);
it ("Testing for: pA: " + "[" + pA.toString() + "]" + ", pB: " + "[" + pB.toString() + "]"+ " , distance = " + result.toString(), function() {
assertFuzzyEquals(res, result);
});
}
});
});
describe ("Random Tests for 2D Cases", function(){
it ("Decimal Values for x, y", function(){
for (var h = 0; h <= 20; h++) {
var pA = [randint(-100, 100) + Math.random(), randint(-100,100) + Math.random()];
var pB = [randint(-100, 100) + Math.random(), randint(-100,100) + Math.random()];
var result = distance2DCheck(pA, pB);
var res = distance2D(pA, pB);
it ("Testing for: pA: " + "[" + pA.toString() + "]" + ", pB: " + "[" + pB.toString() + "]"+ " , distance = " + result.toString(), function() {
assertFuzzyEquals(res, result);
});
}
});
});
describe ("Random Tests for 3D Cases", function(){
it ("Integer Values for x, y, z", function(){
for (var h = 0; h <= 20; h++) {
var pA = [randint(-100, 100), randint(-100,100), randint(-100,100)];
var pB = [randint(-100, 100), randint(-100,100), randint(-100,100)];
var result = distance3DCheck(pA, pB);
var res = distance3D(pA, pB);
it ("Testing for: pA: " + "[" + pA.toString() + "]" + ", pB: " + "[" + pB.toString() + "]"+ " , distance = " + result.toString(), function() {
assertFuzzyEquals(res, result);
});
}
});
});
describe ("Random Tests for 3D Cases", function(){
it ("Decimal Values for x, y, z", function(){
for (var h = 0; h <= 20; h++) {
var pA = [randint(-100, 100) + Math.random(), randint(-100,100) + Math.random(), randint(-100,100) + Math.random()];
var pB = [randint(-100, 100) + Math.random() , randint(-100,100) + Math.random() , randint(-100,100) + Math.random()];
var result = distance3DCheck(pA, pB);
var res = distance3D(pA, pB);
it ("Testing for: pA: " + "[" + pA.toString() + "]" + ", pB: " + "[" + pB.toString() + "]"+ " , distance = " + result.toString(), function() {
assertFuzzyEquals(res, result);
});
}
});
});
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
def distance2D_check(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_check(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 assertFuzzyEquals(actual, expected)
merr = 1e-10
inrange = (actual - expected).abs <= merr
if (inrange == false) then
msg = "At 1e-10: Expected value must be %.10f but was %.10f" % [expected, actual]
end
return Test.expect(inrange, msg)
end
describe "Basic Tests for 2D Cases" do
it "Low Values of x, y" do
pA = [1, 1]; pB = [2, 2]
assertFuzzyEquals(distance2D(pA, pB), 1.41421356237)
pA = [0, 0]; pB = [5, 6]
assertFuzzyEquals(distance2D(pA, pB), 7.81024967591)
pA = [-5, 4]; pB = [-10, 6]
assertFuzzyEquals(distance2D(pA, pB), 5.38516480713)
pA = [-5.1, 4.0]; pB = [10.2, -6.3]
assertFuzzyEquals(distance2D(pA, pB), 18.4439692041)
end
end
describe "Basic Tests for 3D Cases" do
it "Low Values of x, y, z" do
pA = [1, 1, 1]; pB = [2, 2, 2]
assertFuzzyEquals(distance3D(pA, pB), 1.73205080757)
pA = [0, 0, 0]; pB = [7, 8, 9]
assertFuzzyEquals(distance3D(pA, pB), 13.9283882772)
pA = [-5, 4, 7]; pB = [-10, 6, -18]
assertFuzzyEquals(distance3D(pA, pB), 25.5734237051)
pA = [-5.1, 4.0, 1]; pB = [10.2, -6.3, -1]
assertFuzzyEquals(distance3D(pA, pB), 18.5520888312)
end
end
describe "Random Tests for 2D Cases" do
it "Integer Values for x, y" do
for h in 1..20
pA = [rand(-100..100), rand(-100..100)]
pB = [rand(-100..100), rand(-100..100)]
result = distance2D_check(pA, pB)
res = distance2D(pA, pB)
it "Testing for: pA: " + pA.to_s + ", pB: " + pB.to_s + " , distance = " + result.to_s do
assertFuzzyEquals(res, result)
end
end
end
end
describe "Random Tests for 2D Cases" do
it "Decimal Values for x, y" do
for h in 1..20
pA = [rand(-100..100) + range(0, 1) , rand(-100..100) + range(0, 1)]
pB = [rand(-100..100) + range(0, 1), rand(-100..100) + range(0, 1)]
result = distance2D_check(pA, pB)
res = distance2D(pA, pB)
it "Testing for: pA: " + pA.to_s + ", pB: " + pB.to_s + " , distance = " + result.to_s do
assertFuzzyEquals(res, result)
end
end
end
end
describe "Random Tests for 3D Cases" do
it "Integer Values for x, y, z" do
for h in 1..20
pA = [rand(-100..100), rand(-100..100), rand(-100..100)]
pB = [rand(-100..100), rand(-100..100), rand(-100..100)]
result = distance3D_check(pA, pB)
res = distance3D(pA, pB)
it "Testing for: pA: " + pA.to_s + ", pB: " + pB.to_s + " , distance = " + result.to_s do
assertFuzzyEquals(res, result)
end
end
end
end
describe "Random Tests for 3D Cases" do
it "Decimal Values for x, y" do
for h in 1..20
pA = [rand(-100..100) + range(0, 1) , rand(-100..100) + range(0, 1), rand(-100..100) + range(0, 1)]
pB = [rand(-100..100) + range(0, 1), rand(-100..100) + range(0, 1), rand(-100..100) + range(0, 1)]
result = distance3D_check(pA, pB)
res = distance3D(pA, pB)
it "Testing for: pA: " + pA.to_s + ", pB: " + pB.to_s + " , distance = " + result.to_s do
assertFuzzyEquals(res, result)
end
end
end
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)
def distance2D_check(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_check(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)
def assertFuzzyEquals(actual, expected, msg=""):
merr = 1e-10
inrange = abs(actual - expected) <= merr
if (inrange == False):
msg = "At 1e-10: Expected value must be {:.10f} but got {:.10f}"
msg = msg.format(expected, actual)
return Test.expect(inrange, msg)
test.describe("Basic Tests for 2D")
pA = [1, 1]; pB = [2, 2]
assertFuzzyEquals(distance2D(pA, pB), 1.41421356237)
pA = [0, 0]; pB = [5, 6]
assertFuzzyEquals(distance2D(pA, pB), 7.81024967591)
pA = [-5, 4]; pB = [-10, 6]
assertFuzzyEquals(distance2D(pA, pB), 5.38516480713)
pA = [-5.1, 4.0]; pB = [10.2, -6.3]
assertFuzzyEquals(distance2D(pA, pB), 18.4439692041)
test.describe("Basic Tests for 3D")
pA = [1, 1, 1]; pB = [2, 2, 2]
assertFuzzyEquals(distance3D(pA, pB), 1.73205080757)
pA = [0, 0, 0]; pB = [7, 8, 9]
assertFuzzyEquals(distance3D(pA, pB), 13.9283882772)
pA = [-5, 4, 7]; pB = [-10, 6, -18]
assertFuzzyEquals(distance3D(pA, pB), 25.5734237051)
pA = [-5.1, 4.0, 1]; pB = [10.2, -6.3, -1]
assertFuzzyEquals(distance3D(pA, pB), 18.5520888312)
test.describe("Random Tests for 2D")
test.describe("Integer coordinates")
import random
from random import randint
for h in range(20):
pA = [randint(-100, 100), randint(-100, 100)]
pB = [randint(-100, 100), randint(-100, 100)]
result = distance2D_check(pA, pB)
res = distance2D(pA, pB)
assertFuzzyEquals(res, result)
test.it("Distance from " + str(pA) + " to " + str(pB) + " : " + str(result))
test.describe("Random Tests for 2D")
test.describe("Decimal coordinates")
for h in range(20):
pA = [randint(-100, 100) + random.random(), randint(-100, 100) + random.random() ]
pB = [randint(-100, 100) + random.random() , randint(-100, 100) + random.random()]
result = distance2D_check(pA, pB)
res = distance2D(pA, pB)
assertFuzzyEquals(res, result)
test.it("Distance from " + str(pA) + " to " + str(pB) + " : " + str(result))
test.describe("Random Tests for 3D")
test.describe("Integer coordinates")
for h in range(20):
pA = [randint(-100, 100), randint(-100, 100), randint(-100, 100)]
pB = [randint(-100, 100), randint(-100, 100), randint(-100, 100)]
result = distance3D_check(pA, pB)
res = distance3D(pA, pB)
assertFuzzyEquals(res, result)
test.it("Distance from " + str(pA) + " to " + str(pB) + " : " + str(result))
test.describe("Random Tests for 3D")
test.describe("Decimal coordinates")
for h in range(10):
pA = [randint(-100, 100) + random.random(), randint(-100, 100) + random.random(), randint(-100, 100) + random.random() ]
pB = [randint(-100, 100) + random.random() , randint(-100, 100) + random.random(), randint(-100, 100) + random.random()]
result = distance3D_check(pA, pB)
res = distance3D(pA, pB)
assertFuzzyEquals(res, result)
test.it("Distance from " + str(pA) + " to " + str(pB) + " : " + str(result))
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
def prime_fac_check(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
describe "Basic Tests" do
it "Low values of n" do
Test.assert_equals(prime_fac(100), [2, 2, 5, 5])
Test.assert_equals(prime_fac(284), [ 2, 2, 71 ])
Test.assert_equals(prime_fac(498), [ 2, 3, 83 ])
end
end
describe "Random Tests" do
it "Values of from 1000000 up to 10000000000" do
for h in 1..100
n = rand(1000000..10000000000)
result = prime_fac_check(n)
res = prime_fac(n)
it "Testing for n = " + n.to_s do
Test.assert_equals(res, result)
end
end
end
end