Regular Expressions
Declarative Programming
Advanced Language Features
Fundamentals
Strings
Code
Diff
  • class StringParity {
        public static boolean isEvenLength(String str) {
          return (str.length() & 1) == 0;
        }
    }
  • 11
    class StringParity {
    
    2
      public static boolean isEvenLength(String str) {
    
    3
        return str.length() % 2 == 0;
    
    4
      }
    
    2+
        public static boolean isEvenLength(String str) {
    
    3+
          return (str.length() & 1) == 0;
    
    4+
        }
    
    55
    }
    
Mathematics
Algorithms
Numbers

Updated tests.
%timeit prime_checker(10000000019)
-> 13.4 ms ± 40.5 µs

Code
Diff
  • def prime_checker(n):
        if n < 4:
            return n > 1
        if not (n % 2 and n % 3):
            return False
        for i in range(5, int(n ** 0.5)+1, 6):
            if not ((n % i) and (n % (i + 2))):
                return False
        return True
  • 1
    """
    
    2
    Simple and fast, around 2-3ms
    
    3
    """
    
    4
    # One line removed, one is edited with wit but actually works, you really should git gud < idk >
    
    55
    def prime_checker(n):
    
    6
        for i in range(3, int(n**.5)+1 if n > 0 else 10, 2):
    
    7
            if n%i == 0: return False
    
    8
        return (n > 2 and n%2 != 0) or n == 2
    
    2+
        if n < 4:
    
    3+
            return n > 1
    
    4+
        if not (n % 2 and n % 3):
    
    5+
            return False
    
    6+
        for i in range(5, int(n ** 0.5)+1, 6):
    
    7+
            if not ((n % i) and (n % (i + 2))):
    
    8+
                return False
    
    9+
        return True
    
Mathematics
Algorithms
Numbers
Code
Diff
  • """
    https://en.wikipedia.org/wiki/Primality_test
    An alternative using primality mod 30 = 2 * 3 * 5 instead of 6 = 2 * 3
    -- Using generator instead of list (works faster)
    -- range(0, 30 * sqrt_n + 1, 30) instead of (30 * k for k in range(sqrt_n + 1))
    """
    def prime_checker(n):
        if n in [2, 3, 5]:
            return True
        elif n % 2 == 0 or n % 3 == 0 or n % 5 == 0 or n <= 1:
            return False
        
        sqrt_n = int(n ** 0.5 / 30)
        b = [7, 11, 13, 17, 19, 23, 29, 31]
        
        for i in range(0, 30 * sqrt_n + 1, 30):
            if any(n % (i + j) == 0 for j in b if i + j != n):
                return False
        return True
    
  • 11
    """
    
    22
    https://en.wikipedia.org/wiki/Primality_test
    
    33
    An alternative using primality mod 30 = 2 * 3 * 5 instead of 6 = 2 * 3
    
    44
    -- Using generator instead of list (works faster)
    
    5+
    -- range(0, 30 * sqrt_n + 1, 30) instead of (30 * k for k in range(sqrt_n + 1))
    
    55
    """
    
    66
    def prime_checker(n):
    
    77
        if n in [2, 3, 5]:
    
    88
            return True
    
    99
        elif n % 2 == 0 or n % 3 == 0 or n % 5 == 0 or n <= 1:
    
    1010
            return False
    
    1111
        
    
    1212
        sqrt_n = int(n ** 0.5 / 30)
    
    1313
        b = [7, 11, 13, 17, 19, 23, 29, 31]
    
    1414
        
    
    15
        for i in (30 * k for k in range(sqrt_n + 1)):
    
    16
            if any(n % (i + j) == 0 for j in b if i + j is not n):
    
    16+
        for i in range(0, 30 * sqrt_n + 1, 30):
    
    17+
            if any(n % (i + j) == 0 for j in b if i + j != n):
    
    1717
                return False
    
    1818
        return True
    
Mathematics
Algorithms
Numbers
Code
Diff
  • """
    https://en.wikipedia.org/wiki/Primality_test
    An alternative using primality mod 30 = 2 * 3 * 5 instead of 6 = 2 * 3
    -- Using generator instead of list (works faster)
    """
    def prime_checker(n):
        if n in [2, 3, 5]:
            return True
        elif n % 2 == 0 or n % 3 == 0 or n % 5 == 0 or n <= 1:
            return False
        
        sqrt_n = int(n ** 0.5 / 30)
        b = [7, 11, 13, 17, 19, 23, 29, 31]
        
        for i in (30 * k for k in range(sqrt_n + 1)):
            if any(n % (i + j) == 0 for j in b if i + j is not n):
                return False
        return True
  • 11
    """
    
    22
    https://en.wikipedia.org/wiki/Primality_test
    
    3
    This one has lesser tests or usage of % operator.
    
    4
    An alternative using primality mod 30 = 2 * 3 * 5 instead of 6 = 2 * 3 
    
    3+
    An alternative using primality mod 30 = 2 * 3 * 5 instead of 6 = 2 * 3
    
    4+
    -- Using generator instead of list (works faster)
    
    55
    """
    
    66
    def prime_checker(n):
    
    77
        if n in [2, 3, 5]:
    
    88
            return True
    
    9
        elif n % 2 == 0 or n % 3 == 0 or n % 5 == 0:
    
    9+
        elif n % 2 == 0 or n % 3 == 0 or n % 5 == 0 or n <= 1:
    
    1010
            return False
    
    1111
        
    
    12
        a = int(n ** 0.5 / 30)
    
    12+
        sqrt_n = int(n ** 0.5 / 30)
    
    1313
        b = [7, 11, 13, 17, 19, 23, 29, 31]
    
    1414
        
    
    15
        for i in [30 * j for j in range(a + 1)]:
    
    16
            if True in [n % (i + q) == 0 for q in b if i + q is not n]:
    
    15+
        for i in (30 * k for k in range(sqrt_n + 1)):
    
    16+
            if any(n % (i + j) == 0 for j in b if i + j is not n):
    
    1717
                return False
    
    18
        return True 
    
    18+
        return True
    
Mathematics
Algorithms
Numbers
Code
Diff
  • def prime_checker(n):
        if n < 4:
            return n > 1
        if n % 2 == 0 or n % 3 == 0:
            return False
        i = 5
        sqrt_n = int(n ** 0.5)
        while i <= sqrt_n:
            if n % i == 0 or n % (i + 2) == 0:
                return False
            i += 6
        return True
  • 1
    import math
    
    22
    def prime_checker(n):
    
    3
        if n == 1:
    
    2+
        if n < 4:
    
    3+
            return n > 1
    
    4+
        if n % 2 == 0 or n % 3 == 0:
    
    44
            return False
    
    5
        max_divisor = math.floor(math.sqrt(n))
    
    6
        if n % 2 == 0:
    
    7
            return False
    
    8
        if n % 5 == 0 and n > 5:
    
    9
            return False
    
    10
        for d in range(3, 1 + max_divisor, 2):
    
    11
            if n % d == 0:
    
    6+
        i = 5
    
    7+
        sqrt_n = int(n ** 0.5)
    
    8+
        while i <= sqrt_n:
    
    9+
            if n % i == 0 or n % (i + 2) == 0:
    
    1212
                return False
    
    11+
            i += 6
    
    1313
        return True