forked from anishrauniyar/EasySolutionMUM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPorcupineNumber.java
More file actions
89 lines (77 loc) · 1.79 KB
/
PorcupineNumber.java
File metadata and controls
89 lines (77 loc) · 1.79 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import java.util.Scanner;
/*
* example 139 is a porcupine number because:
a. it is prime
b. it ends in a 9
c. The next prime number after it is 149 which also ends in 9.
Note that 140, 141, 142, 143, 144, 145, 146, 147 and 148 are not prime
so 149 is the next prime number after 139.
*/
public class PorcupineNumber {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number to find first porcupine number: ");
int value = sc.nextInt();
PorcupineNumber.findPorcupineNumber(value);
sc.close();
}
private static void findPorcupineNumber(int value) {
// TODO Auto-generated method stub
int num = value;
int test = 0;
int isPrime = 0;
int isPorcupine = 0;
for (; ; ) {
num++;
test = PorcupineNumber.isPrime(num);
if (test == 1) {
isPrime++;
}
if (isPrime > 0 && num % 10 == 9) {
isPorcupine = PorcupineNumber.isPorcupine(num);
}
if (isPorcupine == 1) {
System.out.println("The next Porcupine number is: " + num);
break;
}
}
}
private static int isPorcupine(int num) {
// TODO Auto-generated method stub
int isPrime = 0;
num = num + 1;
for (; ;) {
isPrime = isPrime(num);
if (isPrime == 1) {
if (num % 10 == 9) {
return 1;
} else {
return 0;
}
}
num++;
}
}
private static int isPrime(int num) {
// TODO Auto-generated method stub
int flag = 0;
if (num == 1 || num == 2) {
return 1;
} else if (num <= 0) {
return 0;
} else if (num > 2) {
for (int i = 2; i < num/2; i++) {
if (num % i == 0) {
flag++;
break;
}
}
if (flag == 0) {
return 1;
} else {
return 0;
}
}
return 0;
}
}