Relatives POJ - 2407 (欧拉函数)

欧拉函数的定义:

    在数论中,对于正整数N,少于或等于N ([1,N]),且与N互质的正整数(包括1)的个数,记作φ(n)。

     φ函数的值:

    φ(x)=x(1-1/p(1))(1-1/p(2))(1-1/p(3))(1-1/p(4))…..(1-1/p(n)) 其中p(1),p(2)…p(n)为x的所有质因数;x是正整数; φ(1)=1(唯一和1互质的数,且小于等于1)。注意:每种质因数只有一个。

     例如:

         φ(10)=10×(1-1/2)×(1-1/5)=4;

         1 3 7 9

         φ(30)=30×(1-1/2)×(1-1/3)×(1-1/5)=8;

         φ(49)=49×(1-1/7)=42;

 

Given n, a positive integer, how many positive integers less than n are relatively prime to n? Two integers a and b are relatively prime if there are no integers x > 1, y > 0, z > 0 such that a = xy and b = xz.

Input

There are several test cases. For each test case, standard input contains a line with n <= 1,000,000,000. A line containing 0 follows the last case.

Output

For each test case there should be single line of output answering the question posed above.

Sample Input

7
12
0

Sample Output

6
4

给定n个正整数,有多少个小于n的正整数相对于n是质数?如果没有整数x > 1 y > 0 z > 0 a = xy b = xz,则两个整数a和b相对为质数。

输入

有几个测试用例。对于每个测试用例,标准输入包含一行n <= 1,000,000,000。在最后一种情况之后是一行0。

输出

对于每个测试用例,应该只有一行输出来回答上面提出的问题。

样例输入

7

12

0

样例输出

6

4

#include<iostream>
using namespace std;

long long phi(long long x)
{
    int res = x,a = x;
    for(int i=2;i*i<=a;i++)
    {
        if(a%i==0)
        {		
            res=res/i*(i-1); 
			//φ(x)=x(1-1/p(1))(1-1/p(2))…(1-1/p(n))每一步化简得上式 
            while(a%i==0)
            	a/=i; //找最基本的因子,排除重复可能 eg:对12,2与4重复,取2 
        }
    }
    if(a>1) //当a=1 eg:49 81等数字,除了1和它本身,只有一个因子 
    {
    	res=res/a*(a-1);
    }
    return res;
}
int main()
{
	int n;
    while(cin>>n&&n)
    {
        cout<<phi(n)<<endl;
    }
    return 0;
}

详细介绍请看:

https://www.cnblogs.com/handsomecui/p/4755455.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值