Added math function power (#604)

* added power algorithm

* updating DIRECTORY.md

* make test function static

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update misc/power_recursion.c

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update misc/power_recursion.c

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update misc/power.c

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update misc/power.c

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update misc/power.c

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update misc/power.c

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update misc/power_recursion.c

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update misc/power_recursion.c

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update misc/power.c

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update misc/power_recursion.c

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update misc/power_recursion.c

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update misc/power.c

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update misc/power.c

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update misc/power_recursion.c

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update misc/power.c

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: David Leal <halfpacho@gmail.com>
This commit is contained in:
Du Yuanchao 2020-09-23 00:50:00 +08:00 committed by GitHub
parent 49e8f4a7d7
commit 88726b9425
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 89 additions and 0 deletions

View File

@ -249,6 +249,8 @@
* [Palindrome](https://github.com/TheAlgorithms/C/blob/master/misc/palindrome.c) * [Palindrome](https://github.com/TheAlgorithms/C/blob/master/misc/palindrome.c)
* [Pid](https://github.com/TheAlgorithms/C/blob/master/misc/pid.c) * [Pid](https://github.com/TheAlgorithms/C/blob/master/misc/pid.c)
* [Poly Add](https://github.com/TheAlgorithms/C/blob/master/misc/poly_add.c) * [Poly Add](https://github.com/TheAlgorithms/C/blob/master/misc/poly_add.c)
* [Power](https://github.com/TheAlgorithms/C/blob/master/misc/power.c)
* [Power Recursion](https://github.com/TheAlgorithms/C/blob/master/misc/power_recursion.c)
* [Prime](https://github.com/TheAlgorithms/C/blob/master/misc/prime.c) * [Prime](https://github.com/TheAlgorithms/C/blob/master/misc/prime.c)
* [Prime Factoriziation](https://github.com/TheAlgorithms/C/blob/master/misc/prime_factoriziation.c) * [Prime Factoriziation](https://github.com/TheAlgorithms/C/blob/master/misc/prime_factoriziation.c)
* [Quartile](https://github.com/TheAlgorithms/C/blob/master/misc/quartile.c) * [Quartile](https://github.com/TheAlgorithms/C/blob/master/misc/quartile.c)

47
misc/power.c Normal file
View File

@ -0,0 +1,47 @@
/**
* @file
* @brief Program to calculate
* [exponentiation](https://en.wikipedia.org/wiki/Exponentiation)
*
* @author [Du Yuanchao](https://github.com/shellhub)
*/
#include <assert.h>
/**
* @brief Returns the value of the first argument raised to the power of the
* second argument.
* @param a the base.
* @param b the exponent.
* @returns the value {@code a}<sup>{@code b}</sup>.
*/
long power(int a, int b)
{
long result = 1;
for (int i = 1; i <= b; ++i)
{
result *= a;
}
return result;
}
/**
* @brief Test function
* @return void
*/
static void test()
{
assert(power(0, 2) == 0);
assert(power(2, 3) == 8);
assert(power(2, 10) == 1024);
assert(power(3, 3) == 27);
}
/**
* @brief Driver Code
* @returns 0 on exit
*/
int main()
{
test();
return 0;
}

40
misc/power_recursion.c Normal file
View File

@ -0,0 +1,40 @@
/**
* @file
* @brief Program to calculate
* [exponentiation](https://en.wikipedia.org/wiki/Exponentiation) using
* recursion algorithm.
*
* @author [Du Yuanchao](https://github.com/shellhub)
*/
#include <assert.h>
/**
* @brief Returns the value of the first argument raised to the power of the
* second argument using recursion.
* @param a the base.
* @param b the exponent.
* @returns the value {@code a}<sup>{@code b}</sup>.
*/
long power(int a, int b) { return b == 0 ? 1 : a * power(a, b - 1); }
/**
* @brief Test function
* @return void
*/
static void test()
{
assert(power(0, 2) == 0);
assert(power(2, 3) == 8);
assert(power(2, 10) == 1024);
assert(power(3, 3) == 27);
}
/**
* @brief Driver Code
* @returns 0 on exit
*/
int main()
{
test();
return 0;
}