347,824 views
36 votes
36 votes
MyProgramming Lab

It needs to be as simple as possible. Each question is slightly different.

1. An arithmetic progression is a sequence of numbers in which the distance (or difference) between any two successive numbers is the same. This in the sequence 1, 3, 5, 7, ..., the distance is 2 while in the sequence 6, 12, 18, 24, ..., the distance is 6.

Given the positive integer distance and the non-negative integer n, create a list consisting of the arithmetic progression between (and including) 1 and n with a distance of distance. For example, if distance is 2 and n is 8, the list would be [1, 3, 5, 7].

Associate the list with the variable arith_prog.

2.

An arithmetic progression is a sequence of numbers in which the distance (or difference) between any two successive numbers if the same. This in the sequence 1, 3, 5, 7, ..., the distance is 2 while in the sequence 6, 12, 18, 24, ..., the distance is 6.

Given the positive integer distance and the integers m and n, create a list consisting of the arithmetic progression between (and including) m and n with a distance of distance (if m > n, the list should be empty.) For example, if distance is 2, m is 5, and n is 12, the list would be [5, 7, 9, 11].

Associate the list with the variable arith_prog.

3.

A geometric progression is a sequence of numbers in which each value (after the first) is obtained by multiplying the previous value in the sequence by a fixed value called the common ratio. For example the sequence 3, 12, 48, 192, ... is a geometric progression in which the common ratio is 4.

Given the positive integer ratio greater than 1, and the non-negative integer n, create a list consisting of the geometric progression of numbers between (and including) 1 and n with a common ratio of ratio. For example, if ratio is 2 and n is 8, the list would be [1, 2, 4, 8].

Associate the list with the variable geom_prog.

User Egbokul
by
2.9k points

1 Answer

24 votes
24 votes

Answer:

The program in Python is as follows:

#1

d = int(input("distance: "))

n = int(input("n: "))

arith_prog = []

for i in range(1,n+1,d):

arith_prog.append(i)

print(arith_prog)

#2

d = int(input("distance: "))

m = int(input("m: "))

n = int(input("n: "))

arith_prog = []

for i in range(m,n+1,d):

arith_prog.append(i)

print(arith_prog)

#3

r = int(input("ratio: "))

n = int(input("n: "))

geom_prog = []

m = 1

count = 0

while(m<n):

m = r**count

geom_prog.append(m)

count+=1

print(geom_prog)

Step-by-step explanation:

#Program 1 begins here

This gets input for distance

d = int(input("distance: "))

This gets input for n

n = int(input("n: "))

This initializes the list, arith_prog

arith_prog = []

This iterates from 1 to n (inclusive) with an increment of d

for i in range(1,n+1,d):

This appends the elements of the progression to the list

arith_prog.append(i)

This prints the list

print(arith_prog)

#Program 2 begins here

This gets input for distance

d = int(input("distance: "))

This gets input for m

m = int(input("m: "))

This gets input for n

n = int(input("n: "))

This initializes the list, arith_prog

arith_prog = []

This iterates from m to n (inclusive) with an increment of d

for i in range(m,n+1,d):

This appends the elements of the progression to the list

arith_prog.append(i)

This prints the list

print(arith_prog)

#Program 3 begins here

This gets input for ratio

r = int(input("ratio: "))

This gets input for n

n = int(input("n: "))

This initializes the list, geom_prog

geom_prog = []

This initializes the element of the progression to 1

m = 1

Initialize count to 0

count = 0

This is repeated until the progression element is n

while(m<n):

Generate progression element

m = r**count

Append element to list

geom_prog.append(m)

Increase count by 1

count+=1

This prints the list

print(geom_prog)

User NPn
by
2.8k points