mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-11-29 23:45:34 +08:00
formatting source-code for 153fb7b8a5
This commit is contained in:
@@ -25,32 +25,24 @@
|
||||
|
||||
/// Recursive function to calculate exponent in \f$O(\log(n))\f$ using binary
|
||||
/// exponent.
|
||||
int binExpo(int a, int b)
|
||||
{
|
||||
if (b == 0)
|
||||
{
|
||||
int binExpo(int a, int b) {
|
||||
if (b == 0) {
|
||||
return 1;
|
||||
}
|
||||
int res = binExpo(a, b / 2);
|
||||
if (b % 2)
|
||||
{
|
||||
if (b % 2) {
|
||||
return res * res * a;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
return res * res;
|
||||
}
|
||||
}
|
||||
|
||||
/// Iterative function to calculate exponent in \f$O(\log(n))\f$ using binary
|
||||
/// exponent.
|
||||
int binExpo_alt(int a, int b)
|
||||
{
|
||||
int binExpo_alt(int a, int b) {
|
||||
int res = 1;
|
||||
while (b > 0)
|
||||
{
|
||||
if (b % 2)
|
||||
{
|
||||
while (b > 0) {
|
||||
if (b % 2) {
|
||||
res = res * a;
|
||||
}
|
||||
a = a * a;
|
||||
@@ -60,21 +52,15 @@ int binExpo_alt(int a, int b)
|
||||
}
|
||||
|
||||
/// Main function
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int a, b;
|
||||
/// Give two numbers a, b
|
||||
std::cin >> a >> b;
|
||||
if (a == 0 && b == 0)
|
||||
{
|
||||
if (a == 0 && b == 0) {
|
||||
std::cout << "Math error" << std::endl;
|
||||
}
|
||||
else if (b < 0)
|
||||
{
|
||||
} else if (b < 0) {
|
||||
std::cout << "Exponent must be positive !!" << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
int resRecurse = binExpo(a, b);
|
||||
/// int resIterate = binExpo_alt(a, b);
|
||||
|
||||
|
||||
@@ -13,11 +13,9 @@
|
||||
|
||||
/** Compute double factorial using iterative method
|
||||
*/
|
||||
uint64_t double_factorial_iterative(uint64_t n)
|
||||
{
|
||||
uint64_t double_factorial_iterative(uint64_t n) {
|
||||
uint64_t res = 1;
|
||||
for (uint64_t i = n;; i -= 2)
|
||||
{
|
||||
for (uint64_t i = n;; i -= 2) {
|
||||
if (i == 0 || i == 1)
|
||||
return res;
|
||||
res *= i;
|
||||
@@ -28,16 +26,14 @@ uint64_t double_factorial_iterative(uint64_t n)
|
||||
/** Compute double factorial using resursive method.
|
||||
* <br/>Recursion can be costly for large numbers.
|
||||
*/
|
||||
uint64_t double_factorial_recursive(uint64_t n)
|
||||
{
|
||||
uint64_t double_factorial_recursive(uint64_t n) {
|
||||
if (n <= 1)
|
||||
return 1;
|
||||
return n * double_factorial_recursive(n - 2);
|
||||
}
|
||||
|
||||
/// main function
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
uint64_t n;
|
||||
std::cin >> n;
|
||||
assert(n >= 0);
|
||||
|
||||
@@ -29,15 +29,11 @@
|
||||
|
||||
/** Function to caculate Euler's totient phi
|
||||
*/
|
||||
uint64_t phiFunction(uint64_t n)
|
||||
{
|
||||
uint64_t phiFunction(uint64_t n) {
|
||||
uint64_t result = n;
|
||||
for (uint64_t i = 2; i * i <= n; i++)
|
||||
{
|
||||
if (n % i == 0)
|
||||
{
|
||||
while (n % i == 0)
|
||||
{
|
||||
for (uint64_t i = 2; i * i <= n; i++) {
|
||||
if (n % i == 0) {
|
||||
while (n % i == 0) {
|
||||
n /= i;
|
||||
}
|
||||
result -= result / i;
|
||||
@@ -49,15 +45,11 @@ uint64_t phiFunction(uint64_t n)
|
||||
}
|
||||
|
||||
/// Main function
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int main(int argc, char *argv[]) {
|
||||
uint64_t n;
|
||||
if (argc < 2)
|
||||
{
|
||||
if (argc < 2) {
|
||||
std::cout << "Enter the number: ";
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
n = strtoull(argv[1], nullptr, 10);
|
||||
}
|
||||
std::cin >> n;
|
||||
|
||||
@@ -21,8 +21,7 @@
|
||||
* @param[in] quotient unsigned
|
||||
*/
|
||||
template <typename T, typename T2>
|
||||
inline void update_step(T *r, T *r0, const T2 quotient)
|
||||
{
|
||||
inline void update_step(T *r, T *r0, const T2 quotient) {
|
||||
T temp = *r;
|
||||
*r = *r0 - (quotient * temp);
|
||||
*r0 = temp;
|
||||
@@ -39,8 +38,7 @@ inline void update_step(T *r, T *r0, const T2 quotient)
|
||||
* @param[out] y signed
|
||||
*/
|
||||
template <typename T1, typename T2>
|
||||
void extendedEuclid_1(T1 A, T1 B, T1 *GCD, T2 *x, T2 *y)
|
||||
{
|
||||
void extendedEuclid_1(T1 A, T1 B, T1 *GCD, T2 *x, T2 *y) {
|
||||
if (B > A)
|
||||
std::swap(A, B); // Ensure that A >= B
|
||||
|
||||
@@ -48,8 +46,7 @@ void extendedEuclid_1(T1 A, T1 B, T1 *GCD, T2 *x, T2 *y)
|
||||
T2 t = 1, t0 = 0;
|
||||
T1 r = B, r0 = A;
|
||||
|
||||
while (r != 0)
|
||||
{
|
||||
while (r != 0) {
|
||||
T1 quotient = r0 / r;
|
||||
update_step(&r, &r0, quotient);
|
||||
update_step(&s, &s0, quotient);
|
||||
@@ -70,19 +67,15 @@ void extendedEuclid_1(T1 A, T1 B, T1 *GCD, T2 *x, T2 *y)
|
||||
* @param[in,out] y signed
|
||||
*/
|
||||
template <typename T, typename T2>
|
||||
void extendedEuclid(T A, T B, T *GCD, T2 *x, T2 *y)
|
||||
{
|
||||
void extendedEuclid(T A, T B, T *GCD, T2 *x, T2 *y) {
|
||||
if (B > A)
|
||||
std::swap(A, B); // Ensure that A >= B
|
||||
|
||||
if (B == 0)
|
||||
{
|
||||
if (B == 0) {
|
||||
*GCD = A;
|
||||
*x = 1;
|
||||
*y = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
extendedEuclid(B, A % B, GCD, x, y);
|
||||
T2 temp = *x;
|
||||
*x = *y;
|
||||
@@ -91,8 +84,7 @@ void extendedEuclid(T A, T B, T *GCD, T2 *x, T2 *y)
|
||||
}
|
||||
|
||||
/// Main function
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
uint32_t a, b, gcd;
|
||||
int32_t x, y;
|
||||
std::cin >> a >> b;
|
||||
|
||||
@@ -5,16 +5,14 @@
|
||||
#include <iostream>
|
||||
|
||||
/** function to find factorial of given number */
|
||||
unsigned int factorial(unsigned int n)
|
||||
{
|
||||
unsigned int factorial(unsigned int n) {
|
||||
if (n == 0)
|
||||
return 1;
|
||||
return n * factorial(n - 1);
|
||||
}
|
||||
|
||||
/** Main function */
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int num = 5;
|
||||
std::cout << "Factorial of " << num << " is " << factorial(num)
|
||||
<< std::endl;
|
||||
|
||||
@@ -23,8 +23,7 @@
|
||||
* algorithm implementation for \f$a^b\f$
|
||||
*/
|
||||
template <typename T>
|
||||
double fast_power_recursive(T a, T b)
|
||||
{
|
||||
double fast_power_recursive(T a, T b) {
|
||||
// negative power. a^b = 1 / (a^-b)
|
||||
if (b < 0)
|
||||
return 1.0 / fast_power_recursive(a, -b);
|
||||
@@ -48,15 +47,13 @@ double fast_power_recursive(T a, T b)
|
||||
It still calculates in \f$O(\log N)\f$
|
||||
*/
|
||||
template <typename T>
|
||||
double fast_power_linear(T a, T b)
|
||||
{
|
||||
double fast_power_linear(T a, T b) {
|
||||
// negative power. a^b = 1 / (a^-b)
|
||||
if (b < 0)
|
||||
return 1.0 / fast_power_linear(a, -b);
|
||||
|
||||
double result = 1;
|
||||
while (b)
|
||||
{
|
||||
while (b) {
|
||||
if (b & 1)
|
||||
result = result * a;
|
||||
a = a * a;
|
||||
@@ -68,14 +65,12 @@ double fast_power_linear(T a, T b)
|
||||
/**
|
||||
* Main function
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
std::srand(std::time(nullptr));
|
||||
std::ios_base::sync_with_stdio(false);
|
||||
|
||||
std::cout << "Testing..." << std::endl;
|
||||
for (int i = 0; i < 20; i++)
|
||||
{
|
||||
for (int i = 0; i < 20; i++) {
|
||||
int a = std::rand() % 20 - 10;
|
||||
int b = std::rand() % 20 - 10;
|
||||
std::cout << std::endl << "Calculating " << a << "^" << b << std::endl;
|
||||
|
||||
@@ -14,8 +14,7 @@
|
||||
/**
|
||||
* Recursively compute sequences
|
||||
*/
|
||||
int fibonacci(unsigned int n)
|
||||
{
|
||||
int fibonacci(unsigned int n) {
|
||||
/* If the input is 0 or 1 just return the same
|
||||
This will set the first 2 values of the sequence */
|
||||
if (n <= 1)
|
||||
@@ -26,8 +25,7 @@ int fibonacci(unsigned int n)
|
||||
}
|
||||
|
||||
/// Main function
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int n;
|
||||
std::cin >> n;
|
||||
assert(n >= 0);
|
||||
|
||||
@@ -26,8 +26,7 @@ const uint64_t MAX = 93;
|
||||
uint64_t f[MAX] = {0};
|
||||
|
||||
/** Algorithm */
|
||||
uint64_t fib(uint64_t n)
|
||||
{
|
||||
uint64_t fib(uint64_t n) {
|
||||
if (n == 0)
|
||||
return 0;
|
||||
if (n == 1 || n == 2)
|
||||
@@ -44,11 +43,9 @@ uint64_t fib(uint64_t n)
|
||||
}
|
||||
|
||||
/** Main function */
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
// Main Function
|
||||
for (uint64_t i = 1; i < 93; i++)
|
||||
{
|
||||
for (uint64_t i = 1; i < 93; i++) {
|
||||
std::cout << i << " th fibonacci number is " << fib(i) << std::endl;
|
||||
}
|
||||
return 0;
|
||||
|
||||
@@ -20,13 +20,11 @@
|
||||
* \f[f(n)=f(n-1)+f(n-2)\f]
|
||||
* and returns the result as a large_number type.
|
||||
*/
|
||||
large_number fib(uint64_t n)
|
||||
{
|
||||
large_number fib(uint64_t n) {
|
||||
large_number f0(1);
|
||||
large_number f1(1);
|
||||
|
||||
do
|
||||
{
|
||||
do {
|
||||
large_number f2 = f1;
|
||||
f1 += f0;
|
||||
f0 = f2;
|
||||
@@ -36,15 +34,11 @@ large_number fib(uint64_t n)
|
||||
return f1;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int main(int argc, char *argv[]) {
|
||||
uint64_t N;
|
||||
if (argc == 2)
|
||||
{
|
||||
if (argc == 2) {
|
||||
N = strtoull(argv[1], NULL, 10);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
std::cout << "Enter N: ";
|
||||
std::cin >> N;
|
||||
}
|
||||
|
||||
@@ -12,34 +12,27 @@
|
||||
/**
|
||||
* algorithm
|
||||
*/
|
||||
int gcd(int num1, int num2)
|
||||
{
|
||||
if (num1 <= 0 | num2 <= 0)
|
||||
{
|
||||
int gcd(int num1, int num2) {
|
||||
if (num1 <= 0 | num2 <= 0) {
|
||||
throw std::domain_error("Euclidean algorithm domain is for ints > 0");
|
||||
}
|
||||
|
||||
if (num1 == num2)
|
||||
{
|
||||
if (num1 == num2) {
|
||||
return num1;
|
||||
}
|
||||
|
||||
int base_num = 0;
|
||||
int previous_remainder = 1;
|
||||
|
||||
if (num1 > num2)
|
||||
{
|
||||
if (num1 > num2) {
|
||||
base_num = num1;
|
||||
previous_remainder = num2;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
base_num = num2;
|
||||
previous_remainder = num1;
|
||||
}
|
||||
|
||||
while ((base_num % previous_remainder) != 0)
|
||||
{
|
||||
while ((base_num % previous_remainder) != 0) {
|
||||
int old_base = base_num;
|
||||
base_num = previous_remainder;
|
||||
previous_remainder = old_base % previous_remainder;
|
||||
@@ -51,15 +44,11 @@ int gcd(int num1, int num2)
|
||||
/**
|
||||
* Main function
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
std::cout << "gcd of 120,7 is " << (gcd(120, 7)) << std::endl;
|
||||
try
|
||||
{
|
||||
try {
|
||||
std::cout << "gcd of -120,10 is " << gcd(-120, 10) << std::endl;
|
||||
}
|
||||
catch (const std::domain_error &e)
|
||||
{
|
||||
} catch (const std::domain_error &e) {
|
||||
std::cout << "Error handling was successful" << std::endl;
|
||||
}
|
||||
std::cout << "gcd of 312,221 is " << (gcd(312, 221)) << std::endl;
|
||||
|
||||
@@ -12,12 +12,10 @@
|
||||
* @param[in] a array of integers to compute GCD for
|
||||
* @param[in] n number of integers in array `a`
|
||||
*/
|
||||
int gcd(int *a, int n)
|
||||
{
|
||||
int gcd(int *a, int n) {
|
||||
int j = 1; // to access all elements of the array starting from 1
|
||||
int gcd = a[0];
|
||||
while (j < n)
|
||||
{
|
||||
while (j < n) {
|
||||
if (a[j] % gcd == 0) // value of gcd is as needed so far
|
||||
j++; // so we check for next element
|
||||
else
|
||||
@@ -27,8 +25,7 @@ int gcd(int *a, int n)
|
||||
}
|
||||
|
||||
/** Main function */
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int n;
|
||||
std::cout << "Enter value of n:" << std::endl;
|
||||
std::cin >> n;
|
||||
|
||||
@@ -11,15 +11,12 @@
|
||||
/**
|
||||
* algorithm
|
||||
*/
|
||||
int gcd(int num1, int num2)
|
||||
{
|
||||
if (num1 <= 0 | num2 <= 0)
|
||||
{
|
||||
int gcd(int num1, int num2) {
|
||||
if (num1 <= 0 | num2 <= 0) {
|
||||
throw std::domain_error("Euclidean algorithm domain is for ints > 0");
|
||||
}
|
||||
|
||||
if (num1 == num2)
|
||||
{
|
||||
if (num1 == num2) {
|
||||
return num1;
|
||||
}
|
||||
|
||||
@@ -42,15 +39,11 @@ int gcd(int num1, int num2)
|
||||
/**
|
||||
* Main function
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
std::cout << "gcd of 120,7 is " << (gcd(120, 7)) << std::endl;
|
||||
try
|
||||
{
|
||||
try {
|
||||
std::cout << "gcd of -120,10 is " << gcd(-120, 10) << std::endl;
|
||||
}
|
||||
catch (const std::domain_error &e)
|
||||
{
|
||||
} catch (const std::domain_error &e) {
|
||||
std::cout << "Error handling was successful" << std::endl;
|
||||
}
|
||||
std::cout << "gcd of 312,221 is " << (gcd(312, 221)) << std::endl;
|
||||
|
||||
@@ -13,8 +13,7 @@
|
||||
/** Test implementation for 10! Result must be 3628800.
|
||||
* @returns True if test pass else False
|
||||
*/
|
||||
bool test1()
|
||||
{
|
||||
bool test1() {
|
||||
std::cout << "---- Check 1\t";
|
||||
unsigned int i, number = 10;
|
||||
large_number result;
|
||||
@@ -24,18 +23,15 @@ bool test1()
|
||||
const char *known_reslt = "3628800";
|
||||
|
||||
/* check 1 */
|
||||
if (strlen(known_reslt) != result.num_digits())
|
||||
{
|
||||
if (strlen(known_reslt) != result.num_digits()) {
|
||||
std::cerr << "Result lengths dont match! " << strlen(known_reslt)
|
||||
<< " != " << result.num_digits() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
const size_t N = result.num_digits();
|
||||
for (i = 0; i < N; i++)
|
||||
{
|
||||
if (known_reslt[i] != result.digit_char(i))
|
||||
{
|
||||
for (i = 0; i < N; i++) {
|
||||
if (known_reslt[i] != result.digit_char(i)) {
|
||||
std::cerr << i << "^th digit mismatch! " << known_reslt[i]
|
||||
<< " != " << result.digit_char(i) << std::endl;
|
||||
return false;
|
||||
@@ -54,8 +50,7 @@ bool test1()
|
||||
* ```
|
||||
* @returns True if test pass else False
|
||||
*/
|
||||
bool test2()
|
||||
{
|
||||
bool test2() {
|
||||
std::cout << "---- Check 2\t";
|
||||
unsigned int i, number = 100;
|
||||
large_number result;
|
||||
@@ -68,18 +63,15 @@ bool test2()
|
||||
"000000000000000000";
|
||||
|
||||
/* check 1 */
|
||||
if (strlen(known_reslt) != result.num_digits())
|
||||
{
|
||||
if (strlen(known_reslt) != result.num_digits()) {
|
||||
std::cerr << "Result lengths dont match! " << strlen(known_reslt)
|
||||
<< " != " << result.num_digits() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
const size_t N = result.num_digits();
|
||||
for (i = 0; i < N; i++)
|
||||
{
|
||||
if (known_reslt[i] != result.digit_char(i))
|
||||
{
|
||||
for (i = 0; i < N; i++) {
|
||||
if (known_reslt[i] != result.digit_char(i)) {
|
||||
std::cerr << i << "^th digit mismatch! " << known_reslt[i]
|
||||
<< " != " << result.digit_char(i) << std::endl;
|
||||
return false;
|
||||
@@ -93,16 +85,12 @@ bool test2()
|
||||
/**
|
||||
* Main program
|
||||
**/
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int main(int argc, char *argv[]) {
|
||||
int number, i;
|
||||
|
||||
if (argc == 2)
|
||||
{
|
||||
if (argc == 2) {
|
||||
number = atoi(argv[1]);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
std::cout << "Enter the value of n(n starts from 0 ): ";
|
||||
std::cin >> number;
|
||||
}
|
||||
|
||||
@@ -20,8 +20,7 @@
|
||||
* digit to the number, perform multiplication of
|
||||
* large number with long unsigned integers.
|
||||
**/
|
||||
class large_number
|
||||
{
|
||||
class large_number {
|
||||
public:
|
||||
/**< initializer with value = 1 */
|
||||
large_number() { _digits.push_back(1); }
|
||||
@@ -36,11 +35,9 @@ class large_number
|
||||
// }
|
||||
|
||||
/**< initializer from an integer */
|
||||
explicit large_number(int n)
|
||||
{
|
||||
explicit large_number(int n) {
|
||||
int carry = n;
|
||||
do
|
||||
{
|
||||
do {
|
||||
add_digit(carry % 10);
|
||||
carry /= 10;
|
||||
} while (carry != 0);
|
||||
@@ -53,10 +50,8 @@ class large_number
|
||||
explicit large_number(std::vector<unsigned char> &vec) : _digits(vec) {}
|
||||
|
||||
/**< initializer from a string */
|
||||
explicit large_number(char const *number_str)
|
||||
{
|
||||
for (size_t i = strlen(number_str); i > 0; i--)
|
||||
{
|
||||
explicit large_number(char const *number_str) {
|
||||
for (size_t i = strlen(number_str); i > 0; i--) {
|
||||
unsigned char a = number_str[i - 1] - '0';
|
||||
if (a >= 0 && a <= 9)
|
||||
_digits.push_back(a);
|
||||
@@ -66,55 +61,48 @@ class large_number
|
||||
/**
|
||||
* Function to check implementation
|
||||
**/
|
||||
static bool test()
|
||||
{
|
||||
static bool test() {
|
||||
std::cout << "------ Checking `large_number` class implementations\t"
|
||||
<< std::endl;
|
||||
large_number a(40);
|
||||
// 1. test multiplication
|
||||
a *= 10;
|
||||
if (a != large_number(400))
|
||||
{
|
||||
if (a != large_number(400)) {
|
||||
std::cerr << "\tFailed 1/6 (" << a << "!=400)" << std::endl;
|
||||
return false;
|
||||
}
|
||||
std::cout << "\tPassed 1/6...";
|
||||
// 2. test compound addition with integer
|
||||
a += 120;
|
||||
if (a != large_number(520))
|
||||
{
|
||||
if (a != large_number(520)) {
|
||||
std::cerr << "\tFailed 2/6 (" << a << "!=520)" << std::endl;
|
||||
return false;
|
||||
}
|
||||
std::cout << "\tPassed 2/6...";
|
||||
// 3. test compound multiplication again
|
||||
a *= 10;
|
||||
if (a != large_number(5200))
|
||||
{
|
||||
if (a != large_number(5200)) {
|
||||
std::cerr << "\tFailed 3/6 (" << a << "!=5200)" << std::endl;
|
||||
return false;
|
||||
}
|
||||
std::cout << "\tPassed 3/6...";
|
||||
// 4. test increment (prefix)
|
||||
++a;
|
||||
if (a != large_number(5201))
|
||||
{
|
||||
if (a != large_number(5201)) {
|
||||
std::cerr << "\tFailed 4/6 (" << a << "!=5201)" << std::endl;
|
||||
return false;
|
||||
}
|
||||
std::cout << "\tPassed 4/6...";
|
||||
// 5. test increment (postfix)
|
||||
a++;
|
||||
if (a != large_number(5202))
|
||||
{
|
||||
if (a != large_number(5202)) {
|
||||
std::cerr << "\tFailed 5/6 (" << a << "!=5202)" << std::endl;
|
||||
return false;
|
||||
}
|
||||
std::cout << "\tPassed 5/6...";
|
||||
// 6. test addition with another large number
|
||||
a = a + large_number("7000000000000000000000000000000");
|
||||
if (a != large_number("7000000000000000000000000005202"))
|
||||
{
|
||||
if (a != large_number("7000000000000000000000000005202")) {
|
||||
std::cerr << "\tFailed 6/6 (" << a
|
||||
<< "!=7000000000000000000000000005202)" << std::endl;
|
||||
return false;
|
||||
@@ -126,10 +114,8 @@ class large_number
|
||||
/**
|
||||
* add a digit at MSB to the large number
|
||||
**/
|
||||
void add_digit(unsigned int value)
|
||||
{
|
||||
if (value > 9)
|
||||
{
|
||||
void add_digit(unsigned int value) {
|
||||
if (value > 9) {
|
||||
std::cerr << "digit > 9!!\n";
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
@@ -149,16 +135,14 @@ class large_number
|
||||
**/
|
||||
inline unsigned char &operator[](size_t n) { return this->_digits[n]; }
|
||||
|
||||
inline const unsigned char &operator[](size_t n) const
|
||||
{
|
||||
inline const unsigned char &operator[](size_t n) const {
|
||||
return this->_digits[n];
|
||||
}
|
||||
|
||||
/**
|
||||
* operator overload to compare two numbers
|
||||
**/
|
||||
friend std::ostream &operator<<(std::ostream &out, const large_number &a)
|
||||
{
|
||||
friend std::ostream &operator<<(std::ostream &out, const large_number &a) {
|
||||
for (size_t i = a.num_digits(); i > 0; i--)
|
||||
out << static_cast<int>(a[i - 1]);
|
||||
return out;
|
||||
@@ -167,8 +151,7 @@ class large_number
|
||||
/**
|
||||
* operator overload to compare two numbers
|
||||
**/
|
||||
friend bool operator==(large_number const &a, large_number const &b)
|
||||
{
|
||||
friend bool operator==(large_number const &a, large_number const &b) {
|
||||
size_t N = a.num_digits();
|
||||
if (N != b.num_digits())
|
||||
return false;
|
||||
@@ -181,16 +164,14 @@ class large_number
|
||||
/**
|
||||
* operator overload to compare two numbers
|
||||
**/
|
||||
friend bool operator!=(large_number const &a, large_number const &b)
|
||||
{
|
||||
friend bool operator!=(large_number const &a, large_number const &b) {
|
||||
return !(a == b);
|
||||
}
|
||||
|
||||
/**
|
||||
* operator overload to increment (prefix)
|
||||
**/
|
||||
large_number &operator++()
|
||||
{
|
||||
large_number &operator++() {
|
||||
(*this) += 1;
|
||||
return *this;
|
||||
}
|
||||
@@ -198,8 +179,7 @@ class large_number
|
||||
/**
|
||||
* operator overload to increment (postfix)
|
||||
**/
|
||||
large_number &operator++(int)
|
||||
{
|
||||
large_number &operator++(int) {
|
||||
static large_number tmp(_digits);
|
||||
++(*this);
|
||||
return tmp;
|
||||
@@ -208,15 +188,13 @@ class large_number
|
||||
/**
|
||||
* operator overload to add
|
||||
**/
|
||||
large_number &operator+=(large_number n)
|
||||
{
|
||||
large_number &operator+=(large_number n) {
|
||||
// if adding with another large_number
|
||||
large_number *b = reinterpret_cast<large_number *>(&n);
|
||||
const size_t max_L = std::max(this->num_digits(), b->num_digits());
|
||||
unsigned int carry = 0;
|
||||
size_t i;
|
||||
for (i = 0; i < max_L || carry != 0; i++)
|
||||
{
|
||||
for (i = 0; i < max_L || carry != 0; i++) {
|
||||
if (i < b->num_digits())
|
||||
carry += (*b)[i];
|
||||
if (i < this->num_digits())
|
||||
@@ -238,8 +216,7 @@ class large_number
|
||||
* operator overload to perform addition
|
||||
**/
|
||||
template <class T>
|
||||
friend large_number &operator+(const large_number &a, const T &b)
|
||||
{
|
||||
friend large_number &operator+(const large_number &a, const T &b) {
|
||||
static large_number c = a;
|
||||
c += b;
|
||||
return c;
|
||||
@@ -248,8 +225,7 @@ class large_number
|
||||
/**
|
||||
* assignment operator
|
||||
**/
|
||||
large_number &operator=(const large_number &b)
|
||||
{
|
||||
large_number &operator=(const large_number &b) {
|
||||
this->_digits = b._digits;
|
||||
return *this;
|
||||
}
|
||||
@@ -258,8 +234,7 @@ class large_number
|
||||
* operator overload to increment
|
||||
**/
|
||||
template <class T>
|
||||
large_number &operator*=(const T n)
|
||||
{
|
||||
large_number &operator*=(const T n) {
|
||||
static_assert(std::is_integral<T>::value,
|
||||
"Must be integer addition unsigned integer types.");
|
||||
this->multiply(n);
|
||||
@@ -269,8 +244,7 @@ class large_number
|
||||
/**
|
||||
* returns i^th digit as an ASCII character
|
||||
**/
|
||||
const char digit_char(size_t i) const
|
||||
{
|
||||
const char digit_char(size_t i) const {
|
||||
return _digits[num_digits() - i - 1] + '0';
|
||||
}
|
||||
|
||||
@@ -280,8 +254,7 @@ class large_number
|
||||
* store the result in the same large number
|
||||
**/
|
||||
template <class T>
|
||||
void multiply(const T n)
|
||||
{
|
||||
void multiply(const T n) {
|
||||
static_assert(std::is_integral<T>::value,
|
||||
"Can only have integer types.");
|
||||
// assert(!(std::is_signed<T>::value)); //, "Implemented only for
|
||||
@@ -289,24 +262,19 @@ class large_number
|
||||
|
||||
size_t i;
|
||||
uint64_t carry = 0, temp;
|
||||
for (i = 0; i < this->num_digits(); i++)
|
||||
{
|
||||
for (i = 0; i < this->num_digits(); i++) {
|
||||
temp = (*this)[i] * n;
|
||||
temp += carry;
|
||||
if (temp < 10)
|
||||
{
|
||||
if (temp < 10) {
|
||||
carry = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
carry = temp / 10;
|
||||
temp = temp % 10;
|
||||
}
|
||||
(*this)[i] = temp;
|
||||
}
|
||||
|
||||
while (carry != 0)
|
||||
{
|
||||
while (carry != 0) {
|
||||
this->add_digit(carry % 10);
|
||||
carry /= 10;
|
||||
}
|
||||
|
||||
@@ -49,14 +49,11 @@
|
||||
/** Recursive function to calculate exponent in \f$O(\log n)\f$ using binary
|
||||
* exponent.
|
||||
*/
|
||||
int64_t binExpo(int64_t a, int64_t b, int64_t m)
|
||||
{
|
||||
int64_t binExpo(int64_t a, int64_t b, int64_t m) {
|
||||
a %= m;
|
||||
int64_t res = 1;
|
||||
while (b > 0)
|
||||
{
|
||||
if (b % 2)
|
||||
{
|
||||
while (b > 0) {
|
||||
if (b % 2) {
|
||||
res = res * a % m;
|
||||
}
|
||||
a = a * a % m;
|
||||
@@ -68,18 +65,12 @@ int64_t binExpo(int64_t a, int64_t b, int64_t m)
|
||||
|
||||
/** Prime check in \f$O(\sqrt{m})\f$ time.
|
||||
*/
|
||||
bool isPrime(int64_t m)
|
||||
{
|
||||
if (m <= 1)
|
||||
{
|
||||
bool isPrime(int64_t m) {
|
||||
if (m <= 1) {
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (int64_t i = 2; i * i <= m; i++)
|
||||
{
|
||||
if (m % i == 0)
|
||||
{
|
||||
} else {
|
||||
for (int64_t i = 2; i * i <= m; i++) {
|
||||
if (m % i == 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -90,21 +81,17 @@ bool isPrime(int64_t m)
|
||||
/**
|
||||
* Main function
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int64_t a, m;
|
||||
// Take input of a and m.
|
||||
std::cout << "Computing ((a^(-1))%(m)) using Fermat's Little Theorem";
|
||||
std::cout << std::endl << std::endl;
|
||||
std::cout << "Give input 'a' and 'm' space separated : ";
|
||||
std::cin >> a >> m;
|
||||
if (isPrime(m))
|
||||
{
|
||||
if (isPrime(m)) {
|
||||
std::cout << "The modular inverse of a with mod m is (a^(m-2)) : ";
|
||||
std::cout << binExpo(a, m - 2, m) << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
std::cout << "m must be a prime number.";
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
@@ -31,31 +31,25 @@ respectively.
|
||||
/**
|
||||
* Algorithm
|
||||
*/
|
||||
int number_of_positive_divisors(int n)
|
||||
{
|
||||
int number_of_positive_divisors(int n) {
|
||||
std::vector<int> prime_exponent_count;
|
||||
for (int i = 2; i * i <= n; i++)
|
||||
{
|
||||
for (int i = 2; i * i <= n; i++) {
|
||||
int prime_count = 0;
|
||||
while (n % i == 0)
|
||||
{
|
||||
while (n % i == 0) {
|
||||
prime_count += 1;
|
||||
n /= i;
|
||||
}
|
||||
if (prime_count != 0)
|
||||
{
|
||||
if (prime_count != 0) {
|
||||
prime_exponent_count.push_back(prime_count);
|
||||
}
|
||||
}
|
||||
if (n > 1)
|
||||
{
|
||||
if (n > 1) {
|
||||
prime_exponent_count.push_back(1);
|
||||
}
|
||||
|
||||
int divisors_count = 1;
|
||||
|
||||
for (int i = 0; i < prime_exponent_count.size(); i++)
|
||||
{
|
||||
for (int i = 0; i < prime_exponent_count.size(); i++) {
|
||||
divisors_count = divisors_count * (prime_exponent_count[i] + 1);
|
||||
}
|
||||
|
||||
@@ -65,20 +59,15 @@ int number_of_positive_divisors(int n)
|
||||
/**
|
||||
* Main function
|
||||
*/
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int n;
|
||||
std::cin >> n;
|
||||
if (n < 0)
|
||||
{
|
||||
if (n < 0) {
|
||||
n = -n;
|
||||
}
|
||||
if (n == 0)
|
||||
{
|
||||
if (n == 0) {
|
||||
std::cout << "All non-zero numbers are divisors of 0 !" << std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
std::cout << "Number of positive divisors is : ";
|
||||
std::cout << number_of_positive_divisors(n) << std::endl;
|
||||
}
|
||||
|
||||
@@ -22,15 +22,13 @@
|
||||
* @param res large number representation using array
|
||||
* @param res_size number of digits in `res`
|
||||
*/
|
||||
int multiply(int x, int res[], int res_size)
|
||||
{
|
||||
int multiply(int x, int res[], int res_size) {
|
||||
// Initialize carry
|
||||
int carry = 0;
|
||||
|
||||
// One by one multiply n with
|
||||
// individual digits of res[]
|
||||
for (int i = 0; i < res_size; i++)
|
||||
{
|
||||
for (int i = 0; i < res_size; i++) {
|
||||
int prod = res[i] * x + carry;
|
||||
|
||||
// Store last digit of
|
||||
@@ -43,8 +41,7 @@ int multiply(int x, int res[], int res_size)
|
||||
|
||||
// Put carry in res and
|
||||
// increase result size
|
||||
while (carry)
|
||||
{
|
||||
while (carry) {
|
||||
res[res_size] = carry % 10;
|
||||
carry = carry / 10;
|
||||
res_size++;
|
||||
@@ -56,11 +53,9 @@ int multiply(int x, int res[], int res_size)
|
||||
* @param x base
|
||||
* @param n exponent
|
||||
*/
|
||||
void power(int x, int n)
|
||||
{
|
||||
void power(int x, int n) {
|
||||
// printing value "1" for power = 0
|
||||
if (n == 0)
|
||||
{
|
||||
if (n == 0) {
|
||||
std::cout << "1";
|
||||
return;
|
||||
}
|
||||
@@ -70,8 +65,7 @@ void power(int x, int n)
|
||||
int temp = x;
|
||||
|
||||
// Initialize result
|
||||
while (temp != 0)
|
||||
{
|
||||
while (temp != 0) {
|
||||
res[res_size++] = temp % 10;
|
||||
temp = temp / 10;
|
||||
}
|
||||
@@ -85,8 +79,7 @@ void power(int x, int n)
|
||||
}
|
||||
|
||||
/** Main function */
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int exponent, base;
|
||||
std::cout << "Enter base ";
|
||||
std::cin >> base;
|
||||
|
||||
@@ -20,43 +20,35 @@ std::vector<std::pair<int, int>> factors;
|
||||
|
||||
/** Calculating prime number upto a given range
|
||||
*/
|
||||
void SieveOfEratosthenes(int N)
|
||||
{
|
||||
void SieveOfEratosthenes(int N) {
|
||||
// initializes the array isprime
|
||||
memset(isprime, true, sizeof isprime);
|
||||
|
||||
for (int i = 2; i <= N; i++)
|
||||
{
|
||||
if (isprime[i])
|
||||
{
|
||||
for (int i = 2; i <= N; i++) {
|
||||
if (isprime[i]) {
|
||||
for (int j = 2 * i; j <= N; j += i) isprime[j] = false;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 2; i <= N; i++)
|
||||
{
|
||||
for (int i = 2; i <= N; i++) {
|
||||
if (isprime[i])
|
||||
prime_numbers.push_back(i);
|
||||
}
|
||||
}
|
||||
|
||||
/** Prime factorization of a number */
|
||||
void prime_factorization(int num)
|
||||
{
|
||||
void prime_factorization(int num) {
|
||||
int number = num;
|
||||
|
||||
for (int i = 0; prime_numbers[i] <= num; i++)
|
||||
{
|
||||
for (int i = 0; prime_numbers[i] <= num; i++) {
|
||||
int count = 0;
|
||||
|
||||
// termination condition
|
||||
if (number == 1)
|
||||
{
|
||||
if (number == 1) {
|
||||
break;
|
||||
}
|
||||
|
||||
while (number % prime_numbers[i] == 0)
|
||||
{
|
||||
while (number % prime_numbers[i] == 0) {
|
||||
count++;
|
||||
number = number / prime_numbers[i];
|
||||
}
|
||||
@@ -67,8 +59,7 @@ void prime_factorization(int num)
|
||||
}
|
||||
|
||||
/** Main program */
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int num;
|
||||
std::cout << "\t\tComputes the prime factorization\n\n";
|
||||
std::cout << "Type in a number: ";
|
||||
@@ -79,8 +70,7 @@ int main()
|
||||
prime_factorization(num);
|
||||
|
||||
// Prime factors with their powers in the given number in new line
|
||||
for (auto it : factors)
|
||||
{
|
||||
for (auto it : factors) {
|
||||
std::cout << it.first << " " << it.second << std::endl;
|
||||
}
|
||||
|
||||
|
||||
@@ -9,15 +9,12 @@
|
||||
/** Generate an increasingly large number of primes
|
||||
* and store in a list
|
||||
*/
|
||||
std::vector<int> primes(int max)
|
||||
{
|
||||
std::vector<int> primes(int max) {
|
||||
max++;
|
||||
std::vector<int> res;
|
||||
std::vector<bool> numbers(max, false);
|
||||
for (int i = 2; i < max; i++)
|
||||
{
|
||||
if (!numbers[i])
|
||||
{
|
||||
for (int i = 2; i < max; i++) {
|
||||
if (!numbers[i]) {
|
||||
for (int j = i; j < max; j += i) numbers[j] = true;
|
||||
res.push_back(i);
|
||||
}
|
||||
@@ -26,8 +23,7 @@ std::vector<int> primes(int max)
|
||||
}
|
||||
|
||||
/** main function */
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
std::cout << "Calculate primes up to:\n>> ";
|
||||
int n;
|
||||
std::cin >> n;
|
||||
|
||||
@@ -10,15 +10,12 @@
|
||||
char prime[100000000];
|
||||
|
||||
/** Perform Sieve algorithm */
|
||||
void Sieve(int64_t n)
|
||||
{
|
||||
void Sieve(int64_t n) {
|
||||
memset(prime, '1', sizeof(prime)); // intitize '1' to every index
|
||||
prime[0] = '0'; // 0 is not prime
|
||||
prime[1] = '0'; // 1 is not prime
|
||||
for (int p = 2; p * p <= n; p++)
|
||||
{
|
||||
if (prime[p] == '1')
|
||||
{
|
||||
for (int p = 2; p * p <= n; p++) {
|
||||
if (prime[p] == '1') {
|
||||
for (int i = p * p; i <= n; i += p)
|
||||
prime[i] = '0'; // set all multiples of p to false
|
||||
}
|
||||
@@ -26,8 +23,7 @@ void Sieve(int64_t n)
|
||||
}
|
||||
|
||||
/** Main function */
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
Sieve(100000000);
|
||||
int64_t n;
|
||||
std::cin >> n; // 10006187
|
||||
|
||||
@@ -22,16 +22,12 @@ bool isprime[MAX];
|
||||
* This is the function that finds the primes and eliminates
|
||||
* the multiples.
|
||||
*/
|
||||
void sieve(uint32_t N)
|
||||
{
|
||||
void sieve(uint32_t N) {
|
||||
isprime[0] = false;
|
||||
isprime[1] = false;
|
||||
for (uint32_t i = 2; i <= N; i++)
|
||||
{
|
||||
if (isprime[i])
|
||||
{
|
||||
for (uint32_t j = (i << 1); j <= N; j += i)
|
||||
{
|
||||
for (uint32_t i = 2; i <= N; i++) {
|
||||
if (isprime[i]) {
|
||||
for (uint32_t j = (i << 1); j <= N; j += i) {
|
||||
isprime[j] = false;
|
||||
}
|
||||
}
|
||||
@@ -41,12 +37,9 @@ void sieve(uint32_t N)
|
||||
/**
|
||||
* This function prints out the primes to STDOUT
|
||||
*/
|
||||
void print(uint32_t N)
|
||||
{
|
||||
for (uint32_t i = 1; i <= N; i++)
|
||||
{
|
||||
if (isprime[i])
|
||||
{
|
||||
void print(uint32_t N) {
|
||||
for (uint32_t i = 1; i <= N; i++) {
|
||||
if (isprime[i]) {
|
||||
std::cout << i << ' ';
|
||||
}
|
||||
}
|
||||
@@ -56,17 +49,14 @@ void print(uint32_t N)
|
||||
/**
|
||||
* Initialize the array
|
||||
*/
|
||||
void init()
|
||||
{
|
||||
for (uint32_t i = 1; i < MAX; i++)
|
||||
{
|
||||
void init() {
|
||||
for (uint32_t i = 1; i < MAX; i++) {
|
||||
isprime[i] = true;
|
||||
}
|
||||
}
|
||||
|
||||
/** main function */
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
uint32_t N = 100;
|
||||
init();
|
||||
sieve(N);
|
||||
|
||||
@@ -13,10 +13,8 @@
|
||||
/** Bisection method implemented for the function \f$x^2-a=0\f$
|
||||
* whose roots are \f$\pm\sqrt{a}\f$ and only the positive root is returned.
|
||||
*/
|
||||
double Sqrt(double a)
|
||||
{
|
||||
if (a > 0 && a < 1)
|
||||
{
|
||||
double Sqrt(double a) {
|
||||
if (a > 0 && a < 1) {
|
||||
return 1 / Sqrt(1 / a);
|
||||
}
|
||||
double l = 0, r = a;
|
||||
@@ -26,17 +24,12 @@ double Sqrt(double a)
|
||||
double epsilon = 1e-12;
|
||||
*/
|
||||
double epsilon = 1e-12;
|
||||
while (l <= r)
|
||||
{
|
||||
while (l <= r) {
|
||||
double mid = (l + r) / 2;
|
||||
if (mid * mid > a)
|
||||
{
|
||||
if (mid * mid > a) {
|
||||
r = mid;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (a - mid * mid < epsilon)
|
||||
{
|
||||
} else {
|
||||
if (a - mid * mid < epsilon) {
|
||||
return mid;
|
||||
}
|
||||
l = mid;
|
||||
@@ -46,8 +39,7 @@ double Sqrt(double a)
|
||||
}
|
||||
|
||||
/** main function */
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
double n{};
|
||||
std::cin >> n;
|
||||
assert(n >= 0);
|
||||
|
||||
@@ -21,50 +21,41 @@
|
||||
* \param [in] b second number in string to add
|
||||
* \returns sum as a std::string
|
||||
*/
|
||||
std::string add(std::string a, std::string b)
|
||||
{
|
||||
std::string add(std::string a, std::string b) {
|
||||
std::string temp = "";
|
||||
|
||||
// carry flag
|
||||
int carry = 0;
|
||||
|
||||
// fills up with zeros
|
||||
while (a.length() < b.length())
|
||||
{
|
||||
while (a.length() < b.length()) {
|
||||
a = "0" + a;
|
||||
}
|
||||
|
||||
// fills up with zeros
|
||||
while (b.length() < a.length())
|
||||
{
|
||||
while (b.length() < a.length()) {
|
||||
b = "0" + b;
|
||||
}
|
||||
|
||||
// adds the numbers a and b
|
||||
for (int i = a.length() - 1; i >= 0; i--)
|
||||
{
|
||||
for (int i = a.length() - 1; i >= 0; i--) {
|
||||
char val = static_cast<char>(((a[i] - 48) + (b[i] - 48)) + 48 + carry);
|
||||
if (val > 57)
|
||||
{
|
||||
if (val > 57) {
|
||||
carry = 1;
|
||||
val -= 10;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
carry = 0;
|
||||
}
|
||||
temp = val + temp;
|
||||
}
|
||||
|
||||
// processes the carry flag
|
||||
if (carry == 1)
|
||||
{
|
||||
if (carry == 1) {
|
||||
temp = "1" + temp;
|
||||
}
|
||||
|
||||
// removes leading zeros.
|
||||
while (temp[0] == '0' && temp.length() > 1)
|
||||
{
|
||||
while (temp[0] == '0' && temp.length() > 1) {
|
||||
temp = temp.substr(1);
|
||||
}
|
||||
|
||||
@@ -74,13 +65,11 @@ std::string add(std::string a, std::string b)
|
||||
/** Fibonacci iterator
|
||||
* \param [in] n n^th Fibonacci number
|
||||
*/
|
||||
void fib_Accurate(uint64_t n)
|
||||
{
|
||||
void fib_Accurate(uint64_t n) {
|
||||
std::string tmp = "";
|
||||
std::string fibMinus1 = "1";
|
||||
std::string fibMinus2 = "0";
|
||||
for (uint64_t i = 0; i < n; i++)
|
||||
{
|
||||
for (uint64_t i = 0; i < n; i++) {
|
||||
tmp = add(fibMinus1, fibMinus2);
|
||||
fibMinus2 = fibMinus1;
|
||||
fibMinus1 = tmp;
|
||||
@@ -89,8 +78,7 @@ void fib_Accurate(uint64_t n)
|
||||
}
|
||||
|
||||
/** main function */
|
||||
int main()
|
||||
{
|
||||
int main() {
|
||||
int n;
|
||||
std::cout << "Enter whatever number N you want to find the fibonacci of\n";
|
||||
std::cin >> n;
|
||||
|
||||
Reference in New Issue
Block a user