AP and GP: The Mathematical Magic in the World of Databases and Applications

January 29, 2025 | by dbsnoop

Math

Mathematics: Yes, You Will Use This Stuff (A Lot)

Who hasn’t, in the middle of a math class, asked the classic question: “When am I ever going to use this stuff?” I’ve asked this myself several times, especially when the topic was logarithms, integrals, derivatives, and other “creatures” that seemed to have escaped directly from a Klingon laboratory. Back then, nothing made sense. But then life got its revenge.

If you work or plan to work with data science, big data, databases, or programming, let me tell you a secret: mathematics will find you. And when that happens, you’ll realize that it’s not just useful—it’s essential.


The Foundation of Logic and the Gateway to Vulcan

Mathematics is the foundation of logic. Simple as that. Every decision, every optimization, every efficient calculation you make in code is anchored in mathematical logic. Don’t believe it? Try getting into the Vulcan Science Academy without excelling in numbers. Spoiler: it’s not going to happen.

In technology fields, a strong understanding of mathematics separates performant code from problematic code. It ensures that your query doesn’t turn into a black hole that sucks up all the database (or application) processing power. And this is no exaggeration—a poorly written or poorly designed operation can bring down entire servers.


What Does Mathematics Have to Do with Databases?

If you think mathematics isn’t present in the daily life of databases, here’s a fun fact: the term “relational” in relational databases comes from set theory in mathematics. That’s right.

Every table you create, every relationship between data, is grounded in mathematical principles. Does it seem complicated? It can be. But understanding this is like placing your hand on the control panel of a warp drive—a new world of possibilities opens up.


Why Embrace Mathematics in Technology?

Do you want to shine in areas like data analysis, machine learning, or even in writing elegant and performant SQL (or C, Java, PHP, etc.) code? Mathematics is your ally.

  • Want to create queries that run smoothly and consume minimal resources? Mathematics!
  • Want to design an efficient machine learning algorithm? Mathematics!
  • Want to write Python, SQL, or C++ code with impeccable style and logic? Guess what: Mathematics!

A Journey to the Deep Space of Knowledge

But hold on, no one here wants to turn you into a Vulcan overnight. The goal is to show, in a light and practical way, how mathematics is connected to the universe of data and programming.

In Café com Queries, I want to bring quick, objective, and (why not?) fun texts, exploring how mathematical concepts apply practically to our technological daily lives. We’ll talk about how mathematical functions optimize queries, how statistics improve machine learning models, or even how arithmetic and geometric progressions help in system logic.

So, fasten your seatbelt, align your warp engines, and get ready for this journey. Let’s boldly go where no Nerd has gone before.

Why Learn AP and GP?

Ah, arithmetic progressions (AP) and geometric progressions (GP). For many, they bring back memories of long math classes in school. For others, they are a true superpower that can be used to solve real-world problems. If you think these formulas are just distant theory, get ready—today, I’m going to show how AP and GP can be applied in technology, from databases to modern applications in PHP and C++. Let’s embrace the magic of mathematics with practical examples.

First, it’s worth asking: Why on earth should I learn AP and GP in a world dominated by frameworks, APIs, and machine learning?
Simple: because mathematical patterns are everywhere. They appear in databases, algorithm creation, compound interest modeling, game graphics rendering, and even in dynamic pricing for e-commerce. Understanding how AP and GP work allows you to solve complex problems elegantly (and who doesn’t like to look like a genius?).


How Do AP and GP Work?

  • AP (Arithmetic Progression): Each term is obtained by adding a constant (common difference) to the previous term.
    • General term formula: an=a1+(n−1)×da_n = a_1 + (n – 1) \times dan​=a1​+(n−1)×d
    • Example: 2, 4, 6, 8 (common difference = 2).
  • GP (Geometric Progression): Each term is obtained by multiplying the previous term by a constant (common ratio).
    • General term formula: an=a1×r(n−1)a_n = a_1 \times r^{(n – 1)}an​=a1​×r(n−1)
    • Example: 2, 4, 8, 16 (common ratio = 2).

Where Are AP and GP Used?

1. Databases, Analyses, Dashboards, and Reports

  • AP and GP can calculate accumulated values or predict trends.
  • Classic example: Estimating user growth over time in an application (AP for linear growth, GP for exponential growth).

2. Games

  • AP to calculate the experience needed to level up in RPGs.
  • GP for weapon damage scaling or increasing upgrade costs (who hasn’t struggled to afford that epic item?).

3. E-commerce and Fintechs

  • AP for progressive discount distribution.
  • GP to simulate investment growth or installment payments with compound interest.

4. Machine Learning and AI

  • AP and GP can be useful for scaling datasets or adjusting hyperparameters in models.

Examples in Databases

MySQL: Linear Growth (AP)

Imagine you want to calculate the expected number of new users over 12 months, assuming a fixed monthly growth of 500 users.

WITH RECURSIVE pa_series AS (
    SELECT 1 AS month, 1000 AS users -- Starting with 1000 users
    UNION ALL
    SELECT month + 1, users + 500
    FROM pa_series
    WHERE month < 12
)
SELECT * FROM pa_series;

PostgreSQL: Exponential Growth (GP)

Now, suppose the growth is exponential, doubling the number of users each month.

WITH RECURSIVE gp_series AS (
    SELECT 1 AS month, 1000 AS users -- Starting with 1000 users
    UNION ALL
    SELECT month + 1, users * 2
    FROM gp_series
    WHERE month < 12
)
SELECT * FROM gp_series;

MongoDB: Application with GP

In MongoDB, you can calculate a geometric progression (GP) using aggregations to simulate exponential growth.

db.pg_series.aggregate([
    { $addFields: { users: { $pow: [2, "$month"] } } },
    { $limit: 12 }
]);

Examples in PHP and C++

PHP: Distributing Coupons with AP

$initialValue = 10; // First coupon: $10
$ratio = 5; // Increases by $5 at each step
for ($i = 1; $i <= 10; $i++) {
    $value = $initialValue + ($i - 1) * $ratio;
    echo "Coupon $i: $$value\n";
}

C++: Exponential Growth (GP)

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    double initial = 1000; // Starting with 1000 users
    double ratio = 2; // Geometric ratio
    for (int i = 1; i <= 12; i++) {
        double value = initial * pow(ratio, i - 1);
        cout << "Month " << i << ": " << value << " users\n";
    }
    return 0;
}

Real-World Use Cases

  • E-commerce: Calculating installment values for a product with compound interest (GP).
  • Games: Determining the progressive cost of upgrades in virtual currencies (GP).
  • Fintechs: Simulating investments and accumulated returns over time (GP).
  • Big Data: Generating reports that project future trends (AP).

Which Industries Use AP and GP the Most?

  • Games: To balance game mechanics and create intuitive progression systems.
  • Fintechs: Financial simulations and interest rate calculations.
  • E-commerce: Dynamic pricing and progressive discount campaigns.
  • Education: Developing scalable content, such as difficulty levels.

Enchanted by Mathematics

In Café com Queries, my mission is simple: talk about technology with no strings attached, especially databases, share both good and bad experiences, and, most importantly, show in a practical and fun way how mathematics gives a boost to our technological daily lives.

We’ll talk about functions that make your queries more efficient, statistics that propel machine learning forward, and even mathematical progressions that solve logic problems with elegance.


Don’t Forget!

Take a look at our database monitoring and observability SaaS, Flightdeck:

  • Schedule a demo here.
  • Learn more about Flightdeck here.
dbsnOOp trial
Share

Read more