[RAILS] [Ruby] Arithmetic progression

[Ruby] Try to solve arithmetic progression

problem

Let's create a program that outputs the following sequence.

5 8 11 14 17 20 23 26 29 32

You can see that the first number is 5, and it increases by 3. Generally, this is called the arithmetic progression with the first term 5 and the tolerance 3.

Let's create a program that outputs such a sequence. Since the first term m is given and the tolerance n is given, create a program that outputs up to the 10th number separated by spaces.

Value to be entered

The input is given in the following format with the first term m and tolerance n separated by half-width spaces.

One line break is inserted at the end of the last line of the input value.

m n

Expected output

Output the arithmetic progression with the first term m and the tolerance n separated by spaces from the 1st to the 10th.

Input example 1

3 3

Output example 1

3 6 9 12 15 18 21 24 27 30

Input example 2

5 10

Output example 2

5 15 25 35 45 55 65 75 85 95

Input example 3

1 3

Output example 3

1 4 7 10 13 16 19 22 25 28

My answer

python


num = gets.chomp.split(" ").map(&:to_i)
x = num[0]
i = 1
array = []
while i <= 10
  array << x
  x = x + num[1]
  i += 1
end
print array.join(" ")

This time's point

Since the input value on the first line is separated by spaces, make an array like [3, 3] with split and map.

In the second line, specify a part of the array and assign it to the x variable.

By setting i = 1 on the 3rd line, it will be counted once.

The 4th line allows you to use the array outside the scope, and stores the iterative result in the array

Lines 5-9 repeat until i exceeds 10. x = num [0] is concatenated to array [] using the array operator <<. For while statements, don't forget the self-assignment operator i + = 1

You can open a space between arrays by using the join method on the last line with ("").

that's all!

Recommended Posts

[Ruby] Arithmetic progression
Ruby four arithmetic operations
Ruby learning 4
[Ruby] Array
Ruby basics
Ruby learning 5
Ruby basics
Refactoring Ruby
Ruby learning 3
Ruby setting 2
Ruby problem ⑦
Ruby learning 2
[Ruby] Block
Refactoring Ruby
ruby calculator
Ruby learning 6
Ruby settings 1
Refactoring Ruby
Ruby basics
Ruby memo
Ruby learning 1
Ruby Review 1
[Ruby] Module