Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<fstream>
- #include<cmath>
- using namespace std;
- void show(int **x, int n)
- {
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++)
- cout << x[i][j] << ' ';
- cout << endl;
- }
- }
- int** create(int n) {
- int **x = new int*[n];
- for (int i = 0; i < n; i++)
- x[i] = new int[n];
- return x;
- }
- int** mass_from_file(ifstream &f, int n) {
- int **x = create(n);
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- f >> x[i][j];
- return x;
- }
- bool check_on_simple(int a) {
- if (a <= 2&&a>0) return true;
- else if (a % 2 == 0) return false;
- else {
- for (int i = 3; i <= (int)sqrt(a); i+=2)
- if (a%i == 0)
- return false;
- }
- return true;
- }
- bool simple_num(int**x, int n) {
- bool k;
- for (int i = 0; i < n; i++) {
- k = true;
- for (int j = 0; j < n&&k; j++) {
- k = false;
- for (int p = 0; p < 4 && !k; p++) {
- k = check_on_simple(x[i][j]+2 - p);
- if (j == n - 1 && k)
- return true;
- }
- }
- }
- return false;
- }
- bool sumFrom5To8(int a) {
- int sum = 0;
- if (5 <= a && a <= 8) return true;
- else if (a > 10) {
- while (a > 10) {
- sum += (a % 10);
- a /= 10;
- }
- sum += a;
- if (5 <= sum && sum <= 8) return true;
- else return false;
- }
- else return false;
- }
- bool sumOfNumbers(int** x, int n) {
- bool t = false;
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n && !t; j++) {
- if (sumFrom5To8(x[i][j]))
- t = true;
- else if (!sumFrom5To8(x[i][j]) && j == n - 1)
- return false;
- }
- return true;
- }
- int MaximumNum(int**x, int n) {
- int maximum = x[0][0];
- for (int i = 0; i < n; i++)
- for (int j = 1; j < n; j++)
- if (maximum < x[i][j]) maximum = x[i][j];
- return maximum;
- }
- bool fibonacchiSearcher(int n) {
- if (n <= 2 && n!=0) return true;
- int x = 1;
- int y = 1;
- int ans = 1;
- while (ans <= n) {
- if (ans == n)
- return true;
- ans = x + y;
- x = y;
- y = ans;
- }
- return false;
- }
- bool fibFunc(int** x, int n) {
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- if (!fibonacchiSearcher(x[i][j])) return false;
- return true;
- }
- bool thePowerOf3(int** x, int n) {
- bool t = false;
- for (int i = 0; i < n && !t; i++)
- for (int j = 0; j < n && !t; j++)
- while (x[i][j] > 1) {
- if (x[i][j] % 3 == 0) {
- x[i][j] /= 3;
- if (x[i][j] == 1)
- t = true;
- }
- else break;
- }
- return false;
- }
- bool isEven(int **x, int n) {
- bool t;
- for (int i = 0; i < n; i++) {
- t = true;
- for (int j = 0; j < n && t; j++) {
- if (x[i][j] % 2 != 0)
- t = false;
- else if (x[i][j] % 2 == 0 && j == n - 1)
- return true;
- }
- }
- return false;
- }
- int** summ(int **a, int **b, int n) {
- int **y = create(n);
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- y[i][j] = a[i][j] + b[i][j];
- return y;
- }
- int** sub(int **a, int **b, int n) {
- int **y = create(n);
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- y[i][j] = a[i][j] - b[i][j];
- return y;
- }
- int** mult(int **a, int **b, int n) {
- int **y = create(n + 1);
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- {
- y[i][j] = 0;
- for (int k = 0; k < n; k++)
- y[i][j] += a[i][k] * b[k][j];
- }
- return y;
- }
- int** mult_on_num(int k, int **a, int n) {
- for (int i = 0; i < n; i++)
- for (int j = 0; j < n; j++)
- a[i][j] *= k;
- return a;
- }
- int** funcForCalculatingC(int**a, int**b, int n) {
- int **c = create(n);
- if (isEven(b, n)) {
- c = mult(summ(a, b, n), sub(b, a, n),n);
- }
- else {
- c = summ(mult(mult_on_num(2, b, n), a, n), a, n);
- }
- return c;
- }
- bool EjAiStratForSimpleNum(int** x, int n) {
- bool t;
- for (int i = 0; i < n; i++) {
- t = true;
- for (int j = 0; j < n && t; j++) {
- if (!check_on_simple(x[j][i]))
- t = false;
- else if (check_on_simple(x[j][i]) && j == n - 1)
- return true;
- }
- }
- return false;
- }
- int main()
- {
- ifstream in1("in1.txt");
- ifstream in2("in2.txt");
- int n;
- cout << "Enter n: "; cin >> n;
- int **a = mass_from_file(in1,n);
- int **b = mass_from_file(in2, n);
- int **c = create(n);
- c = funcForCalculatingC(a, b, n);
- //cout << "Checking on simple number (EA)........ Answer is: " << simple_num(a, n) << endl;
- //cout << "Checking on sums (AE)..........Answer is: " << sumOfNumbers(a, n) << endl;
- //cout << "Checking on fibonacchi (AA).........Answer is: " << fibFunc(a, n) << endl;
- //cout << "Checking on third degree (EE)........Answer is: " << thePowerOf3(a, n) << endl;
- //cout << "Checking on even numbers (EA)........Answer is: " << isEven(a, n) << endl;
- //cout << "Array C is: " << endl;
- //show(c, n);
- //cout << "Checking on simple number (EjAi).........Answer is: " << EjAiStratForSimpleNum(a, n) << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement