Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can anyone help? Python programming 1-> Use list comprehension to create a list

ID: 3712287 • Letter: C

Question

Can anyone help? Python programming

1-> Use list comprehension to create a list of the next 20 primes.

2-> Use islice() to create a list of the first 20 primes larger than 1 million.

3-> Use a generator expression to create an iterator that would produce the first 100 primes larger than 1 million, then use a for loop and this iterator to print the values.

4-> Use the above iterator to create a list of the first 100 primes larger than 1 million for which the last two digits are identical.

5-> Create a Primes iterator class equivalent to your generator in (2) and use it with islice() to create a list of the first 20 primes larger than 1 million.

Explanation / Answer

1. pr = [2]

[pr.append(i) for i in xrange(3,num+1) if not [x for x in pr if i%x==0]]

print(pr)

2. import itertools as it

def erat2a( ):

D = { }

yield 2

for q in it.islice(it.count(3), 0, None, 2):

p = D.pop(q, None)

if p is None:

D[q*q] = q

yield q

else:

# old code here:

# x = p + q

# while x in D or not (x&1):

# x += p

# changed into:

x = q + 2*p

while x in D:

x += 2*p

D[x] = p

3. def genprimes(limit): # derived from

# Code by David Eppstein, UC Irvine, 28 Feb 2002

D = {} # http://code.activestate.com/recipes/117119/

q = 2

while q <= limit:

if q not in D:

yield q

D[q * q] = [q]

else:

for p in D[q]:

D.setdefault(p + q, []).append(p)

del D[q]

q += 1

p = genprimes(100)

prms = [i for i in p]

print prms

4. import math

def main():

count = 3

one = 1

while 1:

for x in range(2, int(math.sqrt(count) + 1)):

if count % x == 0:

continue

if count % x != 0:

print count

count += 1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote