Ad
Mathematics
Algorithms
Logic
Numbers
Code
Diff
  • """
    https://en.wikipedia.org/wiki/Primality_test
    This one has lesser tests or usage of % operator.
    An alternative using primality mod 30 = 2 * 3 * 5 instead of 6 = 2 * 3 
    """
    def prime_checker(n):
        if n in [2, 3, 5]:
            return True
        elif n % 2 == 0 or n % 3 == 0 or n % 5 == 0:
            return False
        
        a = int(n ** 0.5 / 30)
        b = [7, 11, 13, 17, 19, 23, 29, 31]
        
        for i in [30 * j for j in range(a + 1)]:
            if True in [n % (i + q) == 0 for q in b if i + q is not n]:
                return False
        return True 
    • """
    • https://en.wikipedia.org/wiki/Primality_test
    • This one has lesser tests or usage of % operator.
    • An alternative using primality mod 30 = 2 * 3 * 5 instead of 6 = 2 * 3
    • """
    • def prime_checker(n):
    • if n in [2, 3, 5]:
    • return True
    • elif n % 2 == 0 or n % 3 == 0 or n % 5 == 0:
    • return False
    • a = int(n ** 0.5 / 30)
    • b = [7, 11, 13, 17, 19, 23, 29, 31]
    • for i in [30 * j for j in range(a)]:
    • if True in [n % (i + q) == 0 for q in b]:
    • for i in [30 * j for j in range(a + 1)]:
    • if True in [n % (i + q) == 0 for q in b if i + q is not n]:
    • return False
    • return True
Code
Diff
  • // Actually worse than the original solution involving so much mathematics 
    // but it can give you the nth group if you seek just that. 
    
    const groupCombinations = (
      arr2D,
      len = arr2D.length,
      mapToLengths = e => e.length,
      lenArr1D = Array.from(arr2D, mapToLengths),
      reduceToProduct = (acc, cV) => [acc[0] * cV].concat(acc),
      lenProducts = lenArr1D.reduceRight(reduceToProduct, [1]),
      mapToGroupElement = (i) => (__, _i) => arr2D[_i][((~~(i / lenProducts[_i + 1]) % lenArr1D[_i]))],
      mapToGroup = (_, i) => Array.from({length: len}, mapToGroupElement(i))
    ) => [lenProducts[0], Array.from({length: lenProducts[0]}, mapToGroup)];
    
    • // Actually worse than the original solution involving so much mathematics
    • // but it can give you the nth group if you seek just that.
    • const groupCombinations = (
    • arr2D,
    • len = arr2D.length,
    • mapToLengths = e => e.length,
    • lenArr1D = Array.from(arr2D, mapToLengths),
    • reduceToProduct = (acc, cV) => [acc[0] * cV].concat(acc),
    • lenProducts = lenArr1D.reduceRight(reduceToProduct, [1]),
    • mapToGroupElement = (i) => (__, _i) => arr2D[_i][((~~(i / lenProducts[_i + 1]) % lenProducts[_i]))],
    • mapToGroupElement = (i) => (__, _i) => arr2D[_i][((~~(i / lenProducts[_i + 1]) % lenArr1D[_i]))],
    • mapToGroup = (_, i) => Array.from({length: len}, mapToGroupElement(i))
    • ) => [lenProducts[0], Array.from({length: lenProducts[0]}, mapToGroup)];
Code
Diff
  • // Actually worse than the original solution involving so much mathematics 
    // but it can give you the nth group if you seek just that. 
    
    const groupCombinations = (
      arr2D,
      len = arr2D.length,
      mapToLengths = e => e.length,
      lenArr1D = Array.from(arr2D, mapToLengths),
      reduceToProduct = (acc, cV) => [acc[0] * cV].concat(acc),
      lenProducts = lenArr1D.reduceRight(reduceToProduct, [1]),
      mapToGroupElement = (i) => (__, _i) => arr2D[_i][((~~(i / lenProducts[_i + 1]) % lenProducts[_i]))],
      mapToGroup = (_, i) => Array.from({length: len}, mapToGroupElement(i))
    ) => [lenProducts[0], Array.from({length: lenProducts[0]}, mapToGroup)];
    
    • function groupCombinations(arr2D) {
    • let solution = [[]];
    • for (let arr1D of arr2D.reverse()) {
    • solution = arr1D.flatMap(x => solution.map(y => [x, ...y])); // O(N) cons
    • }
    • return [solution.length, solution];
    • }
    • // Actually worse than the original solution involving so much mathematics
    • // but it can give you the nth group if you seek just that.
    • const groupCombinations = (
    • arr2D,
    • len = arr2D.length,
    • mapToLengths = e => e.length,
    • lenArr1D = Array.from(arr2D, mapToLengths),
    • reduceToProduct = (acc, cV) => [acc[0] * cV].concat(acc),
    • lenProducts = lenArr1D.reduceRight(reduceToProduct, [1]),
    • mapToGroupElement = (i) => (__, _i) => arr2D[_i][((~~(i / lenProducts[_i + 1]) % lenProducts[_i]))],
    • mapToGroup = (_, i) => Array.from({length: len}, mapToGroupElement(i))
    • ) => [lenProducts[0], Array.from({length: lenProducts[0]}, mapToGroup)];
Code
Diff
  • const revstr = (
      str,
      reduceToRevString = (acc, cV) => cV + acc,
      resStr = [...str].reduce(reduceToRevString, '')
    ) => resStr;
    • function revstr(str) {
    • // i = str.length; newstr = "";
    • // while (i > 0) {
    • // newstr += str[i - 1]; i--;
    • // }
    • // return newstr;
    • return str.split('').reverse().join('');
    • }
    • const revstr = (
    • str,
    • reduceToRevString = (acc, cV) => cV + acc,
    • resStr = [...str].reduce(reduceToRevString, '')
    • ) => resStr;
Mathematics
Algorithms
Logic
Numbers
Code
Diff
  • """
    https://en.wikipedia.org/wiki/Primality_test
    This one has lesser tests or usage of % operator.
    An alternative using primality mod 30 = 2 * 3 * 5 instead of 6 = 2 * 3 
    """
    def prime_checker(n):
        if n in [2, 3, 5]:
            return True
        elif n % 2 == 0 or n % 3 == 0 or n % 5 == 0:
            return False
        
        a = int(n ** 0.5 / 30)
        b = [7, 11, 13, 17, 19, 23, 29, 31]
        
        for i in [30 * j for j in range(a)]:
            if True in [n % (i + q) == 0 for q in b]:
                return False
        return True
    • """
    • https://en.wikipedia.org/wiki/Primality_test
    • This one has lesser tests or usage of % operator
    • This one has lesser tests or usage of % operator.
    • An alternative using primality mod 30 = 2 * 3 * 5 instead of 6 = 2 * 3
    • """
    • def prime_checker(n):
    • if n in [2, 3, 5, 7]:
    • if n in [2, 3, 5]:
    • return True
    • elif n % 2 == 0 or n % 3 == 0:
    • elif n % 2 == 0 or n % 3 == 0 or n % 5 == 0:
    • return False
    • a = int(n ** 0.5 / 6)
    • a = int(n ** 0.5 / 30)
    • b = [7, 11, 13, 17, 19, 23, 29, 31]
    • for i in [5 + 6 * j for j in range(a)]:
    • if n % i == 0 or n % (i + 2) == 0:
    • for i in [30 * j for j in range(a)]:
    • if True in [n % (i + q) == 0 for q in b]:
    • return False
    • return True
Mathematics
Algorithms
Logic
Numbers
Code
Diff
  • """
    https://en.wikipedia.org/wiki/Primality_test
    This one has lesser tests or usage of % operator
    """
    def prime_checker(n):
        if n in [2, 3, 5, 7]:
            return True
        elif n % 2 == 0 or n % 3 == 0:
            return False
        
        a = int(n ** 0.5 / 6)
        
        for i in [5 + 6 * j for j in range(a)]:
            if n % i == 0 or n % (i + 2) == 0:
                return False
        return True
    • """
    • https://en.wikipedia.org/wiki/Primality_test
    • This one has lesser tests or usage of % operator
    • """
    • def prime_checker(n):
    • a = 0
    • for i in range(2,n):
    • if n == 2:
    • return True
    • break
    • elif n%i == 0:
    • if n in [2, 3, 5, 7]:
    • return True
    • elif n % 2 == 0 or n % 3 == 0:
    • return False
    • a = int(n ** 0.5 / 6)
    • for i in [5 + 6 * j for j in range(a)]:
    • if n % i == 0 or n % (i + 2) == 0:
    • return False
    • a += 1
    • break
    • if a == 0:
    • return True
    • return True