Ad
Regular Expressions
Declarative Programming
Advanced Language Features
Programming Paradigms
Fundamentals
Strings
Code
Diff
  • class StringParity {
        public static boolean isEvenLength(String str) {
          return (str.length() & 1) == 0;
        }
    }
    • class StringParity {
    • public static boolean isEvenLength(String str) {
    • return str.length() % 2 == 0;
    • }
    • public static boolean isEvenLength(String str) {
    • return (str.length() & 1) == 0;
    • }
    • }
Mathematics
Algorithms
Logic
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
    • """
    • Simple and fast, around 2-3ms
    • """
    • # One line removed, one is edited with wit but actually works, you really should git gud < idk >
    • def prime_checker(n):
    • for i in range(3, int(n**.5)+1 if n > 0 else 10, 2):
    • if n%i == 0: return False
    • return (n > 2 and n%2 != 0) or n == 2
    • 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
Mathematics
Algorithms
Logic
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
    
    • """
    • 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 (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):
    • 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
Mathematics
Algorithms
Logic
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
    • """
    • 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
    • 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:
    • elif n % 2 == 0 or n % 3 == 0 or n % 5 == 0 or n <= 1:
    • return False
    • a = int(n ** 0.5 / 30)
    • sqrt_n = 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]:
    • 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
    • return True
Mathematics
Algorithms
Logic
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
    • import math
    • def prime_checker(n):
    • if n == 1:
    • if n < 4:
    • return n > 1
    • if n % 2 == 0 or n % 3 == 0:
    • return False
    • max_divisor = math.floor(math.sqrt(n))
    • if n % 2 == 0:
    • return False
    • if n % 5 == 0 and n > 5:
    • return False
    • for d in range(3, 1 + max_divisor, 2):
    • if n % d == 0:
    • 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