-
Notifications
You must be signed in to change notification settings - Fork 0
/
PmNumber.java
57 lines (51 loc) · 1.21 KB
/
PmNumber.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
package com.hackrerth;
public class PmNumber {
static int fLimit, sLimit;
//this is the problem in hacker earth to get the prime minister sum ? use the below link to see answers
//https://www.hackerearth.com/practice/basic-programming/complexity-analysis/time-and-space-complexity/practice-problems/algorithm/prime-ministers-number/
static boolean isPrime(int a) {
for (int i = 2; i <= Math.sqrt((double) a); ++i)
if (a % i == 0)
return false;
return true;
}
static int cDigit(int a) {
int b, c;
b = c = 0;
while ((a /= 10) >= 10) {
//a /= 10;
++c;
}
return c;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
fLimit = 10;
sLimit = 1000;
int b, c;
b = c = 0;
for (int i = fLimit; i <= sLimit; ++i)
if (isPrime(i)) {
b = c = 0;
c = i;
if (cDigit(i) > 0) {
while (c >= 10) {
b = b+(c % 10);
c /= 10;
}
if ( isPrime(b+c))
System.out.println(i);
// if(c > 1 && isPrime(b + c))
// System.out.println(i);
}
else {
//System.out.println("in");
b = c % 10;
c /= 10;
if (isPrime(b + c))
System.out.println(" i value is "+i);
}
// System.out.print(b+" "+c);
}
}
}