求筛选法欧拉函数 hdu 2824

The Euler function

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2272    Accepted Submission(s): 942


Problem Description
The Euler function phi is an important kind of function in number theory, (n) represents the amount of the numbers which are smaller than n and coprime to n, and this function has a lot of beautiful characteristics. Here comes a very easy question: suppose you are given a, b, try to calculate (a)+ (a+1)+....+ (b)
 

Input
There are several test cases. Each line has two integers a, b (2<a<b<3000000).
 

Output
Output the result of (a)+ (a+1)+....+ (b)
 

Sample Input
  
  
3 100
 

Sample Output
  
  
3042
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
#define N 3000001
__int64 phi[N];
void init()
{
    int i, j;
    for(i = 1; i < N; i++)
        phi[i] = i;

    for(i = 2; i < N; i++)
        if(i == phi[i]) //若i为素数 第一次不是
            for(j = i; j < N; j += i)  //让j累加i
                phi[j] = (phi[j] / i) * (i - 1); //求欧拉函数
}

int main()
{
    init();
    int a, b;
    while(scanf("%d%d", &a, &b) != EOF)
    {
        __int64 ans = 0;
        for(int i = a; i <= b; i++)
            ans += phi[i];
        printf("%I64d\n", ans);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 欧拉函数也称为欧拉-费马函数或费马函数,用符号φ(n)表示,表示小于等于n且与n互质的正整数的个数。 以下是一个用MATLAB编写的欧拉函数的代码示例: ```matlab function result = euler_function(n) result = n; % 初始化结果为n p = 2; % 设置初始的素数为2 % 计算欧拉函数 while p^2 <= n if mod(n, p) == 0 result = result * (1 - 1 / p); while mod(n,p) == 0 n = n / p; end end p = p + 1; end % 处理剩余的质因数 if n > 1 result = result * (1 - 1 / n); end end ``` 使用方法: ```matlab n = 12; % 输入要欧拉函数的数n result = euler_function(n); % 调用欧拉函数的方法 disp(result); % 输出结果 ``` 这个代码通过迭代计算n的所有质因数,并根据欧拉函数的公式进行计算,最终得到欧拉函数的值。 ### 回答2: 欧拉函数(Euler's totient function)是一个计算小于等于给定正整数n的所有与n互质的正整数的个数。以下是用Matlab实现欧拉函数的代码: ```matlab function euler = euler_function(n) euler = n; % 将结果初始化为n p = 2; % 从最小的素数2开始 while p * p <= n % 当p的平方大于n时结束循环 if mod(n, p) == 0 % 若n能整除p euler = euler - euler / p; % 将结果减去n除以p的值(贡献) while mod(n, p) == 0 % 如果n继续能整除p,则继续除以p n = n / p; end end p = p + 1; % 继续查找下一个素数 end if n > 1 % 若n不为1,则n是一个大于sqrt(n)的质数 euler = euler - euler / n; % 将结果减去n除以n的值(贡献) end end ``` 这个函数接受一个正整数n作为输入,并返回n的欧拉函数值。函数首先将结果初始化为n,然后从最小的素数2开始,一直遍历到sqrt(n)为止。如果n能被p整除,它将以p的贡献减少结果。然后继续查找下一个素数。最后,如果n不等于1,则n是一个大于sqrt(n)的质数,将其贡献减少结果。 希望以上的代码可以满足你的需。 ### 回答3: 欧拉函数是数论中一个重要的函数,用于计算小于n且与n互质的正整数的个数。欧拉函数的公式为:φ(n)=n×(1-1/p1)×(1-1/p2)×⋯×(1-1/pk)其中p1,p2,...,pk是n的所有质因数。 下面是欧拉函数的MATLAB代码: ```matlab function result = euler_phi(n) result = n; for i = 2 : round(sqrt(n)) if (n % i == 0) result = result * (1 - 1/i); while (n % i == 0) n = n / i; end end end if (n > 1) result = result * (1 - 1/n); end end ``` 在这段代码中,我们首先将结果初始化为n。然后从2到sqrt(n)遍历,如果n能被i整除,则说明i是n的一个质因数。这时,我们将结果乘以(1-1/i),然后不断将n除以i直到n不能再被i整除为止。最后,如果n大于1,说明n本身也是一个质因数,我们将结果乘以(1-1/n)。 这段代码可以在MATLAB中调用,例如: ```matlab n = 12; result = euler_phi(n); disp(result); ``` 这样就可以得到12的欧拉函数值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值