diff --git a/computer_oriented_statistical_methods/Bisection_method.CPP b/computer_oriented_statistical_methods/Bisection_method.CPP index 717987321..266083de1 100644 --- a/computer_oriented_statistical_methods/Bisection_method.CPP +++ b/computer_oriented_statistical_methods/Bisection_method.CPP @@ -1,53 +1,40 @@ -#include -#include -#include +#include +#include -float eq(float i) -{ - return (pow(i, 3) - (4 * i) - 9); // original equation +float eq(float i) { + return (std::pow(i, 3) - (4 * i) - 9); // original equation } -void main() -{ - float a, b, x, z; - clrscr(); - for (int i = 0; i < 100; i++) - { - z = eq(i); - if (z >= 0) - { - b = i; - a = --i; - goto START; - } - } +int main() { + float a, b, x, z; -START: + for (int i = 0; i < 100; i++) { + z = eq(i); + if (z >= 0) { + b = i; + a = --i; + break; + } + } - cout << "\nFirst initial: " << a; - cout << "\nSecond initial: " << b; - for (i = 0; i < 100; i++) - { - x = (a + b) / 2; - z = eq(x); - cout << "\n\nz: " << z << "\t[" << a << " , " << b << " | Bisect: " << x << "]"; + std::cout << "\nFirst initial: " << a; + std::cout << "\nSecond initial: " << b; + for (int i = 0; i < 100; i++) { + x = (a + b) / 2; + z = eq(x); + std::cout << "\n\nz: " << z << "\t[" << a << " , " << b + << " | Bisect: " << x << "]"; - if (z < 0) - { - a = x; - } - else - { - b = x; - } + if (z < 0) { + a = x; + } else { + b = x; + } - if (z > 0 && z < 0.0009) // stoping criteria - { - goto END; - } - } + if (z > 0 && z < 0.0009) // stoping criteria + break; + } -END: - cout << "\n\nRoot: " << x; - getch(); + std::cout << "\n\nRoot: " << x; + return 0; } diff --git a/computer_oriented_statistical_methods/Newton_Raphson.CPP b/computer_oriented_statistical_methods/Newton_Raphson.CPP deleted file mode 100644 index 183a78daf..000000000 --- a/computer_oriented_statistical_methods/Newton_Raphson.CPP +++ /dev/null @@ -1,53 +0,0 @@ -#include -#include -#include - -float eq(float i) -{ - return (pow(i, 3) - (4 * i) - 9); // original equation -} -float eq_der(float i) -{ - return ((3 * pow(i, 2)) - 4); // derivative of equation -} - -void main() -{ - float a, b, z, c, m, n; - clrscr(); - for (int i = 0; i < 100; i++) - { - z = eq(i); - if (z >= 0) - { - b = i; - a = --i; - goto START; - } - } - -START: - - cout << "\nFirst initial: " << a; - cout << "\nSecond initial: " << b; - c = (a + b) / 2; - - for (i = 0; i < 100; i++) - { - float h; - m = eq(c); - n = eq_der(c); - - z = c - (m / n); - c = z; - - if (m > 0 && m < 0.009) // stoping criteria - { - goto END; - } - } - -END: - cout << "\n\nRoot: " << z; - getch(); -} diff --git a/computer_oriented_statistical_methods/Secant_method.CPP b/computer_oriented_statistical_methods/Secant_method.CPP index b897e9184..bd805ed36 100644 --- a/computer_oriented_statistical_methods/Secant_method.CPP +++ b/computer_oriented_statistical_methods/Secant_method.CPP @@ -1,49 +1,37 @@ -#include -#include -#include +#include +#include -float eq(float i) -{ - return (pow(i, 3) - (4 * i) - 9); // original equation +float eq(float i) { + return (pow(i, 3) - (4 * i) - 9); // original equation } -void main() -{ - float a, b, z, c, m, n; - clrscr(); - for (int i = 0; i < 100; i++) - { - z = eq(i); - if (z >= 0) - { - b = i; - a = --i; - goto START; - } - } +int main() { + float a, b, z, c, m, n; + for (int i = 0; i < 100; i++) { + z = eq(i); + if (z >= 0) { + b = i; + a = --i; + break; + } + } -START: + std::cout << "\nFirst initial: " << a; + std::cout << "\nSecond initial: " << b; + for (int i = 0; i < 100; i++) { + float h, d; + m = eq(a); + n = eq(b); - cout << "\nFirst initial: " << a; - cout << "\nSecond initial: " << b; - for (i = 0; i < 100; i++) - { - float h, d; - m = eq(a); - n = eq(b); + c = ((a * n) - (b * m)) / (n - m); + a = b; + b = c; - c = ((a * n) - (b * m)) / (n - m); - a = b; - b = c; + z = eq(c); + if (z > 0 && z < 0.09) // stoping criteria + break; + } - z = eq(c); - if (z > 0 && z < 0.09) // stoping criteria - { - goto END; - } - } - -END: - cout << "\n\nRoot: " << c; - getch(); + std::cout << "\n\nRoot: " << c; + return 0; } diff --git a/computer_oriented_statistical_methods/newton_raphson_method.cpp b/computer_oriented_statistical_methods/newton_raphson_method.cpp new file mode 100644 index 000000000..a2a57768e --- /dev/null +++ b/computer_oriented_statistical_methods/newton_raphson_method.cpp @@ -0,0 +1,41 @@ +#include +#include + +float eq(float i) { + return (std::pow(i, 3) - (4 * i) - 9); // original equation +} +float eq_der(float i) { + return ((3 * std::pow(i, 2)) - 4); // derivative of equation +} + +int main() { + float a, b, z, c, m, n; + + for (int i = 0; i < 100; i++) { + z = eq(i); + if (z >= 0) { + b = i; + a = --i; + break; + } + } + + std::cout << "\nFirst initial: " << a; + std::cout << "\nSecond initial: " << b; + c = (a + b) / 2; + + for (int i = 0; i < 100; i++) { + float h; + m = eq(c); + n = eq_der(c); + + z = c - (m / n); + c = z; + + if (m > 0 && m < 0.009) // stoping criteria + break; + } + + std::cout << "\n\nRoot: " << z << std::endl; + return 0; +} diff --git a/computer_oriented_statistical_methods/successive_approximation.CPP b/computer_oriented_statistical_methods/successive_approximation.CPP index b42ab8f8c..85c42c9fb 100644 --- a/computer_oriented_statistical_methods/successive_approximation.CPP +++ b/computer_oriented_statistical_methods/successive_approximation.CPP @@ -1,37 +1,28 @@ -#include -#include -#include -float eq(float y) -{ - return ((3 * y) - (cos(y)) - 2); + +#include +#include + +float eq(float y) { return ((3 * y) - (cos(y)) - 2); } +float eqd(float y) { return ((0.5) * ((cos(y)) + 2)); } + +int main() { + float y, x1, x2, x3, sum, s, a, f1, f2, gd; + int i, n; + + for (i = 0; i < 10; i++) { + sum = eq(y); + std::cout << "value of equation at " << i << " " << sum << "\n"; + y++; + } + std::cout << "enter the x1->"; + std::cin >> x1; + std::cout << "enter the no iteration to perform->\n"; + std::cin >> n; + + for (i = 0; i <= n; i++) { + x2 = eqd(x1); + std::cout << "\nenter the x2->" << x2; + x1 = x2; + } + return 0; } -float eqd(float y) -{ - return ((0.5) * ((cos(y)) + 2)); -} - -void main() -{ - float y, x1, x2, x3, sum, s, a, f1, f2, gd; - int i, n; - - clrscr(); - for (i = 0; i < 10; i++) - { - sum = eq(y); - cout << "value of equation at " << i << " " << sum << "\n"; - y++; - } - cout << "enter the x1->"; - cin >> x1; - cout << "enter the no iteration to perform->\n"; - cin >> n; - - for (i = 0; i <= n; i++) - { - x2 = eqd(x1); - cout << "\nenter the x2->" << x2; - x1 = x2; - } - getch(); -} \ No newline at end of file diff --git a/math/prime_factorization.cpp b/math/prime_factorization.cpp index 822cad332..bd1b99d97 100644 --- a/math/prime_factorization.cpp +++ b/math/prime_factorization.cpp @@ -1,67 +1,56 @@ +#include +#include #include #include -#include using namespace std; -// Declaring variables for maintaing prime numbers and to check whether a number is prime or not +// Declaring variables for maintaing prime numbers and to check whether a number +// is prime or not bool isprime[1000006]; vector prime_numbers; vector> 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 j = 2 * i; j <= N; j += i) - isprime[j] = false; + 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++) - { - if (isprime[i]) - prime_numbers.push_back(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]; } - if (count) - factors.push_back(make_pair(prime_numbers[i], count)); + if (count) factors.push_back(make_pair(prime_numbers[i], count)); } } /* I added a simple UI. */ -int main() -{ +int main() { int num; cout << "\t\tComputes the prime factorization\n\n"; cout << "Type in a number: "; @@ -72,8 +61,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) { cout << it.first << " " << it.second << endl; }