2

I have a basic understanding of how to calculate Time Complexities, but I'm not sure how to calculate it in this case due to the random nature of primes.

A quick explanation --> Essentially, I'm keeping a running count of the remainders so that I know when the next prime is.

My code:

import math

n = int(input("Enter the number:\t"))

primeList = []
checkList = []

number = 3
isPrime = True
while number <= math.sqrt(n) and isPrime:

    isChanged = False
    for i, checkNum in enumerate(checkList):
        if checkNum == 1:
            isChanged = True
            checkList[i] = primeList[i]
        else:
            checkList[i] = checkNum - 1

    if not isChanged:

        primeList.append(number)
        checkList.append(number)

        if n % number == 0:
            isPrime = False

    number += 2

if isPrime:
    print("Prime")
else:
    print("Not Prime")
Adi219
  • 4,712
  • 2
  • 20
  • 43
  • I see no reason, how the answer is influenced by the distribution of prime numbers, the complexity is O(sqrt(n)), and yes, there are better algorithms, especially if you ony want to know, whether the number is prime or not and are not interested at the factors. – guidot Aug 01 '18 at 11:38
  • @guidot The inner loop makes it worse, I would say by a factor corresponding to the number of primes less than `sqrt(n)` (which can be estimated by the Prime Number Theorem). – John Coleman Aug 01 '18 at 11:45
  • @guidot Sorry, that's my fault (I posted a previous version of my code). In my current version (which isn't working), I catch every continuous prime in one go, so instead of catching a maximum of one prime per iteration, it catches between 0-5. So 13, 15, 17, 19 would be caught in one iteration, but in the next one, only 23 would, but then 29 and 31 would be caught. So what would the time complexity of that be? – Adi219 Aug 01 '18 at 12:04
  • π(N) ~ N/log(N) – Veltzer Doron Aug 01 '18 at 12:13
  • BTW, I ran your code in a loop and plotted the timing as a function of n in range(1,10^9), it came out linear for some reason. – Veltzer Doron Aug 01 '18 at 12:23
  • You mentioned *the random nature of primes.* What, specifically, are you referring to? I don't recall anything particularly random about prime numbers. – Jim Mischel Aug 01 '18 at 13:46
  • @VeltzerDoron if the complexity is `O(n/log(n))` it will look like `O(n)` on a smaller scale. `log(n)` grows so slowly that it is hard to find a scale on which the slope of the curve is visibly changing. – John Coleman Aug 01 '18 at 14:20
  • Everyone is answering the complexity in terms of the input `n`. Note that the way complexity of primality is normally given is in terms of the **size** of `n` (e.g. in bits). So if you want to compare to anything in the literature, it is exponential. – DanaJ Aug 20 '18 at 21:31

1 Answers1

4

Your algorithm seems to be O(n/log(n))

There are sqrt(n) passes through the outer loop. The inner loop is bounded by the number of primes which are less than sqrt(n). By the Prime Number Theorem this is asymptotically given by sqrt(n)/log(sqrt(n)). By the laws of logarithms this is equivalent to sqrt(n)/(0.5*log(n)) = 2*sqrt(n)/log(n). The overall complexity is thus

O(sqrt(n)*2*sqrt(n)/log(n)) = O(2*n/log(n)) = O(n/log(n))

Needless to say, this isn't a very efficient way to check if n is prime. It is asymptotically little better than the O(n) naive check for divisibility by all numbers less than n.

John Coleman
  • 51,337
  • 7
  • 54
  • 119