0

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.

Momo Tontang
  • 113
  • 5
  • 1
    `pow()` returns a `double`, not an `int`. And you cannot use `%` operator with a floating-point type. – Yksisarvinen Oct 12 '18 at 14:57
  • @Yksisarvinen Then can I use `static_cast()` ? – Momo Tontang Oct 12 '18 at 14:59
  • @Momo yes, that'd be one way to fix this: `static_cast(pow(el,i))%c!=1` – scohe001 Oct 12 '18 at 15:02
  • 2
    Yes, but beware that not all integers can be represented with floating point type and vice versa. It might be more worthy to implement an integer version of pow: https://stackoverflow.com/questions/101439/the-most-efficient-way-to-implement-an-integer-based-power-function-powint-int – Yksisarvinen Oct 12 '18 at 15:04
  • @MomoTontang Since I can't post an answer anymore, due to the question being marked as a duplicate, I will write a comment. The cleaner solution, in my opinion, would be to write your own function to raise integers to integer powers, so you wouldn't lose precision, due to conversion. – Algirdas Preidžius Oct 12 '18 at 15:05
  • @AlgirdasPreidžius Ok I think that'll be better because the output I'm getting is not correct – Momo Tontang Oct 12 '18 at 15:08
  • @MomoTontang [Please read this concerning pow](https://stackoverflow.com/questions/25678481/why-does-pown-2-return-24-when-n-5-with-my-compiler-and-os) – PaulMcKenzie Oct 12 '18 at 15:18
  • @PaulMcKenzie Ok I'm getting the correct output now. – Momo Tontang Oct 12 '18 at 15:53

1 Answers1

2

Because you cannot use the % operator with a value of type double, which is what pow() returns.

Andrea Spadaccini
  • 12,378
  • 5
  • 40
  • 54