Merge pull request #7774 from catalin-burcea/BAEL-16629

[BAEL-16629] Split or move java-numbers module
This commit is contained in:
Loredana Crusoveanu 2019-09-12 09:02:16 +03:00 committed by GitHub
commit 71818a79cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 96 additions and 93 deletions

View File

@ -1,2 +1,10 @@
## Relevant Articles ## Relevant Articles
- [Lossy Conversion in Java](https://www.baeldung.com/java-lossy-conversion) - [Lossy Conversion in Java](https://www.baeldung.com/java-lossy-conversion)
- [A Guide to the Java Math Class](https://www.baeldung.com/java-lang-math)
- [Calculate the Area of a Circle in Java](https://www.baeldung.com/java-calculate-circle-area)
- [NaN in Java](http://www.baeldung.com/java-not-a-number)
- [Generating Prime Numbers in Java](http://www.baeldung.com/java-generate-prime-numbers)
- [Using Math.pow in Java](http://www.baeldung.com/java-math-pow)
- [Check If a Number Is Prime in Java](http://www.baeldung.com/java-prime-numbers)
- [Binary Numbers in Java](https://www.baeldung.com/java-binary-numbers)
- [Finding the Least Common Multiple in Java](https://www.baeldung.com/java-least-common-multiple)

View File

@ -1,59 +1,59 @@
package com.baeldung.prime; package com.baeldung.prime;
import java.util.Arrays; import java.util.Arrays;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.IntStream; import java.util.stream.IntStream;
public class PrimeGenerator { public class PrimeGenerator {
public static List<Integer> sieveOfEratosthenes(int n) { public static List<Integer> sieveOfEratosthenes(int n) {
final boolean prime[] = new boolean[n + 1]; final boolean prime[] = new boolean[n + 1];
Arrays.fill(prime, true); Arrays.fill(prime, true);
for (int p = 2; p * p <= n; p++) { for (int p = 2; p * p <= n; p++) {
if (prime[p]) { if (prime[p]) {
for (int i = p * 2; i <= n; i += p) for (int i = p * 2; i <= n; i += p)
prime[i] = false; prime[i] = false;
} }
} }
final List<Integer> primes = new LinkedList<>(); final List<Integer> primes = new LinkedList<>();
for (int i = 2; i <= n; i++) { for (int i = 2; i <= n; i++) {
if (prime[i]) if (prime[i])
primes.add(i); primes.add(i);
} }
return primes; return primes;
} }
public static List<Integer> primeNumbersBruteForce(int max) { public static List<Integer> primeNumbersBruteForce(int max) {
final List<Integer> primeNumbers = new LinkedList<Integer>(); final List<Integer> primeNumbers = new LinkedList<Integer>();
for (int i = 2; i <= max; i++) { for (int i = 2; i <= max; i++) {
if (isPrimeBruteForce(i)) { if (isPrimeBruteForce(i)) {
primeNumbers.add(i); primeNumbers.add(i);
} }
} }
return primeNumbers; return primeNumbers;
} }
private static boolean isPrimeBruteForce(int x) { private static boolean isPrimeBruteForce(int x) {
for (int i = 2; i < x; i++) { for (int i = 2; i < x; i++) {
if (x % i == 0) { if (x % i == 0) {
return false; return false;
} }
} }
return true; return true;
} }
public static List<Integer> primeNumbersTill(int max) { public static List<Integer> primeNumbersTill(int max) {
return IntStream.rangeClosed(2, max) return IntStream.rangeClosed(2, max)
.filter(x -> isPrime(x)) .filter(x -> isPrime(x))
.boxed() .boxed()
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
private static boolean isPrime(int x) { private static boolean isPrime(int x) {
return IntStream.rangeClosed(2, (int) (Math.sqrt(x))) return IntStream.rangeClosed(2, (int) (Math.sqrt(x)))
.allMatch(n -> x % n != 0); .allMatch(n -> x % n != 0);
} }
} }

View File

@ -1,28 +1,29 @@
package com.baeldung.prime; package com.baeldung.prime;
import java.util.Arrays; import org.junit.Test;
import java.util.List;
import org.junit.Test; import java.util.Arrays;
import java.util.List;
import static com.baeldung.prime.PrimeGenerator.*;
import static org.junit.Assert.*; import static com.baeldung.prime.PrimeGenerator.*;
import static org.junit.Assert.*;
public class PrimeGeneratorUnitTest {
@Test public class PrimeGeneratorUnitTest {
public void whenBruteForced_returnsSuccessfully() { @Test
final List<Integer> primeNumbers = primeNumbersBruteForce(20); public void whenBruteForced_returnsSuccessfully() {
assertEquals(Arrays.asList(new Integer[] { 2, 3, 5, 7, 11, 13, 17, 19 }), primeNumbers); final List<Integer> primeNumbers = primeNumbersBruteForce(20);
} assertEquals(Arrays.asList(new Integer[] { 2, 3, 5, 7, 11, 13, 17, 19 }), primeNumbers);
}
@Test
public void whenOptimized_returnsSuccessfully() { @Test
final List<Integer> primeNumbers = primeNumbersTill(20); public void whenOptimized_returnsSuccessfully() {
assertEquals(Arrays.asList(new Integer[] { 2, 3, 5, 7, 11, 13, 17, 19 }), primeNumbers); final List<Integer> primeNumbers = primeNumbersTill(20);
} assertEquals(Arrays.asList(new Integer[] { 2, 3, 5, 7, 11, 13, 17, 19 }), primeNumbers);
}
@Test
public void whenSieveOfEratosthenes_returnsSuccessfully() { @Test
final List<Integer> primeNumbers = sieveOfEratosthenes(20); public void whenSieveOfEratosthenes_returnsSuccessfully() {
assertEquals(Arrays.asList(new Integer[] { 2, 3, 5, 7, 11, 13, 17, 19 }), primeNumbers); final List<Integer> primeNumbers = sieveOfEratosthenes(20);
} assertEquals(Arrays.asList(new Integer[] { 2, 3, 5, 7, 11, 13, 17, 19 }), primeNumbers);
} }
}

View File

@ -4,11 +4,7 @@
### Relevant Articles: ### Relevant Articles:
- [Number of Digits in an Integer in Java](http://www.baeldung.com/java-number-of-digits-in-int) - [Number of Digits in an Integer in Java](http://www.baeldung.com/java-number-of-digits-in-int)
- [NaN in Java](http://www.baeldung.com/java-not-a-number)
- [How to Round a Number to N Decimal Places in Java](http://www.baeldung.com/java-round-decimal-number) - [How to Round a Number to N Decimal Places in Java](http://www.baeldung.com/java-round-decimal-number)
- [Check If a Number Is Prime in Java](http://www.baeldung.com/java-prime-numbers)
- [Using Math.pow in Java](http://www.baeldung.com/java-math-pow)
- [Generating Prime Numbers in Java](http://www.baeldung.com/java-generate-prime-numbers)
- [BigDecimal and BigInteger in Java](http://www.baeldung.com/java-bigdecimal-biginteger) - [BigDecimal and BigInteger in Java](http://www.baeldung.com/java-bigdecimal-biginteger)
- [Find All Pairs of Numbers in an Array That Add Up to a Given Sum](http://www.baeldung.com/java-algorithm-number-pairs-sum) - [Find All Pairs of Numbers in an Array That Add Up to a Given Sum](http://www.baeldung.com/java-algorithm-number-pairs-sum)
- [Java Random Long, Float, Integer and Double](http://www.baeldung.com/java-generate-random-long-float-integer-double) - [Java Random Long, Float, Integer and Double](http://www.baeldung.com/java-generate-random-long-float-integer-double)
@ -17,5 +13,3 @@
- [Calculating the nth Root in Java](https://www.baeldung.com/java-nth-root) - [Calculating the nth Root in Java](https://www.baeldung.com/java-nth-root)
- [Convert Double to String, Removing Decimal Places](https://www.baeldung.com/java-double-to-string) - [Convert Double to String, Removing Decimal Places](https://www.baeldung.com/java-double-to-string)
- [Changing the Order in a Sum Operation Can Produce Different Results?](https://www.baeldung.com/java-floating-point-sum-order) - [Changing the Order in a Sum Operation Can Produce Different Results?](https://www.baeldung.com/java-floating-point-sum-order)
- [Calculate the Area of a Circle in Java](https://www.baeldung.com/java-calculate-circle-area)
- [A Guide to the Java Math Class](https://www.baeldung.com/java-lang-math)