add math fuction

This commit is contained in:
2026-04-09 16:23:37 +03:00
parent 7e85152094
commit 8df7eb638b
2 changed files with 13 additions and 0 deletions

View File

@@ -13,6 +13,9 @@ auto main(int argc, char *argv[]) -> int
int a1 = -4, b1 = -5;
hack::log()(hack::math::max_abs(a1, b1));
// 1, 2, 3, 4, 5
hack::log()(hack::math::sum_arithmetic_progression(1, 1, 5));
return 0;
}

View File

@@ -31,4 +31,14 @@ namespace hack::math
{
return std::abs(a) < std::abs(b) ? a : b;
}
// Функция для вычисления суммы арифметической прогрессии
// a1 - первый член
// d - разность (шаг)
// n - количество членов
inline long long sum_arithmetic_progression(long long a1, long long d, std::size_t n) {
// Формула суммы: S = n * (2*a1 + (n-1)*d) / 2
// см. wiki/math
return n * (2 * a1 + (n - 1) * d) / 2;
}
}