#C4256. Series Generation Challenge
Series Generation Challenge
Series Generation Challenge
In this problem, you are required to generate a numerical series based on a specified pattern. Four patterns are supported:
-
Arithmetic: The series starts at a specified number and increments by 1 for each subsequent element. Mathematically, the nth term is given by (a_n = a_0 + n), where (a_0) is the starting number.
-
Fibonacci: The series begins with the given starting number and the next consecutive number. Every subsequent element is the sum of the previous two. That is, (a_0 = \text{start}), (a_1 = \text{start}+1) and (a_n = a_{n-1} + a_{n-2}) for (n \ge 2).
-
Geometric: The series starts from the initial number and each next element is twice the previous one. In formula, (a_n = a_0 \times 2^n).
-
Square: The series consists of squares of consecutive numbers starting from the given number. That is, (a_n = (\text{start}+n)^2).
Your program should read the input from stdin
, generate the corresponding series based on the provided pattern, and output the series to stdout
as space-separated integers.
inputFormat
The input is provided as a single line containing three items separated by spaces:
- An integer length (the number of elements in the series).
- An integer start (the starting number of the series).
- A string pattern which will be one of: "arithmetic", "fibonacci", "geometric", or "square".
Example: 5 2 arithmetic
outputFormat
Output the generated series as a single line of space-separated integers. If the length is 0 or negative, output an empty line.## sample
5 2 arithmetic
2 3 4 5 6