-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDigitPlay.java
63 lines (46 loc) · 1.01 KB
/
DigitPlay.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
//October Easy 2019 : Problem Digital Play
import java.util.HashSet;
import java.util.Scanner;
public class DigitPlay {
static HashSet<Long> primeNumber = new HashSet<Long>();
static boolean isPrime(long n){
int i;
if (n==2)
return true;
if (n%2==0)
return false;
for (i=3;i<=Math.sqrt(n);i+=2)
if (n%i==0)
return false;
return true;
}
static void precompute() {
for(long i=0; i<=90; i++) {
if(isPrime(i)==true)
primeNumber.add(i);
}
}
public static void main(String[] args) {
long a, b, k;
Scanner sc = new Scanner(System.in);
a = sc.nextLong();
b = sc.nextLong();
k = sc.nextLong();
int res = 0;
precompute();
for(long i=a; i<=b; i++) {
long sum = 0;
long temp = i;
while (temp > 0)
{
long digit = temp % 10;
sum = sum + digit;
temp /= 10;
}
//if( (isPrime(sum)==true) && (i%k==0) )
if( (primeNumber.contains(sum)) && (i%k==0) )
res++;
}
System.out.println(res);
}
}