I want to make a program capable of rising the 2x2 matrix to k power. I know how to make one that is going to square it but once i want to reach higher powers i struggle to save the results I have and use it in the next equation. a,b,c,d are the numbers in the matrix, n is the amount of matrix I will want to do in one time, k is the power i want the matrix to be taken to and m is modulo which I want to use on the numbers. I know there is a method of making this fairly simply but I am unable to figure out a good way to use my results from the equation done before in the next equation.
#include <iostream>
using namespace std;
int mnoz(int a, int b, int c,int d,int m){
int a2 = (a*a + c*b) % m;
int b2 = (a*b + b*d) % m;
int c2 = (c*a + d*c) % m;
int d2 = (c*b + d*d) % m;
return a2, b2, c2, d2;
}
int main()
{
int a, b, c, d, k, m, n;
int e, f, g, h;
int e2, f2, g2, h2;
cin >> n;
// a^k = (a^2)^k/2
for (int i = 0; i<n; i++){
cin >> a >> b >> c >> d >> k >> m;
if (k == 1){
cout << a << " " << b << " " << c << " " << d << endl;
}
else if (k == 2){
e = (a*a + c*b) % m;
f = (a*b + b*d) % m;
g = (c*a + d*c) % m;
h = (c*b + d*d) % m;
cout << e << " " << f << " " << g << " " << h << endl;
}
else{
if (k % 2 == 0){
e = (a*a + c*b) % m;
f = (a*b + b*d) % m;
g = (c*a + d*c) % m;
h = (c*b + d*d) % m;
int z = (k/2)-1;
for (int j = 0; j < z; j++){
int e2 = e;
int f2 = f;
int g2 = g;
int h2 = h;
mnoz(e2, f2, g2, h2, m);
}
cout << e << " " << f << " " << g << " " << h << endl;
}
}
}
system("pause");
return 0;
}
To minimize the number of multiplications, you can use a recursive method.
To give an analogy with real numbers, say you want to compute a^n.
Check whether n is even or odd.
If even, compute b = a^(n/2). Then the final result is b*b.
If odd, compute b = a^((n-1)/2. Then the final result is b*b*a.
Since you are talking matrices instead of numbers, you need to be able multiply any two matrices. You can create a class/struct for the matrix and add an operator*() function.
Here's some sample code.
#include <iostream>
struct Matrix
{
Matrix(double pa, double pb, double pc, double pd) : a(pa), b(pb), c(pc), d(pd) {}
Matrix operator*(Matrix const& rhs) const
{
double an = this->a*rhs.a + this->b*rhs.c;
double bn = this->a*rhs.b + this->b*rhs.d;
double cn = this->c*rhs.a + this->d*rhs.c;
double dn = this->c*rhs.b + this->d*rhs.d;
return Matrix(an, bn, cn, dn);
}
Matrix square() const
{
return (*this)*(*this);
}
double a;
double b;
double c;
double d;
};
Matrix matrixPower(Matrix const& m, int k)
{
if ( k == 1 )
{
return m;
}
Matrix out = matrixPower(m, k/2).square();
if ( k%2 == 1 )
{
return out*m;
}
else
{
return out;
}
}
std::ostream& operator<<(std::ostream& out, Matrix const& m)
{
out << "[ " << m.a << " " << m.b << " ]\n";
out << "[ " << m.c << " " << m.d << " ]\n";
}
int main()
{
Matrix m(0.4, 0.7, 0.7, 0.4);
std::cout << matrixPower(m, 5) << std::endl;
std::cout << matrixPower(m, 10) << std::endl;
std::cout << matrixPower(m, 15) << std::endl;
};
The output:
[ 0.80404 0.80647 ]
[ 0.80647 0.80404 ]
[ 1.29687 1.29687 ]
[ 1.29687 1.29687 ]
[ 2.08862 2.08862 ]
[ 2.08862 2.08862 ]
Related
this is my code for RSA cryptosystem
but I have an error with using the pow function that git an error solution
c = pow(m,e)%n;
and for gcd (greatest common divisor) I'm using #include numeric and algorithm and it does not run
but I create function and will be git a solution
this is a full code
#include<iostream>
#include<cmath>
#include<math.h>
#include <algorithm>
#include<numeric>
using namespace std;
int prime(long int pr)
{
int i;
int j = sqrt(pr);
for (i = 2; i <= j; i++)
{
if (pr % i == 0)
return 0;
}
return 1;
}
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
/*int power(int a, int b) {
float r = 1;
while (b != 0) {
r *= a;
--b;
}
return r;
}
*/
int main() {
int flag,w,k;
cout << "Welcome to RSA Project\n\n ";
cout << "Enter First larg prime for p ";
int p,q,phi,n,e,d;
cin >> p;
flag = prime(p);
if (flag == 0)
{
cout << "\nINVALID INPUT\n";
exit(0);
}
cout << "Enter Second larg prime for q ";
cin >> q;
flag = prime(q);
if (flag == 0)
{
cout << "\nINVALID INPUT\n";
exit(0);
}
//compute n= p*q
n = p * q;
// comput phi
phi = (p - 1) * (q - 1);
//chose e or select randomly e must be gcd(e,phi)=1
w = 2;
while (gcd(w, phi) != 1)
{
w++;
}
e = w;
//chose d from random numbers
k = 1;
while (((k * e) % phi) != 1)
{
k++;
}
d = k;
cout << "\nThe public key is : " << "{ " << n << " , " << e << " }";
cout << "\nThe private key is : " << "{ " << n << " , " << d << " }";
cout << "\n*****\n\n p=" << p << "\nq=" << q <<"\nn="<<n<<"\nphi = " << phi << "\ne = " << e << "\nd = " << d << "\n *****";
//Encryption
cout << " \nEnter the message for Encrypt : ";
int m , c;
cin >> m;
//c = pow(m,e)%n; ((( here an error )))
c = pow(m, e);
c = c% n;
cout << "\n message for encrypt : " << m;
cout << " \nmessage after encrypt : " << c;
//Decryption
m = pow(c , d);
m = c% n;
cout << " \nmessage after decrypt : " << m;
}
output :
output
it's fully tested but the error with pow function ..
if anyone can help me please ..
This code solves the program : Create an algorithm in the form of a flowchart, write and debug the task using recursive and ordinary functions. Corresponding results.
What is dot used for here "x = (1. / 2) * (f + (a / f));" ?
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int k, e, x, a, d, n, f, s;
d = 1;
do {
e = 1;
do {
cout << "Press 1 to use recurrent solution method, press 2 to use nun recurrent solution
method" << endl;
cin >> k;
if (k == 1 || k == 2) e = 2;
} while (e == 1);
switch (k)
{
case 1:
{
cout << "Enter the numder a:" << endl;
cin >> a;
cout << "Enter the numder n:" << endl;
cin >> n;
x = (1. / 2)*(1 + a);
f = x;
s = f;
for (int i = 1; i < n; i++)
{
x = (1. / 2) * (f + (a / f));
s += x;
f = x;
}
cout << "Result: " << s << endl;
}
break;
case 2:
{
cout << " Enter the numder a:" << endl;
cin >> a;
x = sqrt(a);
cout << "Result: " << x << endl;
}
break;
}
cout << "Press 1 to repeat!" << endl;
cin >> d;
} while (d == 1);
}
The dot in "x = (1. / 2) * (f + (a / f));" is used for assuring that the division is interpreted as a float division instead of an integer division, so the result of (1/2) is 0.5 instead of 0.
I am using simpson's 1/3 rule in c++ to find the integral of k*(x*x), where k=2*m and 'm' goes from 1 to 10, hence I have 10 integrals. When I wrote the code below I got the answers but its adding the values of integral from the previous ones! e.g. for m=1 => k=2 the integral is 0.66667, now for m=2 => k=4 instead of getting 0.33333, the integral is 2.00 (0.66667+0.33333). How to prevent it from doing that?
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
int k;
double f(double x) {
return k * pow(x, 2);
}
int main() {
int n = 100000, m;
double h, a = 0, b = 1, sum = 0, x, y;
cout << "The number of sub-intervals is n = " << n << endl;
cout << fixed << showpoint << setprecision(5);
for (m = 1; m <= 10; m++) {
cout << "m = " << m << endl;
k = 2 * m;
cout << "k = " << k << endl;
h = (b - a) / n;
for (int i = 1; i <= n; i++) {
x = a + i * h;
if (i % 2 == 0) {
sum = sum + 2 * f(x);
} else {
sum = sum + 4 * f(x);
}
}
y = h / 3.0 * (f(a) + sum + f(b));
cout << "The integration is: " << y << endl;
}
}
I'm trying to make a quadratic equation solver, but for some reason my program is giving me answers in an unknown format.
I entered the simple quadratic equation x^2 + 2x + 1 = 0, expecting my program to give x = -1 or x = -1, but instead it gave x = 0138151E or x = 0138152D. It looks like it outputs these values for x for any inputs (not recognizing unreal answers and catching them). Why is this and how can I fix it?
#include "../std_lib_facilities_revised.h"
class Imaginary {};
double square(int a)
{
return a * a;
}
double quadratic_solver_pos(int a, int b, int c)
{
double x = 0.0;
double radicand = square(b) - 4 * a * c;
if (radicand < 0) throw Imaginary{};
x = (-b + sqrt(radicand)) / (2 * a);
return x;
}
double quadratic_solver_neg(int a, int b, int c)
{
double x = 0.0;
double radicand = square(b) - 4 * a * c;
if (radicand < 0) throw Imaginary{};
x = (-b - sqrt(radicand)) / (2 * a);
return x;
}
int main()
try {
cout << "This program is a quadratic equation solver.\n";
cout << "Quadratic equations are of the form: ax^2 + bx + c = 0\n";
cout << "Enter a, b, and c, respectively:\n";
double a = 0;
double b = 0;
double c = 0;
cin >> a >> b >> c;
cout << "Your quadratic equation: " << a << "x^2 + " << b << "x + " << c << " = 0\n";
cout << "x = " << quadratic_solver_pos << " or x = " << quadratic_solver_neg << '\n';
}
catch (Imaginary) {
cout << "x is unreal\n";
}
You don't pass your variables to your functions.
You need to do it like so quadratic_solver_pos(a, b, c);.
I am trying to create a c++ program that when I input two numbers (num1, combinationNum), it finds two numbers that multiply together to equal num1, but add together to equal combinationNum. It currently works for positive integers, but not negative. How do I make it work with negative integers? Also, If the equation isn't solvable, I would like it to print an error of some sort. Thanks!
Code:
//
// main.cpp
// Factor
//
// Created by Dani Smith on 2/13/14.
// Copyright (c) 2014 Dani Smith Productions. All rights reserved.
//
#include <iostream>
#include <cmath>
using namespace std;
void factors(int num, int comNum){
int a, b;
cout<<"The factors are ";
bool isPrime = true;
int root = (int)sqrt((double)num);
for(int i = 2; i <= root; i++){
if(num % i == 0 ){
isPrime = false;
//cout<<i<<",";
for(int x = 0; x<3; x++){
if(x==1){
a = i;
}
else if(x == 2){
b = i;
}
if(a + b == comNum){
cout << a << ", and " << b << ".";
}
}
}
}
//----------------------------------------
if(isPrime)cout<<"1 ";
cout<<endl;
}
int main(int argc, const char * argv[])
{
int num1 = 0, num2 = 0, multiple = 0, combinationNum = 0, output1 = 0, output2 = 0;
cout << "What number do you want to factor?\n";
cin >> num1;
cout << "What do you want them to add to?\n";
cin >> combinationNum;
factors(num1, combinationNum);
return 0;
}
To solve:
x + y == a
x * y == b
You have to solve
y == a - x
x * x - a * x + b == 0
So with delta == a * a - 4 * b, if delta positive, the solutions are
x1 = (a + sqrt(delta)) / 2
x2 = (a + sqrt(delta)) / 2
The code : (https://ideone.com/qwrSwa)
void solve(int sum, int mul)
{
std::cout << "solution for x + y = " << sum << std::endl
<< " x * y = " << mul << std::endl;
const int delta = sum * sum - 4 * mul;
if (delta < 0) {
std::cout << "No solution" << std::endl;
return;
}
const float sqrtdelta = sqrtf(delta);
const float x1 = (sum + sqrtdelta) / 2.f;
const float x2 = (sum - sqrtdelta) / 2.f;
std::cout << "x = " << x1 << ", y = " << sum - x1 << std::endl;
if (delta != 0) {
std::cout << "x = " << x2 << ", y = " << sum - x2 << std::endl;
}
}