-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrimorialOfANumber.java
45 lines (38 loc) · 1.06 KB
/
PrimorialOfANumber.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
package com.smlnskgmail.jaman.codewarsjava.kyu6;
import java.math.BigInteger;
// https://www.codewars.com/kata/5a99a03e4a6b34bb3c000124
public class PrimorialOfANumber {
private final int number;
public PrimorialOfANumber(int number) {
this.number = number;
}
public String solution() {
BigInteger result = new BigInteger("1");
for (int i = 0, j = 0; i < number; ) {
if (isPrimeNumber(j)) {
result = result.multiply(
new BigInteger(
String.valueOf(j)
)
);
i++;
}
j++;
}
return result.toString();
}
private boolean isPrimeNumber(int number) {
if (number <= 2) {
return number == 2;
}
if (number % 2 == 0) {
return false;
}
for (int i = 3; i * i <= number; i += 2) {
if (number % i == 0) {
return false;
}
}
return true;
}
}