Search code examples
pythonpython-3.xfunctionprimes

Python implementing a function to return a list where y[i] is the number of prime numbers <= to i


c) Using your function in Part (b), implement the function primesCount(n), which given a integer n, returns the length-n list y, given by y[i] = the number of prime numbers less than or equal to i, for i = 0, 1, . . . , n − 1. Sample Input/Output: Enter a number: 6 [0, 0, 1, 2, 2, 3]

Part (b) function:

def generatePrimes(n):

    prime = [False for i in range(n)]
    I = []
    for i in range(2, n):
       if i > 1:
           for j in range(2, int(math.sqrt(i))+1):
               if i % j == 0:
                   break
           else:
               I.append(i)
               prime[i] = True
    return prime

All of the code (part b + part c function):

import math

def generatePrimes(n):
    prime = [False for i in range(n)]  # creates a list of n-length all consisting of F elem
    I = []
    for i in range(2, n):
       if i > 1:
           for j in range(2, int(math.sqrt(i))+1):
               if i % j == 0:
                   break
           else:
               I.append(i)   # unecessary
               prime[i] = True #change False to True if number is prime
    return prime   # return the list (indexes with False are composite numbers while indexes 
                    with True are prime number

def primesCount(n):
    I = []
    for i in range(n):
        I.append(generatePrimes(i))
    return I
   
n = int(input("Enter a number: "))
print(primesCount(n))

expected input/output:

input: Enter a number: 6 

output: [0, 0, 1, 2, 2, 3]

actual input/output:

input: Enter a number: 6 

output: [[], [False], [False, False], [False, False, True],[False, False, True, True]]

What I need is to convert the False and True to integers so for example False + False + False + True + True = 0 + 0 + 1 + 1 = 2

I would like to convert the above output to [0, 0, 1, 2, 2, 3] ( [False] = 0, [False + False] = 0, [False, False, True] = 1...)

Been scratching my head for hours on this can't seem to get it working


Solution

  • I have updated your code. Please look into it

    import math
    
    def generatePrimes(n):
        prime = [False for i in range(n)]  # creates a list of n-length all consisting of F elem
        I = []
         
        for i in range(2, n):
           if i > 1:
               for j in range(2, int(math.sqrt(i))+1):
                   if i % j == 0:
                       break
               else:
                   I.append(i)   # unecessary
                   prime[i] = True #change False to True if number is prime
        return prime   # return the list (indexes with False are composite numbers while indexes 
                        #with True are prime number
    
    def primesCount(n):
        I = []
        for i in range(n+1):
            if len(generatePrimes(i)):
                I.append(sum(generatePrimes(i)))
        return I
       
    n = int(input("Enter a number: "))
    print(primesCount(n))