-4
 import math


def erathosthene(n):
    
    for d in range(2, int(math.sqrt(n) + 0.5)):
        while n % d != 0:
            d =+ 1
        if n % d == 0:
            print('this number isn\'t prime')
            return False
        else:
            print('this number is prime')
            return True

erathosthene(35)

it literally display nothing and honestly idk why it display nothing

there's nothing after return in terminal

swken999
  • 5
  • 5

1 Answers1

1

What you're doing is a primality test and the problems of your code are

  • let the for loop increase d, don't increase it by yourself too
  • if n is divisible by d then you end as it is not prime
  • if n is NOT divisible by the current d, that doesn't mean it is prime, you wait to wait
  • loop until sqrt(x)+1 not sqrt(x)+0.5, because for
  • add condition for numbers < 2
def erathosthene(n):
    if n < 2:
        return False
    for d in range(2, int(math.sqrt(n) + 1)):
        if n % d == 0:
            return False
    return True

Then call with a print so your code displays something

print(erathosthene(6))

For better implementation, you can see isPrime Function for Python Language for example

azro
  • 53,056
  • 7
  • 34
  • 70