-
Notifications
You must be signed in to change notification settings - Fork 0
/
BackwardsReadPrimes.java
43 lines (36 loc) · 1.15 KB
/
BackwardsReadPrimes.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
package com.smlnskgmail.jaman.codewarsjava.kyu6;
import java.util.LinkedList;
import java.util.List;
// https://www.codewars.com/kata/5539fecef69c483c5a000015
public class BackwardsReadPrimes {
private final long from;
private final long to;
public BackwardsReadPrimes(long from, long to) {
this.from = from;
this.to = to;
}
public String solution() {
List<String> primeNumbers = new LinkedList<>();
for (long i = from; i <= to; i++) {
long reversedNumber = Long.parseLong(
new StringBuilder(String.valueOf(i))
.reverse()
.toString()
);
if (i != reversedNumber
&& isPrimeNumber(i)
&& isPrimeNumber(reversedNumber)) {
primeNumbers.add(String.valueOf(i));
}
}
return String.join(" ", primeNumbers);
}
private boolean isPrimeNumber(long number) {
for (long i = 2; 2 * i < number; i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
}