Ad
Mathematics
Algorithms
Logic
Numbers
Code
Diff
  • """
    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
    
    • """
    • Simple and fast, around 2-3ms
    • """
    • # One line removed, one is added with wit, you really should git gud < Haiku >
    • # One line removed, one is edited with wit but actually works, you really should git gud < idk >
    • def prime_checker(n):
    • if n == 2 and n%2 != 0 and n > 2: return True
    • for i in range(3, int(n**.5)+1, 2):
    • for i in range(3, int(n**.5)+1 if n > 0 else 10, 2):
    • if n%i == 0: return False
    • return True
    • return (n > 2 and n%2 != 0) or n == 2
Mathematics
Algorithms
Logic
Numbers
Code
Diff
  • """
    Simple and fast, around 2-3ms
    """
    def prime_checker(n):
        if n == 2: return True
        if n%2 == 0 or n < 2: return False
        for i in range(3, int(n**.5)+1, 2):
            if n%i == 0: 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))
    • Simple and fast, around 2-3ms
    • """
    • 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
    • if n == 2: return True
    • if n%2 == 0 or n < 2: return False
    • for i in range(3, int(n**.5)+1, 2):
    • if n%i == 0: return False
    • return True
Code
Diff
  • digit_sum = lambda n: sum(map(int, str(abs(n))))
    • def digit_sum(n: int) -> int:
    • return sum(map(int, str(abs(n))))
    • digit_sum = lambda n: sum(map(int, str(abs(n))))