Split or move java-numbers module

This commit is contained in:
catalin-burcea
2019-09-11 13:16:58 +03:00
parent dc1cd98cd0
commit 9addb86012
15 changed files with 96 additions and 93 deletions

View File

@@ -1,13 +0,0 @@
package com.baeldung.algorithms.primechecker;
import java.math.BigInteger;
public class BigIntegerPrimeChecker implements PrimeChecker<Long>{
@Override
public boolean isPrime(Long number) {
BigInteger bigInt = BigInteger.valueOf(number);
return bigInt.isProbablePrime(100);
}
}

View File

@@ -1,14 +0,0 @@
package com.baeldung.algorithms.primechecker;
import java.util.stream.IntStream;
public class BruteForcePrimeChecker implements PrimeChecker<Integer> {
@Override
public boolean isPrime(Integer number) {
return number > 2 ? IntStream.range(2, number)
.noneMatch(n -> (number % n == 0)) : false;
}
}

View File

@@ -1,13 +0,0 @@
package com.baeldung.algorithms.primechecker;
import java.util.stream.IntStream;
public class OptimisedPrimeChecker implements PrimeChecker<Integer> {
@Override
public boolean isPrime(Integer number) {
return number > 2 ? IntStream.rangeClosed(2, (int) Math.sqrt(number))
.noneMatch(n -> (number % n == 0)) : false;
}
}

View File

@@ -1,6 +0,0 @@
package com.baeldung.algorithms.primechecker;
public interface PrimeChecker <T> {
public boolean isPrime( T number );
}

View File

@@ -1,12 +0,0 @@
package com.baeldung.algorithms.primechecker;
import org.apache.commons.math3.primes.Primes;
public class PrimesPrimeChecker implements PrimeChecker<Integer>{
@Override
public boolean isPrime(Integer number) {
return Primes.isPrime(number);
}
}

View File

@@ -1,26 +0,0 @@
package com.baeldung.area.circle;
public class Circle {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
private double calculateArea() {
return radius * radius * Math.PI;
}
public String toString() {
return "The area of the circle [radius = " + radius + "]: " + calculateArea();
}
}

View File

@@ -1,36 +0,0 @@
package com.baeldung.area.circle;
import java.util.InputMismatchException;
import java.util.Scanner;
public class CircleArea {
public static void main(String[] args) {
if (args.length > 0) {
try {
double radius = Double.parseDouble(args[0]);
calculateArea(radius);
} catch (NumberFormatException nfe) {
System.out.println("Invalid value for radius");
System.exit(0);
}
}
try (Scanner scanner = new Scanner(System.in)) {
System.out.println("Please enter radius value: ");
double radius = scanner.nextDouble();
calculateArea(radius);
} catch (InputMismatchException e) {
System.out.println("Invalid value for radius");
System.exit(0);
}
Circle circle = new Circle(7);
System.out.println(circle);
}
private static void calculateArea(double radius) {
double area = radius * radius * Math.PI;
System.out.println("The area of the circle [radius = " + radius + "]: " + area);
}
}

View File

@@ -1,81 +0,0 @@
package com.baeldung.nan;
/**
* Sample usage of NaN.
*
*/
public class NaNExample {
public static void main(String[] args) {
NaNExample naNExample = new NaNExample();
naNExample.demo();
}
void demo() {
undefined_operations_produce_NaN();
operations_with_no_real_results_produce_NaN();
operations_with_NaN_produce_NaN();
comparison_with_NaN();
check_if_a_value_is_NaN();
assign_NaN_to_missing_values();
}
void undefined_operations_produce_NaN() {
System.out.println("Undefined Operations Produce NaN");
final double ZERO = 0;
System.out.println("ZERO / ZERO = " + (ZERO / ZERO));
System.out.println("INFINITY - INFINITY = " + (Double.POSITIVE_INFINITY - Double.POSITIVE_INFINITY));
System.out.println("INFINITY * ZERO = " + (Double.POSITIVE_INFINITY * ZERO));
System.out.println();
}
void operations_with_no_real_results_produce_NaN() {
System.out.println("Operations with no real results produce NaN");
System.out.println("SQUARE ROOT OF -1 = " + Math.sqrt(-1));
System.out.println("LOG OF -1 = " + Math.log(-1));
System.out.println();
}
void operations_with_NaN_produce_NaN() {
System.out.println("Operations with NaN produce NaN");
System.out.println("2 + NaN = " + (2 + Double.NaN));
System.out.println("2 - NaN = " + (2 - Double.NaN));
System.out.println("2 * NaN = " + (2 * Double.NaN));
System.out.println("2 / NaN = " + (2 / Double.NaN));
System.out.println();
}
void assign_NaN_to_missing_values() {
System.out.println("Assign NaN to Missing values");
double salaryRequired = Double.NaN;
System.out.println(salaryRequired);
System.out.println();
}
void comparison_with_NaN() {
System.out.println("Comparison with NaN");
final double NAN = Double.NaN;
System.out.println("NaN == 1 = " + (NAN == 1));
System.out.println("NaN > 1 = " + (NAN > 1));
System.out.println("NaN < 1 = " + (NAN < 1));
System.out.println("NaN != 1 = " + (NAN != 1));
System.out.println("NaN == NaN = " + (NAN == NAN));
System.out.println("NaN > NaN = " + (NAN > NAN));
System.out.println("NaN < NaN = " + (NAN < NAN));
System.out.println("NaN != NaN = " + (NAN != NAN));
System.out.println();
}
void check_if_a_value_is_NaN() {
System.out.println("Check if a value is NaN");
double x = 1;
System.out.println(x + " is NaN = " + (x != x));
System.out.println(x + " is NaN = " + (Double.isNaN(x)));
x = Double.NaN;
System.out.println(x + " is NaN = " + (x != x));
System.out.println(x + " is NaN = " + (Double.isNaN(x)));
System.out.println();
}
}

View File

@@ -1,19 +0,0 @@
package com.baeldung.pow;
import java.text.DecimalFormat;
public class PowerExample {
public static void main(String[] args) {
int intResult = (int) Math.pow(2, 3);
System.out.println("Math.pow(2, 3) = " + intResult);
double dblResult = Math.pow(4.2, 3);
System.out.println("Math.pow(4.2, 3) = " + Math.pow(4.2, 3));
DecimalFormat df = new DecimalFormat(".00");
System.out.println("Math.pow(4.2, 3) rounded = " + df.format(dblResult));
}
}

View File

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