扩展欧几里德算法求乘法逆元(C语言版)

#include <stdio.h>
  int ExtendedEuclid( int f,int d ,int *result);
  int main()
  {
  int x,y,z;
  z = 0;
  printf("输入两个数:\n");
  scanf("%d%d",&x,&y);
  if(ExtendedEuclid(x,y,&z))
  printf("%d和%d互素,乘法的逆元是:%d\n",x,y,z);
  else
  printf("%d和%d不互素,最大公约数为:%d\n",x,y,z);
  return 0;
  }
  int ExtendedEuclid( int f,int d ,int *result)
  {
  int x1,x2,x3,y1,y2,y3,t1,t2,t3,q;
  x1 = y2 = 1;
  x2 = y1 = 0;
  x3 = ( f>=d )?f:d;
  y3 = ( f>=d )?d:f;
  while( 1 )
  {
  if ( y3 == 0 )
  {
  *result = x3; /* 两个数不互素则result为两个数的最大公约数,此时返回值为零 */
  return 0;
  }
  if ( y3 == 1 )
  {
  *result = y2; /* 两个数互素则resutl为其乘法逆元,此时返回值为1 */
  return 1;
  }
  q = x3/y3;
  t1 = x1 - q*y1;
  t2 = x2 - q*y2;
  t3 = x3 - q*y3;
  x1 = y1;
  x2 = y2;
  x3 = y3;
  y1 = t1;
  y2 = t2;
  y3 = t3;
  }
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值