Skip to content

Commit 5fcb4d2

Browse files
committed
Optimized prime search utility methods and added timer to FalloffPerlinNoiseTest
1 parent e969cbc commit 5fcb4d2

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

Tests/src/src/john01dav/jnoise/tests/FalloffPerlinNoiseTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
public class FalloffPerlinNoiseTest{
1111

1212
public static void main(String[] args) throws IOException{
13+
long start = System.currentTimeMillis();
1314
HeightMap heightMap = new HeightMap(2048, new PerlinNoiseTemplate(0L, 9, 2, new FalloffPerlinNoiseController()));
1415
heightMap.generate();
16+
System.out.println("Generation Time: " + (System.currentTimeMillis() - start));
17+
1518
heightMap.saveImage("./falloffislandperlinnoise.png");
1619
}
1720

src/src/john01dav/jnoise/util/MathUtil.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ public final class MathUtil{
55
private MathUtil(){}
66

77
public static boolean isPrime(int n){
8-
for(int i=2;i<n;i++){
8+
int sqrtOfN = (int) Math.ceil(Math.sqrt(n));
9+
10+
for(int i=2;i<sqrtOfN;i++){
911
if(n % i == 0){
1012
return false;
1113
}
@@ -15,7 +17,9 @@ public static boolean isPrime(int n){
1517
}
1618

1719
public static int getNextPrime(int n){
18-
while(!isPrime(n)) n++;
20+
if(n % 2 == 0) n++;
21+
22+
while(!isPrime(n)) n+=2;
1923
return n;
2024
}
2125

0 commit comments

Comments
 (0)