-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOula.java
More file actions
64 lines (62 loc) · 1.74 KB
/
Oula.java
File metadata and controls
64 lines (62 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Oula {
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
int num=scanner.nextInt();
int a=num;
double oulaAnwser=0;
ArrayList<Integer> oulaList = new ArrayList<Integer>();
if (isPrime(num)){
oulaAnwser=num-1;
}else{
List<Integer> allPrime = getAllPrime(num);
for(int i : allPrime){
int tem=num;
num=repeatdivide(num,i);
if (tem!=num){
oulaList.add(i);
}
}
oulaAnwser=a;
for (int j :oulaList){
oulaAnwser=oulaAnwser*(1-(double)1/j);
}
}
scanner.close();
System.out.println("欧拉函数的值为"+Math.round(oulaAnwser));
}
public static List<Integer> getAllPrime(int num){
ArrayList<Integer> result = new ArrayList<Integer>();
for (int i =2;i<num;i++){
if (isPrime(i)) {
result.add(i);
}
}
return result;
}
public static boolean isPrime(int num){
if(num < 2) {
return false;
}
for(int i = 2; i <= Math.sqrt(num); i++ ) {
if(num%i == 0) {
return false;
}
}
return true;
}
public static boolean canbedivide(int num,int i ){
return num==1?false:num%i==0?true:false;
}
public static int repeatdivide(int num,int i ){
int result=0;
if (canbedivide(num,i)){
result=repeatdivide(num/i,i);
}else{
return num;
}
return result;
}
}