-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountCoPrime.java
63 lines (53 loc) · 1.39 KB
/
CountCoPrime.java
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
import java.util.Scanner;
public class CountCoPrime
{
public static void main(String arg[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter number to find number of Co-Prime numbers in range: ");
int n = sc.nextInt();
int prime[] = new int[n-1];
for(int i = 0;i<n-1;i++)
{
prime[i] = i+2;
}
int temp = 0;
for(int i = 0;i<n-1;i++)
{
for(int j = 0;j <n/2;j++)
{
if(prime[i]!=0)
{
temp = prime[i]*prime[i] + j * prime[i];
if(temp<=n)
prime[temp-2] = 0;
else
continue;
}
else
continue;
}
}
for(int i = 0; i<n-1; i++)
{
if(prime[i] == 0)
continue;
else if(n % prime[i] == 0)
continue;
else
prime[i] = 0;
}
double count = n;
for(int i = 0; i<n-1; i++)
{
if(prime[i] != 0)
{
count = count * (1 - ( 1 / (double) prime[i] ));
}
else
continue;
}
System.out.println("The number of Co-Primes: " + (int) count);
sc.close();
}
}