You made it to the end of week 1! This quick test is made to test your basic understanding of everything you've learnt in the past 6 days.

• You can look at the syntax, bonus points if you don't.
• The test should ideally not take more than 20 minutes to solve.
• All the criterias must be met.
• The answer and complete breakdown of the program is available at the end of this page.

## The Test

### Pre-requisite

The Fibonacci Sequence is a series of numbers where every number is the sum of 2 numbers before it. The sequence starts with numbers 0, 1 and following numbers are the sum of their 2 previous numbers. You can read more on Fibonacci Sequence on Wikipedia.

### Question

Write a class `NumberGenerator` that takes two `static` methods named `fibonacci`.

1. The first method should take an `Integer` and generate that many numbers in the fibonacci sequence in a single line. For example, if the input integer is `5`, it should generate `0, 1, 1, 2, 3`.
2. The second method should take 2 `Integer` values. The first input defines where to start the sequence from, and the second input defines how many numbers to generate in the fibonacci sequence in a single line. For example, if the input integer values are `5 , 5`, it should generate `1,5,6,11,17`. Hint: Use 1 as the other value when generating the sequence.

Start a timer and note how long you take to solve. Ideally, it should take ~20 minutes, but don't worry if you go overboard. This is your first time programming.

First, let's see how many variables we need. We have 3 numbers at hand before we add them in the final list. Number 1 and 2 that already exist and Number 3 that is the sum of Number 1 and 2. We repeat this `n` number of times. The reason why we don't ever need to know how many times we repeat the process is because the logic stays the same, `Number 1 + Number 2 = Number 3` and add Number 3 in the final list.

``````public class NumberGenerator{
public static List<Integer> fibonacci(Integer n){ //where n denotes how many numbers to generate.
List<Integer> finalList = new List<Integer>(); //This is out final list.
Integer a = 0, b = 1; //First two numbers are 0 and 1 in the sequence.
Integer c; //Empty integer to hold the sum.
}
}``````

Now to generate the sequence we need a for loop, that runs `n` times.

`````` for(Integer i = 0; i < n; i++){

}``````

We keep the exit condition to `i < n` beause we are starting counting from 0. Now to add the values in the list, we first add values of number 1 and 2 outside the loop and number 3 inside the loop. This is done because Number 1 and 2 always are constant in the List, while every number after that is added on the fly.

``````finalList.add(a);
for(Integer i = 0; i < n; i++){
c = a + b;
}``````

We also need to increment the value of a, b and c in the loop, so the previous values update too, allowing us to generate further numbers.

``````for(Integer i = 0; i < n; i++){
c = a + b;
a = b;
b = c;
}``````

Since we are adding the starting 2 numbers in the final list, we need to subtract `n` from it. Here is what the final method looks like:

``````public static List<Integer> fibonacci(Integer n){
List<Integer> returnList = new List<Integer>();

Integer a = 0, b = 1; //Initial numbers and place holders
Integer c; //new number place holder

for(Integer i = 0; i < n-2; i++){

c = a + b;
a = b;
b = c;
}

return returnList;
}``````

The second `fibonacci` method wouldn't require too many changes. Here's what's different between the two methods:

• The method would take two integers as inputs.
• ``public static List<Integer> fibonacci(Integer starter, Integer sizer){}``
• The value for Number 1 and Number 2 would be `1` and `starter` respectively.
• ``Integer a = 1, b = starter;``
• The `for` loop would update to using `sizer-2` as exit condition instead of `n-2`
• ``for(Integer i = 0; i < sizer-2; i++){` `

The final code would look something like this:

``````public static List<Integer> fibonacci(Integer starter, Integer sizer){
List<Integer> returnList = new List<Integer>();

Integer a = 1, b = starter; //Initial numbers and place holders
Integer c = 0; //new number place holder

for(Integer i = 0; i < sizer-2; i++){

c = a + b;
a = b;
b = c;
}

return returnList;

}``` ```

To execute this code, open an Anonymous Execution window `Control + E` and log the entry:

``````System.debug(NumberGenerator.fibonacci(5));
System.debug(NumberGenerator.fibonacci(6,5));``````

Congratulations! If you got your answers right, you have a clear understanding of basic programming concepts. It's okay to take more than ideal time to solve because as a first time programmer you will take time to form your solutions in code!