int an = a1 * pow(r, n - 1);
For r != 1
:
int Sn = a1 * (pow(r, n) - 1) / (r - 1);
|r| < 1
):Summation (sigma notation: Σ) is used to sum a sequence of terms.
int n = 10, sum = 0;
for (int i = 1; i <= n; ++i) {
sum += i;
}
Riemann sum formula for consecutive integers:
int rs = n * (n + 1) / 2; // Direct formula instead of loop
The product of a sequence (pi notation: Π) is used to multiply a sequence of terms.
int n = 5, product = 1;
for (int i = 1; i <= n; ++i) {
product *= i;
}
Factorial formula for consecutive integers:
#include <cmath>
int factorial = tgamma(n + 1); // Using gamma function (requires <cmath>)
break
: Terminates the loop or switch statement and transfers control to the statement immediately following.for (int i = 1; i <= 10; ++i) {
if (i == 5) break;
cout << i << " ";
}
continue
: Skips the current iteration of a loop and continues with the next iteration.for (int i = 1; i <= 10; ++i) {
if (i == 5) continue;
cout << i << " ";
}
return
: Exits a function and returns a value to the caller.int add(int a, int b) {
return a + b;
}
int result = add(3, 4);
cout << result << endl;
goto
: Transfers control to a labeled statement within the same function. (Note: can create unreadable and error-prone code, but can also solve problems with recursion).int i = 1;
start:
if (i > 5) goto end;
cout << i << " ";
++i;
goto start;
end:
Nesting occurs when we nest conditionals inside one another. This leads to code that is difficult to read.
inline void foo() {
if (var) {
if (qux) {
if (baz) {
cout << "All conditions are true";
} else {
cout << "baz is false";
}
} else {
cout << "qux is false";
}
} else {
cout << "var is false";
}
}
There are two methods to avoid nesting and become a never-nester: inversion and extraction.
It consists of handling negative cases first and using return statements to exit the control flow as early as possible.
inline void foo() {
if (!var) {
cout << "var is false";
return;
}
if (!qux) {
cout << "qux is false";
return;
}
if (!baz) {
cout << "baz is false";
return;
}
cout << "All conditions are true";
}
It consists of dividing the code into smaller and more specific functions to improve readability.
inline void checkBaz() {
if (!baz) {
cout << "baz is false";
return;
} cout << "All conditions are true";
}
inline void checkQux() {
if (!qux) {
cout << "qux is false";
return;
} checkBaz();
}
inline void foo() {
if (!var) {
cout << "var is false";
return;
} checkQux();
}
The term branching refers to conditionals, when the program diverges into two paths it can become slow in certain cases because the CPU tries to get ahead by preloading one of the possible functions. The branchless methodology avoids this, but it can make the function less readable.
inline int minorBranch(int a, int b) {
if (a < b)
return a;
return b;
}
inline int minorBranchLess(int a, int b) {
return a * (a < b) + b * (b <= a);
}
Lambdas or lambda functions allow defining anonymous functions concisely. They are useful for creating short functions that are used in the context of another function, such as in STL algorithms.
An example:
py
suma = lambda a, b: a + b
res = suma(5, 3)
c++
auto suma = [](int a, int b) -> int { return a + b; };
int res = suma(5, 3);
Recordar el write = sys.stdout.write ya que no se volvera a repetir y se asumira en lugar de print para ejemplos
mencionar que la parte del codigo que nos interesa es el algoritmo en si, el read y el write
El chiste es que conditional suena similar a conditioner
F = False, T = True
En el ultimo snippet de código preguntar cuales serian los resultados cuando programming
explicar que el while no parara con true
Aquí los ejemplos seran en c++ y ya si quieren python que pregunten a chatpgt xD
Progreción aritmetica en español
explicar cada funcion
a_n is the nth term. / a_n es el término n-ésimo. a_1 is the first term. / a_1 es el primer término. n is the term number. / n es el número del término. d is the common difference. / d es la diferencia común.
a_n is the nth term. / a_n es el término n-ésimo. a_1 is the first term. / a_1 es el primer término. n is the term number. / n es el número del término. r is the common ratio. / r es la razón común.
i=1: Es el índice de la sumatoria. En el código, es la variable de iteración del bucle for. Donde 1 es el valor inicial. n: Es el valor final del índice i. En el código, corresponde a la condición de paro del bucle i <= n. i: Es el término general que se suma. En el código, es lo que se suma a la variable sum en cada iteración sum += i.
i=1: Es el índice de la sumatoria. En el código, es la variable de iteración del bucle for. Donde 1 es el valor inicial. n: Es el valor final del índice i. En el código, corresponde a la condición de paro del bucle i <= n. i: Es el término general que se multiplica. En el código, es lo que se multiplica por la variable product en cada iteración product *= i.
si preguntan Inline obliga al compilador a siempre poner la funcion dentro del bloque donde se llama (aunque aveces el compilador lo hace solo)
esto es redundante y no tan necesario ya que el codigo no es legible y aun asi el compilador optimizaria este codigo
La sintaxis básica de una lambda es: [capturas](parámetros) -> tipo_retorno { // Cuerpo de la función };
auto nos ahorra escribir: std::function<int(int, int)> suma = [](int a, int b) -> int { return a + b; };
explicar que es necesario hacer el primer problema en el club y el segundo se lo pueden llevar de tarea