Answer:
num = 1
i = 1
while (num <= 100):
print (num)
num += i
i += 1
if num % 3 == 0:
print ("Got one!")
*** Sample Input ***
*** Sample Output ***
1
2
4
7
11
16
22
29
37
46
56
67
79
92
Step-by-step explanation:
For this problem, we will simply devise the algorithm necessary to create the desired number sequence, and then program the output using a while loop.
First, let's find the pattern that creates this sequence.
The first term is 1. The second term is 2. The third term is 4. The fourth term is 7. The fifth term is 11.
We can observe to get from the first to the second, we add 1.
We can observe to get from the second to the third, we add 2.
We can observe to get from the third to the fourth, we add 3.
We can observe to get from the fourth to the fifth, we add 4.
Notice, that as we move forward in the terms of the sequence, the gap between values is increasing by 1. This means, that as our index increase, we should add that value to the previous term to get the new term at the next index. For example, to get from 1 to 2, the index at 1 is 0 and the index at 2 is 1. Hence, to get from 1 to 2, we simply will add the value from index 1, to the desired index. Therefore, we will get 1 + 1 = 2.
With this in mind, let's build this in code and explain the purpose of each line (Note there are more efficient ways to accomplish this, but these lines are used for simplicity of explanation).
Line (1) num = 1
Here we are initializing the value of our number in the sequence. In this case, this is our first value of the sequence, 1.
Line (2) i = 1
Here we are initializing the value of our index in the sequence. In this case, we are ignoring the initial index 0, and starting with index 1. This is because of our observation that the base case to get from 1 to 2, is a shift to index 1.
Line (3) while (num <= 100):
Here we create the while loop to check for the condition if our num in the sequence is less than 100. If it is true, then the while loop executes. If it is not, then the while loop exits.
Line (4) print (num)
Here we print out the number in the sequence. On the first iteration of our loop, our algorithm hasn't been applied yet so we will simply print out the initialized value 1, which is also the zero index, or the first value in our sequence.
Line (5) num += i
Here we are applying our algorithm to add the index of the next term in the sequence to the existing value of the number we have from the previous index to get the value at the current index. Hence our base case, 1 + 1 = 2.
Line (6) i += 1
Here we are simply moving our index pointer to the right in the sequence. This allows us to be ready for the next iteration of our algorithm.
Line (7) if num % 3 == 0:
Here we are checking to see if our number at the current index is divisible by 3. This is accomplished using the modulo operator, %.
Line (8) print ("Got one!")
Here we are printing to the console the string "Got one!" if the value at our current index is indeed divisible by 3.
I hope that this helps you understand the process by which to accomplish the problem that has been asked.
Cheers.