Note: This program is related to Group Theory in mathematics but what I need is help with the program. I am sorry if it is Off-Topic.
So the task is to write a program that will find the order of the elements of U(n) where n is entered by the user.
The elements a
of U(n)={0<a<n: gcd(n,a)=1}
The order of an element a
of U(n) is the least positive integer z
such that
(a^z)mod n=1
So this is where the pow()
function inside the while
loop comes in.
My code is as follows:
#include<iostream>
#include<cmath>
using namespace std;
int gcd(int a, int b) //For finding the gcd to display the elements of U(n)
{
int rem=1,c;
while(rem!=0)
{
rem=a%b;
a=b;
b=rem;
}
return a;
}
int U(int c)
{
int el;
cout<<"U("<<c<<") = {1";
for(int i=2;i<=c;i++) //This is for listing the elements of U(n)
{
if(gcd(c,i)==1)
{
cout<<","<<i;
}
}
cout<<"}"<<endl;
cout<<"Enter the element to check order: "<<endl; //The code for the order of element starts here
cin>>el;
int i=1;
while(pow(el,i)%c!=1) //This is the only part that is showing some error. I want to terminate the program as soon as it encounters the specific i such that (el^i)%c=1
{
i++;
}
cout<<"|"<<el<<"| = "<<i<<endl;
return 0;
}
int main()
{
int num,el;
cout<<"Enter a integer: "<<endl;
cin>>num;
num=abs(num);
U(num);
return 0;
}
So this is the whole code.
The error I am getting is:
||In function 'int U(int)':|
|32|error: invalid operands of types '__gnu_cxx::__promote_2<int, int, double, double>::__type {aka double}' and 'int' to binary 'operator%'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 4 second(s)) ===|
I am not asking for an alternate or a simpler version of this code but rather an explanation to why the pow()
function inside the while
loop is not working and also how to make it right
Thanks a lot in advance.