政策资讯

Policy Information


基于java进行欧拉函数算法实现的方法

来源: 重庆市软件正版化服务中心    |    时间: 2024-03-08    |    浏览量: 52611    |   

1.对每个数分解质因数

2.

代码复现:

import java.io.*;

 

public class Main {

    static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));

    static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

 

    public static void main(String[] args) throws IOException {

        int n = Integer.parseInt(in.readLine());

        while (n -- > 0){

            int a = Integer.parseInt(in.readLine());

            int res = a;

            for (int i = 2; i <= a / i; i ++){

                if (a % i == 0){

                    res = res / i * (i - 1);

                    while (a % i == 0) a /= i;

                }

            }

 

            if (a > 1) res = res / a * (a - 1);

 

            out.write(res+"");

            out.newLine();

        }

        out.flush();

    }

}

评论

产品推荐

更多 >

QQ咨询 扫一扫加入群聊,了解更多平台咨询
微信咨询 扫一扫加入群聊,了解更多平台咨询
意见反馈
立即提交
QQ咨询
微信咨询
意见反馈