Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading