Skip to main content
use mathjax formatting
Source Link

For the sake of argument, let's use your code above as our basic requirements. Let's say we have access to sympy and basic Python, but nothing else. As a baseline, let's consider a trivial prime number finder that use nothing but pure Python. We'll just count up from 3 up to sqrt(n)\$\sqrt{n}\$: it's naive, it's dead simple to write, and it's actually reasonably fast just because Python is a decent language (and it even has an O(sqrt(N))\$O(\sqrt{N})\$ runtime, which is not a bad place to start):

For the sake of argument, let's use your code above as our basic requirements. Let's say we have access to sympy and basic Python, but nothing else. As a baseline, let's consider a trivial prime number finder that use nothing but pure Python. We'll just count up from 3 up to sqrt(n): it's naive, it's dead simple to write, and it's actually reasonably fast just because Python is a decent language (and it even has an O(sqrt(N)) runtime, which is not a bad place to start):

For the sake of argument, let's use your code above as our basic requirements. Let's say we have access to sympy and basic Python, but nothing else. As a baseline, let's consider a trivial prime number finder that use nothing but pure Python. We'll just count up from 3 up to \$\sqrt{n}\$: it's naive, it's dead simple to write, and it's actually reasonably fast just because Python is a decent language (and it even has an \$O(\sqrt{N})\$ runtime, which is not a bad place to start):

added 44 characters in body
Source Link
scnerd
  • 2.1k
  • 7
  • 13
def naive_is_prime(n):
    if n <=== 2:
        return True
    if n < 2 or n % 2 == 0:
        return False

    # Consolidated code thanks to GZ0's comment
    return all(n % i != 0 for i in range(3, int(n ** 0.5 + 1), 2)) 
def naive_is_prime(n):
    if n <= 2 or n % 2 == 0:
        return False

    # Consolidated code thanks to GZ0's comment
    return all(n % i != 0 for i in range(3, int(n ** 0.5 + 1), 2)) 
def naive_is_prime(n):
    if n == 2:
        return True
    if n < 2 or n % 2 == 0:
        return False

    # Consolidated code thanks to GZ0's comment
    return all(n % i != 0 for i in range(3, int(n ** 0.5 + 1), 2)) 
deleted 20 characters in body
Source Link
scnerd
  • 2.1k
  • 7
  • 13
import math

def naive_is_prime(n):
    if n <= 2 or n % 2 == 0:
        return False

    for i in range(3,# int(math.ceil(math.sqrt(n))),Consolidated 2):
code thanks to GZ0's comment
    ifreturn all(n % i ==!= 0:
        for i in range(3, returnint(n False

** 0.5 + 1), return2)) True
import math

def naive_is_prime(n):
    if n <= 2 or n % 2 == 0:
        return False

    for i in range(3, int(math.ceil(math.sqrt(n))), 2):
        if n % i == 0:
            return False

    return True
def naive_is_prime(n):
    if n <= 2 or n % 2 == 0:
        return False

    # Consolidated code thanks to GZ0's comment
    return all(n % i != 0 for i in range(3, int(n ** 0.5 + 1), 2)) 
Source Link
scnerd
  • 2.1k
  • 7
  • 13
Loading