码迷,mamicode.com
首页 > 编程语言 > 详细

模板C++ 02数论算法 1最大公约数 AND 2素数判断

时间:2017-06-06 14:17:22      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:素数   算法   comm   com   return   bool   c++   str   test   

2.1最大公约数Greatest Common Divisor

补充知识:x*y=最小公倍数*最大公约数

int Euclid(int a,int b)
{
    if(b==0) return a;
    return Euclid(b,a%b);
}

2.2素数判断Prime

#include<cmath>
bool Prime(int n)
{
  int t=sqrt(n);
  for(int i=2;i<=t;i++) if(n%i==0) return false;
  return true;
}

 

模板C++ 02数论算法 1最大公约数 AND 2素数判断

标签:素数   算法   comm   com   return   bool   c++   str   test   

原文地址:http://www.cnblogs.com/Zory/p/6950999.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!