提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供 中文繁体 英文版本 中英对照 版本,有任何建议请联系yoyou2525@163.com。
这个问题已经在这里有了答案:
注意:该程序与数学中的小组理论有关,但我需要该程序的帮助。 很抱歉,如果它是非主题。
因此,任务是编写一个程序,该程序将查找用户输入n的U(n)元素的顺序。
U(n)={0<a<n: gcd(n,a)=1}
的元素a
U(n)={0<a<n: gcd(n,a)=1}
U(n)的元素a
的顺序是最小正整数z
,使得(a^z)mod n=1
这就是while
循环内的pow()
函数进入的地方。
我的代码如下:
#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)) ===|
我不是在寻求该代码的替代版本或更简单的版本,而是要解释为何while
循环内的pow()
函数无法正常工作以及如何使其正确执行
非常感谢。
因为您不能将%
运算符与double
类型的值一起使用,这是pow()
返回的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.