mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-14 02:30:40 +08:00
feat: Add ncr mod p code (#1325)
* feat: Add ncr mod p code (#1323) * Update math/ncr_modulo_p.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Added all functions inside a class + added more asserts * updating DIRECTORY.md * clang-format and clang-tidy fixes forf6df24a5* Replace int64_t to uint64_t + add namespace + detailed documentation * clang-format and clang-tidy fixes fore09a0579* Add extra namespace + add const& in function arguments * clang-format and clang-tidy fixes for8111f881* Update ncr_modulo_p.cpp * clang-format and clang-tidy fixes for2ad2f721* Update math/ncr_modulo_p.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update math/ncr_modulo_p.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * Update math/ncr_modulo_p.cpp Co-authored-by: David Leal <halfpacho@gmail.com> * clang-format and clang-tidy fixes for5b69ba5c* updating DIRECTORY.md * clang-format and clang-tidy fixes fora8401d4bCo-authored-by: David Leal <halfpacho@gmail.com> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
* method
|
||||
*
|
||||
* \details
|
||||
* First, multiple intervals are selected with the interval gap provided.
|
||||
* First, multiple intervals are selected with the interval gap provided.
|
||||
* Separate recursive function called for every root.
|
||||
* Roots are printed Separatelt.
|
||||
*
|
||||
@@ -22,8 +22,8 @@
|
||||
* \author Unknown author
|
||||
* \author [Samruddha Patil](https://github.com/sampatil578)
|
||||
*/
|
||||
#include <cmath> /// for math operations
|
||||
#include <iostream> /// for io operations
|
||||
#include <cmath> /// for math operations
|
||||
#include <iostream> /// for io operations
|
||||
|
||||
/**
|
||||
* @namespace numerical_methods
|
||||
@@ -37,93 +37,92 @@ namespace numerical_methods {
|
||||
*/
|
||||
namespace false_position {
|
||||
/**
|
||||
* @brief This function gives the value of f(x) for given x.
|
||||
* @param x value for which we have to find value of f(x).
|
||||
* @return value of f(x) for given x.
|
||||
*/
|
||||
* @brief This function gives the value of f(x) for given x.
|
||||
* @param x value for which we have to find value of f(x).
|
||||
* @return value of f(x) for given x.
|
||||
*/
|
||||
static float eq(float x) {
|
||||
return (x*x-x); // original equation
|
||||
return (x * x - x); // original equation
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function finds root of the equation in given interval i.e. (x1,x2).
|
||||
* @brief This function finds root of the equation in given interval i.e.
|
||||
(x1,x2).
|
||||
* @param x1,x2 values for an interval in which root is present.
|
||||
@param y1,y2 values of function at x1, x2 espectively.
|
||||
* @return root of the equation in the given interval.
|
||||
*/
|
||||
static float regula_falsi(float x1,float x2,float y1,float y2){
|
||||
float diff = x1-x2;
|
||||
if(diff<0){
|
||||
diff= (-1)*diff;
|
||||
*/
|
||||
static float regula_falsi(float x1, float x2, float y1, float y2) {
|
||||
float diff = x1 - x2;
|
||||
if (diff < 0) {
|
||||
diff = (-1) * diff;
|
||||
}
|
||||
if(diff<0.00001){
|
||||
if (y1<0) {
|
||||
y1=-y1;
|
||||
if (diff < 0.00001) {
|
||||
if (y1 < 0) {
|
||||
y1 = -y1;
|
||||
}
|
||||
if (y2<0) {
|
||||
y2=-y2;
|
||||
if (y2 < 0) {
|
||||
y2 = -y2;
|
||||
}
|
||||
if (y1<y2) {
|
||||
if (y1 < y2) {
|
||||
return x1;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
return x2;
|
||||
}
|
||||
}
|
||||
float x3=0,y3=0;
|
||||
x3 = x1 - (x1-x2)*(y1)/(y1-y2);
|
||||
float x3 = 0, y3 = 0;
|
||||
x3 = x1 - (x1 - x2) * (y1) / (y1 - y2);
|
||||
y3 = eq(x3);
|
||||
return regula_falsi(x2,x3,y2,y3);
|
||||
return regula_falsi(x2, x3, y2, y3);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function prints roots of the equation.
|
||||
* @param root which we have to print.
|
||||
* @param count which is count of the root in an interval [-range,range].
|
||||
*/
|
||||
void printRoot(float root, const int16_t &count){
|
||||
if(count==1){
|
||||
* @brief This function prints roots of the equation.
|
||||
* @param root which we have to print.
|
||||
* @param count which is count of the root in an interval [-range,range].
|
||||
*/
|
||||
void printRoot(float root, const int16_t &count) {
|
||||
if (count == 1) {
|
||||
std::cout << "Your 1st root is : " << root << std::endl;
|
||||
}
|
||||
else if(count==2){
|
||||
} else if (count == 2) {
|
||||
std::cout << "Your 2nd root is : " << root << std::endl;
|
||||
}
|
||||
else if(count==3){
|
||||
} else if (count == 3) {
|
||||
std::cout << "Your 3rd root is : " << root << std::endl;
|
||||
}
|
||||
else{
|
||||
std::cout << "Your "<<count<<"th root is : " << root << std::endl;
|
||||
} else {
|
||||
std::cout << "Your " << count << "th root is : " << root << std::endl;
|
||||
}
|
||||
}
|
||||
} // namespace false_position
|
||||
} // namespace numerical_methods
|
||||
|
||||
|
||||
/**
|
||||
* @brief Main function
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
* @brief Main function
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
int main() {
|
||||
float a=0, b=0,i=0,root=0;
|
||||
int16_t count=0;
|
||||
float range = 100000; //Range in which we have to find the root. (-range,range)
|
||||
float gap = 0.5; // interval gap. lesser the gap more the accuracy
|
||||
a = numerical_methods::false_position::eq((-1)*range);
|
||||
i=((-1)*range + gap);
|
||||
//while loop for selecting proper interval in provided range and with provided interval gap.
|
||||
while(i<=range){
|
||||
float a = 0, b = 0, i = 0, root = 0;
|
||||
int16_t count = 0;
|
||||
float range =
|
||||
100000; // Range in which we have to find the root. (-range,range)
|
||||
float gap = 0.5; // interval gap. lesser the gap more the accuracy
|
||||
a = numerical_methods::false_position::eq((-1) * range);
|
||||
i = ((-1) * range + gap);
|
||||
// while loop for selecting proper interval in provided range and with
|
||||
// provided interval gap.
|
||||
while (i <= range) {
|
||||
b = numerical_methods::false_position::eq(i);
|
||||
if(b==0){
|
||||
if (b == 0) {
|
||||
count++;
|
||||
numerical_methods::false_position::printRoot(i,count);
|
||||
numerical_methods::false_position::printRoot(i, count);
|
||||
}
|
||||
if(a*b<0){
|
||||
root = numerical_methods::false_position::regula_falsi(i-gap,i,a,b);
|
||||
if (a * b < 0) {
|
||||
root = numerical_methods::false_position::regula_falsi(i - gap, i,
|
||||
a, b);
|
||||
count++;
|
||||
numerical_methods::false_position::printRoot(root,count);
|
||||
numerical_methods::false_position::printRoot(root, count);
|
||||
}
|
||||
a=b;
|
||||
i+=gap;
|
||||
a = b;
|
||||
i += gap;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,38 +1,36 @@
|
||||
/**
|
||||
* @{
|
||||
* \file
|
||||
* \brief [Runge Kutta fourth order](https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods) method implementation
|
||||
* \brief [Runge Kutta fourth
|
||||
* order](https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods) method
|
||||
* implementation
|
||||
*
|
||||
* \author [Rudra Prasad Das](http://github.com/rudra697)
|
||||
*
|
||||
* \details
|
||||
* It solves the unknown value of y
|
||||
* It solves the unknown value of y
|
||||
* for a given value of x
|
||||
* only first order differential equations
|
||||
* can be solved
|
||||
* \example
|
||||
* it solves \frac{\mathrm{d} y}{\mathrm{d} x}= \frac{\left ( x-y \right )}{2}
|
||||
* given x for given initial
|
||||
* given x for given initial
|
||||
* conditions
|
||||
* There can be many such equations
|
||||
* There can be many such equations
|
||||
*/
|
||||
#include <iostream> /// for io operations
|
||||
#include <vector> /// for using the vector container
|
||||
#include <cassert> /// asserting the test functions
|
||||
#include <cassert> /// asserting the test functions
|
||||
#include <iostream> /// for io operations
|
||||
#include <vector> /// for using the vector container
|
||||
|
||||
/**
|
||||
* @brief The change() function is used
|
||||
* to return the updated iterative value corresponding
|
||||
* @brief The change() function is used
|
||||
* to return the updated iterative value corresponding
|
||||
* to the given function
|
||||
* @param x is the value corresponding to the x coordinate
|
||||
* @param y is the value corresponding to the y coordinate
|
||||
* @param y is the value corresponding to the y coordinate
|
||||
* @returns the computed function value at that call
|
||||
*/
|
||||
static double change(double x, double y)
|
||||
{
|
||||
return ((x - y)/2.0);
|
||||
|
||||
}
|
||||
static double change(double x, double y) { return ((x - y) / 2.0); }
|
||||
|
||||
/**
|
||||
* @namespace numerical_methods
|
||||
@@ -41,95 +39,95 @@ static double change(double x, double y)
|
||||
namespace numerical_methods {
|
||||
/**
|
||||
* @namespace runge_kutta
|
||||
* @brief Functions for [Runge Kutta fourth order](https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods) method
|
||||
* @brief Functions for [Runge Kutta fourth
|
||||
* order](https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods) method
|
||||
*/
|
||||
namespace runge_kutta {
|
||||
/**
|
||||
* @brief the Runge Kutta method finds the value of integration of a function in the given limits.
|
||||
* the lower limit of integration as the initial value and the upper limit is the given x
|
||||
* @brief the Runge Kutta method finds the value of integration of a function in
|
||||
* the given limits. the lower limit of integration as the initial value and the
|
||||
* upper limit is the given x
|
||||
* @param init_x is the value of initial x and is updated after each call
|
||||
* @param init_y is the value of initial x and is updated after each call
|
||||
* @param x is current iteration at which the function needs to be evaluated
|
||||
* @param h is the step value
|
||||
* @returns the value of y at thr required value of x from the initial conditions
|
||||
* @param h is the step value
|
||||
* @returns the value of y at thr required value of x from the initial
|
||||
* conditions
|
||||
*/
|
||||
double rungeKutta(double init_x, const double &init_y, const double &x, const double &h)
|
||||
{
|
||||
|
||||
// Count number of iterations
|
||||
// using step size or
|
||||
// step height h
|
||||
|
||||
|
||||
// n calucates the number of iterations
|
||||
// k1, k2, k3, k4 are the Runge Kutta variables
|
||||
// used for calculation of y at each iteration
|
||||
|
||||
auto n = static_cast<uint64_t>((x - init_x) / h);
|
||||
// used a vector container for the variables
|
||||
std::vector<double> k(4,0.0);
|
||||
|
||||
|
||||
// Iterate for number of iterations
|
||||
|
||||
double y = init_y;
|
||||
for (int i=1; i<=n; ++i)
|
||||
{
|
||||
|
||||
// Apply Runge Kutta Formulas
|
||||
// to find next value of y
|
||||
k[0] = h*change(init_x, y);
|
||||
k[1] = h*change(init_x + 0.5*h, y + 0.5*k[0]);
|
||||
k[2] = h*change(init_x + 0.5*h, y + 0.5*k[1]);
|
||||
k[3] = h*change(init_x + h, y + k[2]);
|
||||
|
||||
|
||||
// Update next value of y
|
||||
|
||||
y += (1.0/6.0)*(k[0] + 2*k[1] + 2*k[2] + k[3]);
|
||||
|
||||
// Update next value of x
|
||||
|
||||
init_x += h;
|
||||
}
|
||||
double rungeKutta(double init_x, const double &init_y, const double &x,
|
||||
const double &h) {
|
||||
// Count number of iterations
|
||||
// using step size or
|
||||
// step height h
|
||||
|
||||
return y;
|
||||
}
|
||||
} // namespace runge_kutta
|
||||
} // namespace numerical_methods
|
||||
// n calucates the number of iterations
|
||||
// k1, k2, k3, k4 are the Runge Kutta variables
|
||||
// used for calculation of y at each iteration
|
||||
|
||||
auto n = static_cast<uint64_t>((x - init_x) / h);
|
||||
// used a vector container for the variables
|
||||
std::vector<double> k(4, 0.0);
|
||||
|
||||
// Iterate for number of iterations
|
||||
|
||||
double y = init_y;
|
||||
for (int i = 1; i <= n; ++i) {
|
||||
// Apply Runge Kutta Formulas
|
||||
// to find next value of y
|
||||
k[0] = h * change(init_x, y);
|
||||
k[1] = h * change(init_x + 0.5 * h, y + 0.5 * k[0]);
|
||||
k[2] = h * change(init_x + 0.5 * h, y + 0.5 * k[1]);
|
||||
k[3] = h * change(init_x + h, y + k[2]);
|
||||
|
||||
// Update next value of y
|
||||
|
||||
y += (1.0 / 6.0) * (k[0] + 2 * k[1] + 2 * k[2] + k[3]);
|
||||
|
||||
// Update next value of x
|
||||
|
||||
init_x += h;
|
||||
}
|
||||
|
||||
return y;
|
||||
}
|
||||
} // namespace runge_kutta
|
||||
} // namespace numerical_methods
|
||||
|
||||
/**
|
||||
* @brief Tests to check algorithm implementation.
|
||||
* @returns void
|
||||
* @returns void
|
||||
*/
|
||||
static void test()
|
||||
{
|
||||
std::cout << "The Runge Kutta function will be tested on the basis of precomputed values\n";
|
||||
static void test() {
|
||||
std::cout << "The Runge Kutta function will be tested on the basis of "
|
||||
"precomputed values\n";
|
||||
|
||||
std::cout << "Test 1...." << "\n";
|
||||
double valfirst=numerical_methods::runge_kutta::rungeKutta(2,3,4,0.2); // Tests the function with pre calculated values
|
||||
assert(valfirst==3.10363932323749570);
|
||||
std::cout << "Passed Test 1\n";
|
||||
std::cout << "Test 1...."
|
||||
<< "\n";
|
||||
double valfirst = numerical_methods::runge_kutta::rungeKutta(
|
||||
2, 3, 4, 0.2); // Tests the function with pre calculated values
|
||||
assert(valfirst == 3.10363932323749570);
|
||||
std::cout << "Passed Test 1\n";
|
||||
|
||||
std::cout << "Test 2...." << "\n";
|
||||
double valsec=numerical_methods::runge_kutta::rungeKutta(1,2,5,0.1); // The value of step changed
|
||||
assert(valsec==3.40600589380261409);
|
||||
std::cout << "Passed Test 2\n";
|
||||
std::cout << "Test 2...."
|
||||
<< "\n";
|
||||
double valsec = numerical_methods::runge_kutta::rungeKutta(
|
||||
1, 2, 5, 0.1); // The value of step changed
|
||||
assert(valsec == 3.40600589380261409);
|
||||
std::cout << "Passed Test 2\n";
|
||||
|
||||
std::cout << "Test 3...."
|
||||
<< "\n";
|
||||
double valthird = numerical_methods::runge_kutta::rungeKutta(
|
||||
-1, 3, 4, 0.1); // Tested with negative value
|
||||
assert(valthird == 2.49251005860244268);
|
||||
std::cout << "Passed Test 3\n";
|
||||
}
|
||||
|
||||
std::cout << "Test 3...." << "\n";
|
||||
double valthird=numerical_methods::runge_kutta::rungeKutta(-1,3,4,0.1); // Tested with negative value
|
||||
assert(valthird==2.49251005860244268);
|
||||
std::cout << "Passed Test 3\n";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Main function
|
||||
* @returns 0 on exit
|
||||
* @returns 0 on exit
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
|
||||
test(); // Execute the tests
|
||||
return 0;
|
||||
}
|
||||
int main() {
|
||||
test(); // Execute the tests
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user