gpt4 book ai didi

c++ - 在 C++ 的 while 循环中使用 pow() 函数

转载 作者:行者123 更新时间:2023-11-28 01:28:21 24 4
gpt4 key购买 nike

<分区>

注意:该程序与数学中的群论相关,但我需要的是该程序的帮助。如果是题外话,我很抱歉。

因此,任务是编写一个程序,找出用户输入 n 的 U(n) 元素的顺序。

元素 aU(n)={0<a<n: gcd(n,a)=1}

元素的顺序a U(n) 的最小正整数 z这样 (a^z)mod n=1

所以这就是 pow() 的位置while 中的函数循环进来。

我的代码如下:

#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;
}

这就是全部代码。

我得到的错误是:

||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)) ===|

我不是要此代码的替代或更简单版本,而是要解释为什么 pow() while 中的函数循环不工作以及如何使其正确

非常感谢。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com