You are on page 1of 1

Link to the exposition: ​https://youtu.

be/8S603d65WXw 
 
Visual aids used: 

To tackle this problem, this method makes use of 2 for loops, one nested. 
 
The formula 
 
n*(n + 1)/2 
 
gives the total spaces needed 
 
n = 1  n = 2  n = 3  n = 4 

spaces = 1  spaces = 3  spaces = 6  spaces = 10 

[1]  [1, 1, 2]  [1, 1, 2, 1, 2, 3]  [1, 1, 2, 1, 2, 3, 1, 2, 3, 4] 


​0  0 ​1​ 2  0 1 2 ​ 3​ 4 5  0 1 2 3 4 5 ​6​ 7 8 9 
 
If you look carefully, we can also infer at which index we need to stop in order 
to restart the sequence (the number of spaces is the index in which the 
sequence restarts for the next value of n). 

Knowing how, we’ll use the initial for loop to calculate our limit and the nested 
one to write in the array. Thus, the algorithm works as next: 
 
0) Initializes required local variables 
1) Sets the value of our variable that needs to be assigned to 1. 
2) Calculates the index where we must stop the sequence (limit) 
3) Assigns to the array the value according to the sequence in the position 
referenced by the super for loop index  
4) Increases the value that will be stored (since the sequence has 
consecutive numbers) 
5) Increases the subindex reference by one to move forward through the 
array.  
If the actual referencing index (index + subindex) is minor than the 
index were the sequence must repeat, repeats 3 and 4.  
If not, follows next step. 
6) Increases the index modifier to calculate the next limit. 
7) Assigns to the index the value of the limit to write in the array from that 
index reference. 
8) Repeats 1 - 7 until the modifier is greater than the parameter (the 
sequence is completed). 

You might also like