“Python ist erstklassig” Code-Antworten

ist Prime Python

def is_prime(n):
    for i in range(2,int(n**0.5)+1):
        if n%i==0:
            return False
    return True
Mouad En-naciry

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

Primzahl in Python

def prime_number(n):
    c = 0
    for x in range(2, n):
        if n % x == 0:
            c = c + 1
    return c


n = int(input("Enter a number = "))
if prime_number(n) == 0:
    print("Prime number.")
else:
    print("Not prime number.")
Excited Earthworm

Python Prime Check

def isPrime(n):
  if n<2:		#1, 0 and all negative numbers are not prime
    return False
  elif n==2:	#2 is prime but cannot be calculated with the formula below becuase of the range function
    return True
  else:
    for i in range(2, n):
      if (n % i) == 0:	#if you can precisely divide a number by another number, it is not prime
        return False
    return True			#if the progam dont return False and arrives here, it means it has checked all the numebrs smaller than n and nono of them divides n. So n is prime
RoD

Python ist erstklassig

from math import sqrt, floor;

def is_prime(num):
    if num < 2: return False;
    if num == 2: return True;
    if num % 2 == 0: return False;
    for i in range(3,floor(sqrt(num))+1,2):
        if num % i == 0: return False;
    return True;
Grumpy Gemsbok

Ähnliche Antworten wie “Python ist erstklassig”

Fragen ähnlich wie “Python ist erstklassig”

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen