Mathematics
Algorithms
Logic
Numbers
""" 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 Truefor 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
import codewars_test as test from solution import prime_checker @test.describe("Example") def test_group(): @test.it("test case") def test_case(): test.assert_equals(prime_checker(653), True) test.assert_equals(prime_checker(654), False) test.assert_equals(prime_checker(5), True) test.assert_equals(prime_checker(777), False)# test.assert_equals(prime_checker(977), True) test.assert_equals(prime_checker(125), False)# test.assert_equals(prime_checker(997), True) test.assert_equals(prime_checker(709), True) test.assert_equals(prime_checker(15), False)# test.assert_equals(prime_checker(11), True) test.assert_equals(prime_checker(13), True) test.assert_equals(prime_checker(17), True) test.assert_equals(prime_checker(19), True) test.assert_equals(prime_checker(23), True) test.assert_equals(prime_checker(29), True) test.assert_equals(prime_checker(1021), True) test.assert_equals(prime_checker(39 * 41), False) test.assert_equals(prime_checker(553_105_253), True) test.assert_equals(prime_checker(982_451_653), True) test.assert_equals(prime_checker(-1), False) test.assert_equals(prime_checker(4), False) test.assert_equals(prime_checker(2), True)
- import codewars_test as test
- from solution import prime_checker
- @test.describe("Example")
- def test_group():
- @test.it("test case")
- def test_case():
- test.assert_equals(prime_checker(653), True)
- test.assert_equals(prime_checker(654), False)
- test.assert_equals(prime_checker(5), True)
- test.assert_equals(prime_checker(777), False)#
- test.assert_equals(prime_checker(977), True)
- test.assert_equals(prime_checker(125), False)#
- test.assert_equals(prime_checker(997), True)
- test.assert_equals(prime_checker(709), True)
- test.assert_equals(prime_checker(15), False)#
- test.assert_equals(prime_checker(11), True)
- test.assert_equals(prime_checker(13), True)
- test.assert_equals(prime_checker(17), True)
- test.assert_equals(prime_checker(19), True)
- test.assert_equals(prime_checker(23), True)
- test.assert_equals(prime_checker(29), True)
- test.assert_equals(prime_checker(1021), True)
- test.assert_equals(prime_checker(39 * 41), False)
- test.assert_equals(prime_checker(553_105_253), True)
- test.assert_equals(prime_checker(982_451_653), True)
- test.assert_equals(prime_checker(-1), False)
- test.assert_equals(prime_checker(4), False)
- test.assert_equals(prime_checker(2), True)
Mathematics
Algorithms
Logic
Numbers
""" 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_testAn 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 Trueelif n % 2 == 0 or n % 3 == 0 or n % 5 == 0 or n <= 1:return Falsesqrt_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