Complete-Python-3-Bootcamp icon indicating copy to clipboard operation
Complete-Python-3-Bootcamp copied to clipboard

trouble with code to write function to count number of primes up to and including input value

Open LeeZee1 opened this issue 6 years ago • 2 comments

Can someone please help me with the following code to write a function that returns the count of prime numbers up to and including the input number?

def count_primes(num):
    start = 2
    end = 1000
    count=0
    for num in range(start, end + 1):   
        if num > 1 and num%num==0 and num%1==0:
            for x in range(2, num): 
                for x<num:
                    if num%x==0:
                        not count+=1
                    elif num%x!=0:
                        count+=1
    return count

It returns nothing with: Check count_primes(100)

LeeZee1 avatar Mar 20 '20 19:03 LeeZee1

def fact(x): if x == 0: return 1 return x * fact(x-1)

def count_primes(y): primes = [] for i in range(2, y + 1): if (fact(i-1) +1) % i == 0: #look at Wilson's theorem if it does not make sense primes.append(i) return len(primes)

print(count_primes(100))

  • try this one. It has two functions though.

0xYoki avatar Nov 18 '20 15:11 0xYoki

Not sure if this helps but:


def PrimeCount(a):
    primes = []
    for possiblePrime in range(3,a+1,2):
        Prime = True
        for num in range(2,int(possiblePrime**0.5)+1):
            if possiblePrime%num == 0:
                Prime = False
                break
        if Prime:
            primes.append(possiblePrime)
    return len(primes)

kinsky-main avatar Nov 18 '20 15:11 kinsky-main