Merge branch 'major-corrections-to-files' into cmake

* major-corrections-to-files:
  updating DIRECTORY.md
  made functions static to files *BUG* in CPP lint github action
  fix inttypes
  updating DIRECTORY.md
  filenames and namespace fixes
  updating DIRECTORY.md
  files renamed to standard - without spaces and made CPPLINT compatible
  non compiling and illegible code removed
  updating DIRECTORY.md
  major corrections in the files -  bad CPPLINT and non-compilable codes
  ignore build directory
  file rename to standards
  syntax correction
  cherry pick changes from "cmake branch"
This commit is contained in:
Krishna Vedala 2020-05-25 23:40:54 -04:00
commit a0208cffc3
No known key found for this signature in database
GPG Key ID: BA19ACF8FC8792F7
27 changed files with 429 additions and 463 deletions

View File

@ -10,13 +10,13 @@
* [Sudoku Solve](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/backtracking/sudoku_solve.cpp)
## Computer Oriented Statistical Methods
* [Bisection Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/Bisection_method.CPP)
* [Bisection Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/bisection_method.cpp)
* [False-Position](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/false-position.cpp)
* [Gaussian Elimination](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/Gaussian_elimination.cpp)
* [Newton Raphson](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/Newton_Raphson.CPP)
* [Newton Raphson Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/newton_raphson_method.cpp)
* [Ordinary Least Squares Regressor](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/ordinary_least_squares_regressor.cpp)
* [Secant Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/Secant_method.CPP)
* [Successive Approximation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/successive_approximation.CPP)
* [Secant Method](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/secant_method.cpp)
* [Successive Approximation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/computer_oriented_statistical_methods/successive_approximation.cpp)
## Data Structure
* [Avltree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/data_structure/AVLtree.cpp)
@ -116,7 +116,7 @@
* [Power For Huge Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/power_for_huge_numbers.cpp)
* [Prime Factorization](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/prime_factorization.cpp)
* [Prime Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/prime_numbers.cpp)
* [Primes Up To 10^8](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/primes_up_to_10^8.cpp)
* [Primes Up To Billion](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/primes_up_to_billion.cpp)
* [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/sieve_of_eratosthenes.cpp)
* [Sqrt Double](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/math/sqrt_double.cpp)
@ -133,27 +133,26 @@
## Others
* [Buzz Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Buzz_number.cpp)
* [Decimal To Binary](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Decimal%20To%20Binary.cpp)
* [Decimal To Hexadecimal ](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Decimal%20To%20Hexadecimal%20.cpp)
* [Decimal To Roman Numeral](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Decimal%20to%20Roman%20Numeral.cpp)
* [Decimal To Binary](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/decimal_to_binary.cpp)
* [Decimal To Hexadecimal](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/decimal_to_hexadecimal.cpp)
* [Decimal To Roman Numeral](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/decimal_to_roman_numeral.cpp)
* [Fast Interger Input](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/fast_interger_input.cpp)
* [Fibonacci](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/fibonacci.cpp)
* [Fibonacci Fast](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/fibonacci_fast.cpp)
* [Gcd Of N Numbers](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/GCD_of_n_numbers.cpp)
* [Happy Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/happy_number.cpp)
* [Matrix Exponentiation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/matrix_exponentiation.cpp)
* [Measure Time Elapsed](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/measure_time_elapsed.cpp)
* [Palindromeofnumber](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Palindromeofnumber.cpp)
* [Paranthesis Matching](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Paranthesis%20Matching.cpp)
* [Paranthesis Matching](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/paranthesis_matching.cpp)
* [Pascal Triangle](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/pascal_triangle.cpp)
* [Primality Test](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Primality%20Test.cpp)
* [Primality Test](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/primality_test.cpp)
* [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/sieve_of_Eratosthenes.cpp)
* [Smallest-Circle](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/smallest-circle.cpp)
* [Sparse Matrix](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Sparse%20matrix.cpp)
* [Sparse Matrix](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/sparse_matrix.cpp)
* [Spiral Print](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/spiral_print.cpp)
* [Stairs Pattern](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/stairs_pattern.cpp)
* [Strassen Matrix Multiplication](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Strassen%20Matrix%20Multiplication.cpp)
* [String Fibonacci](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/String%20Fibonacci.cpp)
* [Tower Of Hanoi](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/Tower%20of%20Hanoi.cpp)
* [String Fibonacci](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/string_fibonacci.cpp)
* [Tower Of Hanoi](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/tower_of_hanoi.cpp)
* [Vector Important Functions](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/others/vector_important_functions.cpp)
## Probability
@ -200,6 +199,7 @@
* [Radix Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Radix%20Sort.cpp)
* [Selection Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Selection%20Sort.cpp)
* [Shell Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Shell%20Sort.cpp)
* [Shell Sort2](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/shell_sort2.cpp)
* [Slow Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Slow%20Sort.cpp)
* [Swap Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/swap_sort.cpp)
* [Tim Sort](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/sorting/Tim%20Sort.cpp)

View File

@ -1,53 +0,0 @@
#include <iostream.h>
#include <conio.h>
#include <math.h>
float eq(float i)
{
return (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;
}
}
START:
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 << "]";
if (z < 0)
{
a = x;
}
else
{
b = x;
}
if (z > 0 && z < 0.0009) // stoping criteria
{
goto END;
}
}
END:
cout << "\n\nRoot: " << x;
getch();
}

View File

@ -1,29 +1,23 @@
#include <iostream>
using namespace std;
int main()
{
int main() {
int mat_size, i, j, step;
cout << "Matrix size: ";
cin >> mat_size;
std::cout << "Matrix size: ";
std::cin >> mat_size;
double mat[mat_size + 1][mat_size + 1], x[mat_size][mat_size + 1];
cout << endl
<< "Enter value of the matrix: " << endl;
for (i = 0; i < mat_size; i++)
{
for (j = 0; j <= mat_size; j++)
{
cin >> mat[i][j]; //Enter (mat_size*mat_size) value of the matrix.
std::cout << std::endl << "Enter value of the matrix: " << std::endl;
for (i = 0; i < mat_size; i++) {
for (j = 0; j <= mat_size; j++) {
std::cin >>
mat[i][j]; // Enter (mat_size*mat_size) value of the matrix.
}
}
for (step = 0; step < mat_size - 1; step++)
{
for (i = step; i < mat_size - 1; i++)
{
for (step = 0; step < mat_size - 1; step++) {
for (i = step; i < mat_size - 1; i++) {
double a = (mat[i + 1][step] / mat[step][step]);
for (j = step; j <= mat_size; j++)
@ -31,24 +25,20 @@ int main()
}
}
cout << endl
<< "Matrix using Gaussian Elimination method: " << endl;
for (i = 0; i < mat_size; i++)
{
for (j = 0; j <= mat_size; j++)
{
std::cout << std::endl
<< "Matrix using Gaussian Elimination method: " << std::endl;
for (i = 0; i < mat_size; i++) {
for (j = 0; j <= mat_size; j++) {
x[i][j] = mat[i][j];
cout << mat[i][j] << " ";
std::cout << mat[i][j] << " ";
}
cout << endl;
std::cout << std::endl;
}
cout << endl
<< "Value of the Gaussian Elimination method: " << endl;
for (i = mat_size - 1; i >= 0; i--)
{
std::cout << std::endl
<< "Value of the Gaussian Elimination method: " << std::endl;
for (i = mat_size - 1; i >= 0; i--) {
double sum = 0;
for (j = mat_size - 1; j > i; j--)
{
for (j = mat_size - 1; j > i; j--) {
x[i][j] = x[j][j] * x[i][j];
sum = x[i][j] + sum;
}
@ -57,7 +47,7 @@ int main()
else
x[i][i] = (x[i][mat_size] - sum) / (x[i][i]);
cout << "x" << i << "= " << x[i][i] << endl;
std::cout << "x" << i << "= " << x[i][i] << std::endl;
}
return 0;
}

View File

@ -1,53 +0,0 @@
#include <iostream.h>
#include <conio.h>
#include <math.h>
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();
}

View File

@ -1,49 +0,0 @@
#include <iostream.h>
#include <conio.h>
#include <math.h>
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;
}
}
START:
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;
z = eq(c);
if (z > 0 && z < 0.09) // stoping criteria
{
goto END;
}
}
END:
cout << "\n\nRoot: " << c;
getch();
}

View File

@ -0,0 +1,40 @@
#include <cmath>
#include <iostream>
static float eq(float i) {
return (std::pow(i, 3) - (4 * i) - 9); // original equation
}
int main() {
float a, b, x, z;
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;
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 && z < 0.0009) // stoping criteria
break;
}
std::cout << "\n\nRoot: " << x;
return 0;
}

View File

@ -1,9 +1,11 @@
#include<stdlib.h>
#include <math.h>
#include <cmath>
#include <cstdlib>
#include <iostream>
float eq(float i) {
static float eq(float i) {
return (pow(i, 3) - (4 * i) - 9); // origial equation
}
int main() {
float a, b, z, c, m, n;
system("clear");
@ -12,12 +14,13 @@ int main() {
if (z >= 0) {
b = i;
a = --i;
goto START;
}
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);
@ -26,10 +29,10 @@ int main() {
a = c;
z = eq(c);
if (z > 0 && z < 0.09) { // stoping criteria
goto END;
break;
}
}
END:
std::cout << "\n\nRoot: " << c;
system("pause");
return 0;
}

View File

@ -0,0 +1,42 @@
#include <cmath>
#include <iostream>
static float eq(float i) {
return (std::pow(i, 3) - (4 * i) - 9); // original equation
}
static 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;
}

View File

@ -0,0 +1,37 @@
#include <cmath>
#include <iostream>
static float eq(float i) {
return (pow(i, 3) - (4 * i) - 9); // original 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;
for (int 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;
z = eq(c);
if (z > 0 && z < 0.09) // stoping criteria
break;
}
std::cout << "\n\nRoot: " << c;
return 0;
}

View File

@ -1,37 +0,0 @@
#include <conio.h>
#include <iostream.h>
#include <math.h>
float eq(float y)
{
return ((3 * y) - (cos(y)) - 2);
}
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();
}

View File

@ -0,0 +1,27 @@
#include <cmath>
#include <iostream>
static float eq(float y) { return ((3 * y) - (cos(y)) - 2); }
static 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;
}

View File

@ -1,80 +1,67 @@
#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
#include <algorithm>
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<int> prime_numbers;
vector<pair<int, int>> factors;
std::vector<int> prime_numbers;
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 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(std::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: ";
cin >> num;
std::cout << "\t\tComputes the prime factorization\n\n";
std::cout << "Type in a number: ";
std::cin >> num;
SieveOfEratosthenes(num);
prime_factorization(num);
// Prime factors with their powers in the given number in new line
for (auto it : factors)
{
cout << it.first << " " << it.second << endl;
for (auto it : factors) {
std::cout << it.first << " " << it.second << std::endl;
}
return 0;

View File

@ -1,23 +1,23 @@
//This program aims at calculating the GCD of n numbers by division method
// This program aims at calculating the GCD of n numbers by division method
#include <iostream>
using namepsace std;
int main()
{
cout << "Enter value of n:" << endl;
cin >> n;
int a[n];
int i, j, gcd;
cout << "Enter the n numbers:" << endl;
for (i = 0; i < n; i++)
cin >> a[i];
j = 1; //to access all elements of the array starting from 1
gcd = a[0];
while (j < n)
{
if (a[j] % gcd == 0) //value of gcd is as needed so far
j++; //so we check for next element
else
gcd = a[j] % gcd; //calculating GCD by division method
}
cout << "GCD of entered n numbers:" << gcd;
int main() {
int n;
std::cout << "Enter value of n:" << std::endl;
std::cin >> n;
int *a = new int[n];
int i, j, gcd;
std::cout << "Enter the n numbers:" << std::endl;
for (i = 0; i < n; i++) std::cin >> a[i];
j = 1; // to access all elements of the array starting from 1
gcd = a[0];
while (j < n) {
if (a[j] % gcd == 0) // value of gcd is as needed so far
j++; // so we check for next element
else
gcd = a[j] % gcd; // calculating GCD by division method
}
std::cout << "GCD of entered n numbers:" << gcd;
delete[] a;
return 0;
}

View File

@ -1,56 +0,0 @@
#include <iostream>
using namespace std;
Multiply(int A[][], int B[][], int n)
{
if (n == 2)
{
int p1 = (a[0][0] + a[1][1]) * (b[0][0] + b[1][1]);
int p2 = (a[1][0] + a[1][1]) * b[0][0];
int p3 = a[0][0] * (b[0][1] - b[1][1]);
int p4 = a[1][1] * (b[1][0] - b[0][0]);
int p5 = (a[0][0] + a[0][1]) * b[1][1];
int p6 = (a[1][0] - a[0][0]) * (b[0][0] + b[0][1]);
int p7 = (a[0][1] - a[1][1]) * (b[1][0] + b[1][1]);
int c[n][n];
c[0][0] = p1 + p4 - p5 + p7;
c[0][1] = p3 + p5;
c[1][0] = p2 + p4;
c[1][1] = p1 - p2 + p3 + p6;
return c[][];
}
else
{
}
}
int main()
{
int p, q, r, s;
cout << "Enter the dimensions of Matrices";
cin >> n;
int A[n][n], ;
int B[n][n], ;
cout << "Enter the elements of Matrix A";
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> A[i][j];
}
}
cout << "Enter the elements of Matrix B";
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> B[i][j];
}
}
Multiply(A, B, n);
return 0;
}

View File

@ -1,42 +0,0 @@
//An efficient way to calculate nth fibonacci number faster and simpler than O(nlogn) method of matrix exponentiation
//This works by using both recursion and dynamic programming.
//as 93rd fibonacci exceeds 19 digits, which cannot be stored in a single long long variable, we can only use it till 92nd fibonacci
//we can use it for 10000th fibonacci etc, if we implement bigintegers.
//This algorithm works with the fact that nth fibonacci can easily found if we have already found n/2th or (n+1)/2th fibonacci
//It is a property of fibonacci similar to matrix exponentiation.
#include <iostream>
#include <cstdio>
using namespace std;
const long long MAX = 93;
long long f[MAX] = {0};
long long fib(long long n)
{
if (n == 0)
return 0;
if (n == 1 || n == 2)
return (f[n] = 1);
if (f[n])
return f[n];
long long k = (n % 2 != 0) ? (n + 1) / 2 : n / 2;
f[n] = (n % 2 != 0) ? (fib(k) * fib(k) + fib(k - 1) * fib(k - 1))
: (2 * fib(k - 1) + fib(k)) * fib(k);
return f[n];
}
int main()
{
//Main Function
for (long long i = 1; i < 93; i++)
{
cout << i << " th fibonacci number is " << fib(i) << "\n";
}
return 0;
}

37
others/fibonacci_fast.cpp Normal file
View File

@ -0,0 +1,37 @@
// An efficient way to calculate nth fibonacci number faster and simpler than
// O(nlogn) method of matrix exponentiation This works by using both recursion
// and dynamic programming. as 93rd fibonacci exceeds 19 digits, which cannot be
// stored in a single long long variable, we can only use it till 92nd fibonacci
// we can use it for 10000th fibonacci etc, if we implement bigintegers.
// This algorithm works with the fact that nth fibonacci can easily found if we
// have already found n/2th or (n+1)/2th fibonacci It is a property of fibonacci
// similar to matrix exponentiation.
#include <cinttypes>
#include <cstdio>
#include <iostream>
const uint64_t MAX = 93;
uint64_t f[MAX] = {0};
uint64_t fib(uint64_t n) {
if (n == 0) return 0;
if (n == 1 || n == 2) return (f[n] = 1);
if (f[n]) return f[n];
uint64_t k = (n % 2 != 0) ? (n + 1) / 2 : n / 2;
f[n] = (n % 2 != 0) ? (fib(k) * fib(k) + fib(k - 1) * fib(k - 1))
: (2 * fib(k - 1) + fib(k)) * fib(k);
return f[n];
}
int main() {
// Main Function
for (uint64_t i = 1; i < 93; i++) {
std::cout << i << " th fibonacci number is " << fib(i) << "\n";
}
return 0;
}

View File

@ -19,6 +19,8 @@ The first element of this matrix is the required result.
*/
#include <iostream>
#include <vector>
using std::cin;
using std::cout;
using std::vector;
@ -46,8 +48,7 @@ vector<vector<ll>> multiply(vector<vector<ll>> A, vector<vector<ll>> B) {
// computing power of a matrix
vector<vector<ll>> power(vector<vector<ll>> A, ll p) {
if (p == 1)
return A;
if (p == 1) return A;
if (p % 2 == 1) {
return multiply(A, power(A, p - 1));
} else {
@ -58,14 +59,11 @@ vector<vector<ll>> power(vector<vector<ll>> A, ll p) {
// main function
ll ans(ll n) {
if (n == 0)
return 0;
if (n <= k)
return b[n - 1];
if (n == 0) return 0;
if (n <= k) return b[n - 1];
// F1
vector<ll> F1(k + 1);
for (ll i = 1; i <= k; i++)
F1[i] = b[i - 1];
for (ll i = 1; i <= k; i++) F1[i] = b[i - 1];
// Transpose matrix
vector<vector<ll>> T(k + 1, vector<ll>(k + 1));

View File

@ -1,63 +1,53 @@
#include<iostream>
#include <cstring>
#include <iostream>
using namespace std;
void show_pascal(int **arr, int n)
{
//pint Pascal's Triangle
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n + i; ++j)
{
if (arr[i][j] == 0)
cout << " ";
else
cout << arr[i][j];
}
cout << endl;
}
void show_pascal(int **arr, int n) {
// pint Pascal's Triangle
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n + i; ++j) {
if (arr[i][j] == 0)
std::cout << " ";
else
std::cout << arr[i][j];
}
std::cout << std::endl;
}
}
int **pascal_triangle(int **arr, int n)
{
for (int i = 0; i < n; ++i)
{
for (int j = n - i - 1; j < n + i; ++j)
{
if (j == n - i - 1 || j == n + i - 1)
arr[i][j] = 1; //The edge of the Pascal triangle goes in 1
else
arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j + 1];
}
}
int **pascal_triangle(int **arr, int n) {
for (int i = 0; i < n; ++i) {
for (int j = n - i - 1; j < n + i; ++j) {
if (j == n - i - 1 || j == n + i - 1)
arr[i][j] = 1; // The edge of the Pascal triangle goes in 1
else
arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j + 1];
}
}
return arr;
return arr;
}
int main()
{
int n = 0;
int main() {
int n = 0;
cout << "Set Pascal's Triangle Height" << endl;
cin >> n;
//memory allocation (Assign two-dimensional array to store Pascal triangle)
int **arr = new int*[n];
for (int i = 0; i < n; ++i)
{
arr[i] = new int[2 * n - 1];
memset(arr[i], 0, sizeof(int)*(2 * n - 1));
}
pascal_triangle(arr, n);
show_pascal(arr, n);
std::cout << "Set Pascal's Triangle Height" << std::endl;
std::cin >> n;
//deallocation
for (int i = 0; i < n; ++i)
{
delete[] arr[i];
}
delete[] arr;
// memory allocation (Assign two-dimensional array to store Pascal triangle)
int **arr = new int *[n];
for (int i = 0; i < n; ++i) {
arr[i] = new int[2 * n - 1];
memset(arr[i], 0, sizeof(int) * (2 * n - 1));
}
return 0;
pascal_triangle(arr, n);
show_pascal(arr, n);
// deallocation
for (int i = 0; i < n; ++i) {
delete[] arr[i];
}
delete[] arr;
return 0;
}

105
sorting/shell_sort2.cpp Normal file
View File

@ -0,0 +1,105 @@
#include <array>
#include <cstdlib>
#include <ctime>
#include <iostream>
// for std::swap
#include <utility>
template <class T> void show_data(T *arr, size_t LEN) {
size_t i;
for (i = 0; i < LEN; i++)
std::cout << arr[i] << ", ";
std::cout << std::endl;
}
template <class T, size_t N> void show_data(T (&arr)[N]) { show_data(arr, N); }
/**
* Optimized algorithm - takes half the time by utilizing
* Mar
**/
template <class T> void shell_sort(T *arr, size_t LEN) {
const unsigned int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1};
const unsigned int gap_len = 8;
size_t i, j, g;
for (g = 0; g < gap_len; g++) {
unsigned int gap = gaps[g];
for (i = gap; i < LEN; i++) {
T tmp = arr[i];
for (j = i; j >= gap && (arr[j - gap] - tmp) > 0; j -= gap)
arr[j] = arr[j - gap];
arr[j] = tmp;
}
}
}
template <class T, size_t N> void shell_sort(T (&arr)[N]) {
shell_sort(arr, N);
}
/**
* function to compare sorting using cstdlib's qsort
**/
int compare(const void *a, const void *b) {
int arg1 = *static_cast<const int *>(a);
int arg2 = *static_cast<const int *>(b);
if (arg1 < arg2)
return -1;
if (arg1 > arg2)
return 1;
return 0;
// return (arg1 > arg2) - (arg1 < arg2); // possible shortcut
// return arg1 - arg2; // erroneous shortcut (fails if INT_MIN is present)
}
int main(int argc, char *argv[]) {
int i, NUM_DATA;
if (argc == 2)
NUM_DATA = atoi(argv[1]);
else
NUM_DATA = 200;
// int array = new int[NUM_DATA];
int *data = new int[NUM_DATA];
int *data2 = new int[NUM_DATA];
// int array2 = new int[NUM_DATA];
int range = 1800;
std::srand(time(NULL));
for (i = 0; i < NUM_DATA; i++)
data[i] = data2[i] = (std::rand() % range) - (range >> 1);
std::cout << "Unsorted original data: " << std::endl;
show_data(data, NUM_DATA);
std::clock_t start = std::clock();
shell_sort(data, NUM_DATA);
std::clock_t end = std::clock();
std::cout << std::endl
<< "Data Sorted using custom implementation: " << std::endl;
show_data(data, NUM_DATA);
double elapsed_time = (end - start) * 1.f / CLOCKS_PER_SEC;
std::cout << "Time spent sorting: " << elapsed_time << "s\n" << std::endl;
start = std::clock();
qsort(data2, NUM_DATA, sizeof(data2[0]), compare);
end = std::clock();
std::cout << "Data Sorted using cstdlib qsort: " << std::endl;
show_data(data2, NUM_DATA);
elapsed_time = (end - start) * 1.f / CLOCKS_PER_SEC;
std::cout << "Time spent sorting: " << elapsed_time << "s\n" << std::endl;
free(data);
free(data2);
return 0;
}