“Primes Pytyhon” Code-Antworten

ist Prime Python

import math
def isPrimeNumber(n):
    if (n < 2):
        return False;
    sq = int(math.sqrt(n))
    for i in range(2, sq + 1):
        if (n % i == 0):
            return False
    return True
Sore Stork

Primzahlen in Python

from math import sqrt
for i in range(2, int(sqrt(num)) + 1):
    if num % i == 0:
        print("Not Prime")
        break
    print("Prime")

# Note: Use this if your num is big (ex. 10000 or bigger) for efficiency
# The result is still the same if the num is smaller
Old-fashioned Ostrich

Primes Pytyhon

def is_prime(n):
    if n in (2, 3):
        return True
    if n <= 1 or not (n%6==1 or n%6==5):
        return False
    a, b= 5, 2
    while a <= n**0.5:
        if not n%a:
            return False
        a, b = a+b, 6-b
    return True
# this method is much faster than checking every number because it uses the fact
# that every prime is either 1 above or 1 below a multiple of 6
# and that if a number has no prime factors, it has no factors at all
fmoeran

ist Prime Python

def is_prime(n):
  return bool(n&1)
Mouad En-naciry

Ähnliche Antworten wie “Primes Pytyhon”

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen