Calculate sequences with interactive demo and VB code examples
Geometric progression (GP) is a sequence of numbers where each subsequent term is found by multiplying the previous term by a fixed number. This fixed number is called the common ratio (r).
nth term = a × r(n-1)
Where a = first term, r = common ratio
2, 4, 8, 16, 32... (a=2, r=2)
81, 27, 9, 3... (a=81, r=1/3)
Common ratio cannot be zero
Can be positive or negative
Enter the values below to generate a geometric progression:
Below are Visual Basic implementations for generating geometric progressions:
Private Sub cmd_compute_Click()
Dim x, n, num As Integer
Dim r As Single
' Get values from text boxes
x = Txt_FirstNum.Text
r = Txt_CR
num = Txt_Terms.Text
' Clear list and add headers
List1.Clear
List1.AddItem "n" & vbTab & "x"
List1.AddItem "___________"
' Generate geometric progression
n = 1
Do
' Calculate next term
x = x * r
' Add to list
List1.AddItem n & vbTab & x
' Increment term counter
n = n + 1
Loop Until n = num + 1
End Sub
Do...Loop Until for iterationAddItemPrivate Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Dim firstTerm As Double
Dim commonRatio As Double
Dim numTerms As Integer
Dim results As New List(Of String)
' Validate inputs
If Not Double.TryParse(txtFirstTerm.Text, firstTerm) Then
MessageBox.Show("Please enter a valid number for first term")
Return
End If
If Not Double.TryParse(txtCommonRatio.Text, commonRatio) Then
MessageBox.Show("Please enter a valid number for common ratio")
Return
End If
If Not Integer.TryParse(txtNumTerms.Text, numTerms) OrElse numTerms < 1 Then
MessageBox.Show("Please enter a positive integer for number of terms")
Return
End If
' Clear previous results
lstResults.Items.Clear()
lstResults.Items.Add("Term".PadRight(10) & "Value")
lstResults.Items.Add("".PadRight(20, "-"))
' Calculate and add terms
Dim currentTerm = firstTerm
For termIndex = 1 To numTerms
lstResults.Items.Add($"{termIndex.ToString().PadRight(10)}{currentTerm}")
currentTerm *= commonRatio
Next
End Sub
function calculateGP() {
// Get input values
const firstTerm = parseFloat(document.getElementById('firstTerm').value);
const commonRatio = parseFloat(document.getElementById('commonRatio').value);
const numTerms = parseInt(document.getElementById('numTerms').value);
// Validate inputs
if (isNaN(firstTerm) {
alert('Please enter a valid number for first term');
return;
}
if (isNaN(commonRatio)) {
alert('Please enter a valid number for common ratio');
return;
}
if (isNaN(numTerms) || numTerms < 1 || numTerms > 50) {
alert('Please enter a valid number of terms (1-50)');
return;
}
// Generate results
let currentTerm = firstTerm;
let resultsHTML = '';
let sequence = [];
for (let n = 1; n <= numTerms; n++) {
// Format calculation display
const calcDisplay = n === 1 ?
`${firstTerm} × ${commonRatio}0` :
`${firstTerm} × ${commonRatio}${n-1}`;
resultsHTML += `<tr>
<td>${n}</td>
<td>${currentTerm.toFixed(4)}</td>
<td>${calcDisplay}</td>
</tr>`;
sequence.push(currentTerm.toFixed(4));
currentTerm *= commonRatio;
}
// Display results
document.getElementById('gpResults').innerHTML = resultsHTML;
document.getElementById('sequenceSummary').innerHTML = sequence.join(', ');
document.getElementById('resultContainer').style.display = 'block';
}
| First Term | Ratio | Sequence |
|---|---|---|
| 3 | 2 | 3, 6, 12, 24, 48... |
| 100 | 0.5 | 100, 50, 25, 12.5, 6.25... |
| 4 | -2 | 4, -8, 16, -32, 64... |
| 1 | 10 | 1, 10, 100, 1000, 10000... |