125k views
5 votes
Consider the following algorithm:

Step 1: Start with a list of the positive integers from 2 to 100, inclusive (meaning you include 2 and 100 in your list).

Step 2: For each $n$ from 2 to 50, inclusive, do the following: Remove all multiples of $n$ greater than $n$ from the list.

Step 3: Output all of the numbers remaining in the list.

What is the tenth number in the list that gets output?

1 Answer

3 votes

Answer:

The tenth number in the list that gets output is 29

Step-by-step explanation:

To get this done, the algorithm has to be implemented using a programming language;

The algorithm is translated to Python Programming Language as follows (Comment explains difficult lines);

#Declare an empty list

mylist = []

#Input n = 2 to 100 to list

for n in range(2,101):

mylist.append(n)

#Iterate from 2 to 50

for n in range(2,51):

#Initialize first multiple of n

k = 0

#Iterate multiples of n

for j in range(n,51,n):

k = k + 1

#Check if item exist

if j in mylist:

#Print multiples of n greater than n

if not k == 1:

mylist.remove(j)

#Print All List

print("List: ",mylist)

#Print 10th element of the list

print("10th Element: ",mylist[9])

After running the program, the output is as follows:

List: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]

10th Element: 29