Add java numbers
This commit is contained in:
parent
103e960eda
commit
7d0cf99b3d
|
@ -0,0 +1,8 @@
|
|||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="CompilerConfiguration">
|
||||
<annotationProcessing>
|
||||
<profile name="Maven default annotation processors profile" enabled="true">
|
||||
<sourceOutputDir name="target/generated-sources/annotations" />
|
||||
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
|
||||
<outputRelativeToContentRoot value="true" />
|
||||
<module name="java-numbers-2" />
|
||||
</profile>
|
||||
</annotationProcessing>
|
||||
</component>
|
||||
</project>
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Encoding">
|
||||
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||
</component>
|
||||
</project>
|
|
@ -0,0 +1,20 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="RemoteRepositoriesConfiguration">
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Central Repository" />
|
||||
<option name="url" value="https://repo.maven.apache.org/maven2" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="central" />
|
||||
<option name="name" value="Maven Central repository" />
|
||||
<option name="url" value="https://repo1.maven.org/maven2" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="jboss.community" />
|
||||
<option name="name" value="JBoss Community repository" />
|
||||
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
|
||||
</remote-repository>
|
||||
</component>
|
||||
</project>
|
|
@ -0,0 +1,12 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||
<component name="MavenProjectsManager">
|
||||
<option name="originalFiles">
|
||||
<list>
|
||||
<option value="$PROJECT_DIR$/pom.xml" />
|
||||
</list>
|
||||
</option>
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK" />
|
||||
</project>
|
|
@ -0,0 +1,10 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="RunConfigurationProducerService">
|
||||
<option name="ignoredProducers">
|
||||
<set>
|
||||
<option value="com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunConfigurationProducer" />
|
||||
</set>
|
||||
</option>
|
||||
</component>
|
||||
</project>
|
|
@ -0,0 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||
</component>
|
||||
</project>
|
|
@ -0,0 +1,16 @@
|
|||
## Java Number Cookbooks and Examples
|
||||
|
||||
This module contains articles about numbers in Java.
|
||||
|
||||
### Relevant Articles
|
||||
- [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](https://www.baeldung.com/java-not-a-number)
|
||||
- [Generating Prime Numbers in Java](https://www.baeldung.com/java-generate-prime-numbers)
|
||||
- [Using Math.pow in Java](https://www.baeldung.com/java-math-pow)
|
||||
- [Check If a Number Is Prime in Java](https://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)
|
||||
- [Binary Numbers in Java](https://www.baeldung.com/java-binary-numbers)
|
||||
- More articles: [[<-- prev]](/java-numbers) [[next -->]](/java-numbers-3)
|
|
@ -0,0 +1,45 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>java-numbers-2</artifactId>
|
||||
<version>${parent.version}</version>
|
||||
<name>java-numbers-2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.ossez</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.2-SNAPSHOT</version>
|
||||
<relativePath>../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-generator-annprocess</artifactId>
|
||||
<version>${jmh-generator.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>it.unimi.dsi</groupId>
|
||||
<artifactId>dsiutils</artifactId>
|
||||
<version>${dsiutils.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>java-numbers-2</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<dsiutils.version>2.6.0</dsiutils.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,13 @@
|
|||
package com.ossez.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);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.ossez.algorithms.primechecker;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class BruteForcePrimeChecker implements PrimeChecker<Integer> {
|
||||
|
||||
@Override
|
||||
public boolean isPrime(Integer number) {
|
||||
|
||||
return number > 1 ? IntStream.range(2, number)
|
||||
.noneMatch(n -> (number % n == 0)) : false;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.ossez.algorithms.primechecker;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class OptimisedPrimeChecker implements PrimeChecker<Integer> {
|
||||
|
||||
@Override
|
||||
public boolean isPrime(Integer number) {
|
||||
return number > 1 ? IntStream.rangeClosed(2, (int) Math.sqrt(number))
|
||||
.noneMatch(n -> (number % n == 0)) : false;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package com.ossez.algorithms.primechecker;
|
||||
|
||||
public interface PrimeChecker <T> {
|
||||
|
||||
public boolean isPrime( T number );
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.ossez.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);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
package com.ossez.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();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
package com.ossez.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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,140 @@
|
|||
package com.ossez.binarynumbers;
|
||||
|
||||
public class BinaryNumbers {
|
||||
|
||||
/**
|
||||
* This method takes a decimal number and convert it into a binary number.
|
||||
* example:- input:10, output:1010
|
||||
*
|
||||
* @param decimalNumber
|
||||
* @return binary number
|
||||
*/
|
||||
public Integer convertDecimalToBinary(Integer decimalNumber) {
|
||||
|
||||
if (decimalNumber == 0) {
|
||||
return decimalNumber;
|
||||
}
|
||||
|
||||
StringBuilder binaryNumber = new StringBuilder();
|
||||
Integer quotient = decimalNumber;
|
||||
|
||||
while (quotient > 0) {
|
||||
|
||||
int remainder = quotient % 2;
|
||||
binaryNumber.append(remainder);
|
||||
quotient /= 2;
|
||||
}
|
||||
|
||||
binaryNumber = binaryNumber.reverse();
|
||||
return Integer.valueOf(binaryNumber.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* This method takes a binary number and convert it into a decimal number.
|
||||
* example:- input:101, output:5
|
||||
*
|
||||
* @param binary number
|
||||
* @return decimal Number
|
||||
*/
|
||||
public Integer convertBinaryToDecimal(Integer binaryNumber) {
|
||||
|
||||
Integer decimalNumber = 0;
|
||||
Integer base = 1;
|
||||
|
||||
while (binaryNumber > 0) {
|
||||
|
||||
int lastDigit = binaryNumber % 10;
|
||||
binaryNumber = binaryNumber / 10;
|
||||
|
||||
decimalNumber += lastDigit * base;
|
||||
base = base * 2;
|
||||
}
|
||||
return decimalNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method accepts two binary numbers and returns sum of input numbers.
|
||||
* Example:- firstNum: 101, secondNum: 100, output: 1001
|
||||
*
|
||||
* @param firstNum
|
||||
* @param secondNum
|
||||
* @return addition of input numbers
|
||||
*/
|
||||
public Integer addBinaryNumber(Integer firstNum, Integer secondNum) {
|
||||
|
||||
StringBuilder output = new StringBuilder();
|
||||
|
||||
int carry = 0;
|
||||
int temp;
|
||||
|
||||
while (firstNum != 0 || secondNum != 0) {
|
||||
|
||||
temp = (firstNum % 10 + secondNum % 10 + carry) % 2;
|
||||
output.append(temp);
|
||||
|
||||
carry = (firstNum % 10 + secondNum % 10 + carry) / 2;
|
||||
|
||||
firstNum = firstNum / 10;
|
||||
secondNum = secondNum / 10;
|
||||
}
|
||||
|
||||
if (carry != 0) {
|
||||
output.append(carry);
|
||||
}
|
||||
|
||||
return Integer.valueOf(output.reverse()
|
||||
.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* This method takes two binary number as input and subtract second number from the first number.
|
||||
* example:- firstNum: 1000, secondNum: 11, output: 101
|
||||
* @param firstNum
|
||||
* @param secondNum
|
||||
* @return Result of subtraction of secondNum from first
|
||||
*/
|
||||
public Integer substractBinaryNumber(Integer firstNum, Integer secondNum) {
|
||||
|
||||
int onesComplement = Integer.valueOf(getOnesComplement(secondNum));
|
||||
StringBuilder output = new StringBuilder();
|
||||
int carry = 0;
|
||||
int temp;
|
||||
|
||||
while (firstNum != 0 || onesComplement != 0) {
|
||||
|
||||
temp = (firstNum % 10 + onesComplement % 10 + carry) % 2;
|
||||
output.append(temp);
|
||||
|
||||
carry = (firstNum % 10 + onesComplement % 10 + carry) / 2;
|
||||
|
||||
firstNum = firstNum / 10;
|
||||
onesComplement = onesComplement / 10;
|
||||
}
|
||||
|
||||
String additionOfFirstNumAndOnesComplement = output.reverse()
|
||||
.toString();
|
||||
|
||||
if (carry == 1) {
|
||||
return addBinaryNumber(Integer.valueOf(additionOfFirstNumAndOnesComplement), carry);
|
||||
} else {
|
||||
return getOnesComplement(Integer.valueOf(additionOfFirstNumAndOnesComplement));
|
||||
}
|
||||
}
|
||||
|
||||
public Integer getOnesComplement(Integer num) {
|
||||
|
||||
StringBuilder onesComplement = new StringBuilder();
|
||||
while (num > 0) {
|
||||
int lastDigit = num % 10;
|
||||
if (lastDigit == 0) {
|
||||
onesComplement.append(1);
|
||||
} else {
|
||||
onesComplement.append(0);
|
||||
}
|
||||
num = num / 10;
|
||||
}
|
||||
return Integer.valueOf(onesComplement.reverse()
|
||||
.toString());
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.ossez.lcm;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
public class BigIntegerLCM {
|
||||
|
||||
public static BigInteger lcm(BigInteger number1, BigInteger number2) {
|
||||
BigInteger gcd = number1.gcd(number2);
|
||||
BigInteger absProduct = number1.multiply(number2).abs();
|
||||
return absProduct.divide(gcd);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.ossez.lcm;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class EuclideanAlgorithm {
|
||||
|
||||
public static int gcd(int number1, int number2) {
|
||||
if (number1 == 0 || number2 == 0) {
|
||||
return number1 + number2;
|
||||
} else {
|
||||
int absNumber1 = Math.abs(number1);
|
||||
int absNumber2 = Math.abs(number2);
|
||||
int biggerValue = Math.max(absNumber1, absNumber2);
|
||||
int smallerValue = Math.min(absNumber1, absNumber2);
|
||||
return gcd(biggerValue % smallerValue, smallerValue);
|
||||
}
|
||||
}
|
||||
|
||||
public static int lcm(int number1, int number2) {
|
||||
if (number1 == 0 || number2 == 0)
|
||||
return 0;
|
||||
else {
|
||||
int gcd = gcd(number1, number2);
|
||||
return Math.abs(number1 * number2) / gcd;
|
||||
}
|
||||
}
|
||||
|
||||
public static int lcmForArray(int[] numbers) {
|
||||
int lcm = numbers[0];
|
||||
for (int i = 1; i <= numbers.length - 1; i++) {
|
||||
lcm = lcm(lcm, numbers[i]);
|
||||
}
|
||||
return lcm;
|
||||
}
|
||||
|
||||
public static int lcmByLambda(int... numbers) {
|
||||
return Arrays.stream(numbers).reduce(1, (lcmSoFar, currentNumber) -> Math.abs(lcmSoFar * currentNumber) / gcd(lcmSoFar, currentNumber));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package com.ossez.lcm;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class PrimeFactorizationAlgorithm {
|
||||
|
||||
public static Map<Integer, Integer> getPrimeFactors(int number) {
|
||||
int absNumber = Math.abs(number);
|
||||
Map<Integer, Integer> primeFactorsMap = new HashMap<Integer, Integer>();
|
||||
for (int factor = 2; factor <= absNumber; factor++) {
|
||||
while (absNumber % factor == 0) {
|
||||
Integer power = primeFactorsMap.get(factor);
|
||||
if (power == null) {
|
||||
power = 0;
|
||||
}
|
||||
primeFactorsMap.put(factor, power + 1);
|
||||
absNumber /= factor;
|
||||
}
|
||||
}
|
||||
return primeFactorsMap;
|
||||
}
|
||||
|
||||
public static int lcm(int number1, int number2) {
|
||||
if (number1 == 0 || number2 == 0) {
|
||||
return 0;
|
||||
}
|
||||
Map<Integer, Integer> primeFactorsForNum1 = getPrimeFactors(number1);
|
||||
Map<Integer, Integer> primeFactorsForNum2 = getPrimeFactors(number2);
|
||||
Set<Integer> primeFactorsUnionSet = new HashSet<Integer>(primeFactorsForNum1.keySet());
|
||||
primeFactorsUnionSet.addAll(primeFactorsForNum2.keySet());
|
||||
int lcm = 1;
|
||||
for (Integer primeFactor : primeFactorsUnionSet) {
|
||||
lcm *= Math.pow(primeFactor, Math.max(primeFactorsForNum1.getOrDefault(primeFactor, 0),
|
||||
primeFactorsForNum2.getOrDefault(primeFactor, 0)));
|
||||
}
|
||||
return lcm;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.ossez.lcm;
|
||||
|
||||
public class SimpleAlgorithm {
|
||||
public static int lcm(int number1, int number2) {
|
||||
if (number1 == 0 || number2 == 0) {
|
||||
return 0;
|
||||
}
|
||||
int absNumber1 = Math.abs(number1);
|
||||
int absNumber2 = Math.abs(number2);
|
||||
int absHigherNumber = Math.max(absNumber1, absNumber2);
|
||||
int absLowerNumber = Math.min(absNumber1, absNumber2);
|
||||
int lcm = absHigherNumber;
|
||||
while (lcm % absLowerNumber != 0) {
|
||||
lcm += absHigherNumber;
|
||||
}
|
||||
return lcm;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,81 @@
|
|||
package com.ossez.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();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.ossez.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));
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
package com.ossez.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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
package com.ossez.algorithms.primechecker;
|
||||
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
|
||||
public class PrimeCheckerUnitTest {
|
||||
|
||||
private final BigIntegerPrimeChecker primeChecker = new BigIntegerPrimeChecker();
|
||||
|
||||
@Test
|
||||
public void whenCheckIsPrime_thenTrue() {
|
||||
assertTrue(primeChecker.isPrime(2L));
|
||||
assertTrue(primeChecker.isPrime(13L));
|
||||
assertTrue(primeChecker.isPrime(1009L));
|
||||
assertTrue(primeChecker.isPrime(74207281L));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckIsPrime_thenFalse() {
|
||||
assertFalse(primeChecker.isPrime(50L));
|
||||
assertFalse(primeChecker.isPrime(1001L));
|
||||
assertFalse(primeChecker.isPrime(74207282L));
|
||||
}
|
||||
|
||||
private final BruteForcePrimeChecker bfPrimeChecker = new BruteForcePrimeChecker();
|
||||
|
||||
@Test
|
||||
public void whenBFCheckIsPrime_thenTrue() {
|
||||
assertTrue(bfPrimeChecker.isPrime(2));
|
||||
assertTrue(bfPrimeChecker.isPrime(13));
|
||||
assertTrue(bfPrimeChecker.isPrime(1009));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBFCheckIsPrime_thenFalse() {
|
||||
assertFalse(bfPrimeChecker.isPrime(50));
|
||||
assertFalse(bfPrimeChecker.isPrime(1001));
|
||||
}
|
||||
|
||||
private final OptimisedPrimeChecker optimisedPrimeChecker = new OptimisedPrimeChecker();
|
||||
|
||||
@Test
|
||||
public void whenOptCheckIsPrime_thenTrue() {
|
||||
assertTrue(optimisedPrimeChecker.isPrime(2));
|
||||
assertTrue(optimisedPrimeChecker.isPrime(13));
|
||||
assertTrue(optimisedPrimeChecker.isPrime(1009));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenOptCheckIsPrime_thenFalse() {
|
||||
assertFalse(optimisedPrimeChecker.isPrime(50));
|
||||
assertFalse(optimisedPrimeChecker.isPrime(1001));
|
||||
}
|
||||
|
||||
private final PrimesPrimeChecker primesPrimeChecker = new PrimesPrimeChecker();
|
||||
|
||||
@Test
|
||||
public void whenPrimesCheckIsPrime_thenTrue() {
|
||||
assertTrue(primesPrimeChecker.isPrime(2));
|
||||
assertTrue(primesPrimeChecker.isPrime(13));
|
||||
assertTrue(primesPrimeChecker.isPrime(1009));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPrimesCheckIsPrime_thenFalse() {
|
||||
assertFalse(primesPrimeChecker.isPrime(50));
|
||||
assertFalse(primesPrimeChecker.isPrime(1001));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
package com.ossez.binarynumbers;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class BinaryNumbersUnitTest {
|
||||
|
||||
private BinaryNumbers binaryNumbers = new BinaryNumbers();
|
||||
|
||||
@Test
|
||||
public void given_decimalNumber_then_returnBinaryNumber() {
|
||||
assertEquals(Integer.valueOf(1000), binaryNumbers.convertDecimalToBinary(8));
|
||||
assertEquals(Integer.valueOf(10100), binaryNumbers.convertDecimalToBinary(20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given_decimalNumber_then_convertToBinaryNumber() {
|
||||
assertEquals("1000", Integer.toBinaryString(8));
|
||||
assertEquals("10100", Integer.toBinaryString(20));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given_binaryNumber_then_ConvertToDecimalNumber() {
|
||||
assertEquals(8, Integer.parseInt("1000", 2));
|
||||
assertEquals(20, Integer.parseInt("10100", 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given_binaryNumber_then_returnDecimalNumber() {
|
||||
assertEquals(Integer.valueOf(8), binaryNumbers.convertBinaryToDecimal(1000));
|
||||
assertEquals(Integer.valueOf(20), binaryNumbers.convertBinaryToDecimal(10100));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given_twoBinaryNumber_then_returnAddition() {
|
||||
// adding 4 and 10
|
||||
assertEquals(Integer.valueOf(1110), binaryNumbers.addBinaryNumber(100, 1010));
|
||||
|
||||
// adding 26 and 14
|
||||
assertEquals(Integer.valueOf(101000), binaryNumbers.addBinaryNumber(11010, 1110));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given_twoBinaryNumber_then_returnSubtraction() {
|
||||
// subtracting 16 from 25
|
||||
assertEquals(Integer.valueOf(1001), binaryNumbers.substractBinaryNumber(11001, 10000));
|
||||
|
||||
// subtracting 29 from 16, the output here is negative
|
||||
assertEquals(Integer.valueOf(1101), binaryNumbers.substractBinaryNumber(10000, 11101));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given_binaryLiteral_thenReturnDecimalValue() {
|
||||
|
||||
byte five = 0b101;
|
||||
assertEquals((byte) 5, five);
|
||||
|
||||
short three = 0b11;
|
||||
assertEquals((short) 3, three);
|
||||
|
||||
int nine = 0B1001;
|
||||
assertEquals(9, nine);
|
||||
|
||||
long twentyNine = 0B11101;
|
||||
assertEquals(29, twentyNine);
|
||||
|
||||
int minusThirtySeven = -0B100101;
|
||||
assertEquals(-37, minusThirtySeven);
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,175 @@
|
|||
package com.ossez.java.math;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class MathUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenAbsInteger_thenReturnAbsoluteValue() {
|
||||
assertEquals(5,Math.abs(-5));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMaxBetweenTwoValue_thenReturnMaximum() {
|
||||
assertEquals(10, Math.max(5,10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMinBetweenTwoValue_thenReturnMinimum() {
|
||||
assertEquals(5, Math.min(5,10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSignumWithNegativeNumber_thenReturnMinusOne() {
|
||||
assertEquals(-1, Math.signum(-5), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCopySignWithNegativeSign_thenReturnNegativeArgument() {
|
||||
assertEquals(-5, Math.copySign(5,-1), 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPow_thenReturnPoweredValue() {
|
||||
assertEquals(25, Math.pow(5,2),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSqrt_thenReturnSquareRoot() {
|
||||
assertEquals(5, Math.sqrt(25),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCbrt_thenReturnCubeRoot() {
|
||||
assertEquals(5, Math.cbrt(125),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenExp_thenReturnEulerNumberRaised() {
|
||||
assertEquals(2.718, Math.exp(1),0.1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenExpm1_thenReturnEulerNumberMinusOne() {
|
||||
assertEquals(1.718, Math.expm1(1),0.1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetExponent_thenReturnUnbiasedExponent() {
|
||||
assertEquals(8, Math.getExponent(333.3),0);
|
||||
assertEquals(7, Math.getExponent(222.2f),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLog_thenReturnValue() {
|
||||
assertEquals(1, Math.log(Math.E),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLog10_thenReturnValue() {
|
||||
assertEquals(1, Math.log10(10),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLog1p_thenReturnValue() {
|
||||
assertEquals(1.31, Math.log1p(Math.E),0.1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSin_thenReturnValue() {
|
||||
assertEquals(1, Math.sin(Math.PI/2),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCos_thenReturnValue() {
|
||||
assertEquals(1, Math.cos(0),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenTan_thenReturnValue() {
|
||||
assertEquals(1, Math.tan(Math.PI/4),0.1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAsin_thenReturnValue() {
|
||||
assertEquals(Math.PI/2, Math.asin(1),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAcos_thenReturnValue() {
|
||||
assertEquals(Math.PI/2, Math.acos(0),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAtan_thenReturnValue() {
|
||||
assertEquals(Math.PI/4, Math.atan(1),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAtan2_thenReturnValue() {
|
||||
assertEquals(Math.PI/4, Math.atan2(1,1),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenToDegrees_thenReturnValue() {
|
||||
assertEquals(180, Math.toDegrees(Math.PI),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenToRadians_thenReturnValue() {
|
||||
assertEquals(Math.PI, Math.toRadians(180),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCeil_thenReturnValue() {
|
||||
assertEquals(4, Math.ceil(Math.PI),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFloor_thenReturnValue() {
|
||||
assertEquals(3, Math.floor(Math.PI),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetExponent_thenReturnValue() {
|
||||
assertEquals(8, Math.getExponent(333.3),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenIEEERemainder_thenReturnValue() {
|
||||
assertEquals(1.0, Math.IEEEremainder(5,2),0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNextAfter_thenReturnValue() {
|
||||
assertEquals(1.9499999284744263, Math.nextAfter(1.95f,1),0.0000001);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNextUp_thenReturnValue() {
|
||||
assertEquals(1.9500002, Math.nextUp(1.95f),0.0000001);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRint_thenReturnValue() {
|
||||
assertEquals(2.0, Math.rint(1.95f),0.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRound_thenReturnValue() {
|
||||
assertEquals(2.0, Math.round(1.95f),0.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenScalb_thenReturnValue() {
|
||||
assertEquals(48, Math.scalb(3, 4),0.0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenHypot_thenReturnValue() {
|
||||
assertEquals(5, Math.hypot(4, 3),0.0);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.ossez.lcm;
|
||||
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
public class BigIntegerLCMUnitTest {
|
||||
|
||||
@Test
|
||||
public void testLCM() {
|
||||
BigInteger number1 = new BigInteger("12");
|
||||
BigInteger number2 = new BigInteger("18");
|
||||
BigInteger expectedLCM = new BigInteger("36");
|
||||
Assert.assertEquals(expectedLCM, BigIntegerLCM.lcm(number1, number2));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,27 @@
|
|||
package com.ossez.lcm;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class EuclideanAlgorithmUnitTest {
|
||||
|
||||
@Test
|
||||
public void testGCD() {
|
||||
Assert.assertEquals(6, EuclideanAlgorithm.gcd(12, 18));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLCM() {
|
||||
Assert.assertEquals(36, EuclideanAlgorithm.lcm(12, 18));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLCMForArray() {
|
||||
Assert.assertEquals(15, EuclideanAlgorithm.lcmForArray(new int[]{3, 5, 15}));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLCMByLambdaForArray() {
|
||||
Assert.assertEquals(15, EuclideanAlgorithm.lcmByLambda(new int[]{3, 5, 15}));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.ossez.lcm;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
public class PrimeFactorizationAlgorithmUnitTest {
|
||||
|
||||
@Test
|
||||
public void testGetPrimeFactors() {
|
||||
Map<Integer, Integer> expectedPrimeFactorsMapForTwelve = new HashMap<>();
|
||||
expectedPrimeFactorsMapForTwelve.put(2, 2);
|
||||
expectedPrimeFactorsMapForTwelve.put(3, 1);
|
||||
Map<Integer, Integer> expectedPrimeFactorsMapForEighteen = new HashMap<>();
|
||||
expectedPrimeFactorsMapForEighteen.put(2, 1);
|
||||
expectedPrimeFactorsMapForEighteen.put(3, 2);
|
||||
Assert.assertEquals(expectedPrimeFactorsMapForTwelve, getPrimeFactors(12));
|
||||
Assert.assertEquals(expectedPrimeFactorsMapForEighteen, getPrimeFactors(18));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLCM() {
|
||||
Assert.assertEquals(36, PrimeFactorizationAlgorithm.lcm(12, 18));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.ossez.lcm;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class SimpleAlgorithmUnitTest {
|
||||
|
||||
@Test
|
||||
public void testLCM() {
|
||||
Assert.assertEquals(36, lcm(12, 18));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
package com.ossez.lossyconversion;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class ConversionTechniquesUnitTest {
|
||||
|
||||
@Test
|
||||
public void testPrimitiveConversion() {
|
||||
|
||||
long longNum = 24;
|
||||
short shortNum = (short) longNum;
|
||||
assertEquals(24, shortNum);
|
||||
|
||||
double doubleNum = 15.6;
|
||||
int integerNum = (int) doubleNum;
|
||||
assertEquals(15, integerNum);
|
||||
|
||||
long largeLongNum = 32768;
|
||||
short minShortNum = (short) largeLongNum;
|
||||
assertEquals(-32768, minShortNum);
|
||||
|
||||
long smallLongNum = -32769;
|
||||
short maxShortNum = (short) smallLongNum;
|
||||
assertEquals(32767, maxShortNum);
|
||||
|
||||
long maxLong = Long.MAX_VALUE;
|
||||
int minInt = (int) maxLong;
|
||||
assertEquals(-1, minInt);
|
||||
|
||||
long minLong = Long.MIN_VALUE;
|
||||
int maxInt = (int) minLong;
|
||||
assertEquals(0, maxInt);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWrapperToPrimitiveConversion() {
|
||||
|
||||
Float floatNum = 17.564f;
|
||||
long longNum = floatNum.longValue();
|
||||
assertEquals(17, longNum);
|
||||
|
||||
Double doubleNum = 15.9999;
|
||||
longNum = doubleNum.longValue();
|
||||
assertEquals(15, longNum);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWrapperToPrimitiveConversionUsingMathRound() {
|
||||
|
||||
Double doubleNum = 15.9999;
|
||||
long longNum = Math.round(doubleNum);
|
||||
assertEquals(16, longNum);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWrapperConversion() {
|
||||
|
||||
Double doubleNum = 10.3;
|
||||
double dbl = doubleNum.doubleValue(); //unboxing
|
||||
int intgr = (int) dbl; //downcasting
|
||||
Integer intNum = Integer.valueOf(intgr);
|
||||
assertEquals(Integer.valueOf(10), intNum);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
package com.ossez.prime;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static com.ossez.prime.PrimeGenerator.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class PrimeGeneratorUnitTest {
|
||||
@Test
|
||||
public void whenBruteForced_returnsSuccessfully() {
|
||||
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() {
|
||||
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() {
|
||||
final List<Integer> primeNumbers = sieveOfEratosthenes(20);
|
||||
assertEquals(Arrays.asList(new Integer[] { 2, 3, 5, 7, 11, 13, 17, 19 }), primeNumbers);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
package com.ossez.primechecker;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class PrimeCheckerUnitTest {
|
||||
|
||||
private final BigIntegerPrimeChecker primeChecker = new BigIntegerPrimeChecker();
|
||||
|
||||
@Test
|
||||
public void whenCheckIsPrime_thenTrue() {
|
||||
assertTrue(primeChecker.isPrime(2L));
|
||||
assertTrue(primeChecker.isPrime(13L));
|
||||
assertTrue(primeChecker.isPrime(1009L));
|
||||
assertTrue(primeChecker.isPrime(74207281L));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCheckIsPrime_thenFalse() {
|
||||
assertFalse(primeChecker.isPrime(50L));
|
||||
assertFalse(primeChecker.isPrime(1001L));
|
||||
assertFalse(primeChecker.isPrime(74207282L));
|
||||
}
|
||||
|
||||
private final BruteForcePrimeChecker bfPrimeChecker = new BruteForcePrimeChecker();
|
||||
|
||||
@Test
|
||||
public void whenBFCheckIsPrime_thenTrue() {
|
||||
assertTrue(bfPrimeChecker.isPrime(2));
|
||||
assertTrue(bfPrimeChecker.isPrime(13));
|
||||
assertTrue(bfPrimeChecker.isPrime(1009));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBFCheckIsPrime_thenFalse() {
|
||||
assertFalse(bfPrimeChecker.isPrime(50));
|
||||
assertFalse(bfPrimeChecker.isPrime(1001));
|
||||
}
|
||||
|
||||
private final OptimisedPrimeChecker optimisedPrimeChecker = new OptimisedPrimeChecker();
|
||||
|
||||
@Test
|
||||
public void whenOptCheckIsPrime_thenTrue() {
|
||||
assertTrue(optimisedPrimeChecker.isPrime(2));
|
||||
assertTrue(optimisedPrimeChecker.isPrime(13));
|
||||
assertTrue(optimisedPrimeChecker.isPrime(1009));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenOptCheckIsPrime_thenFalse() {
|
||||
assertFalse(optimisedPrimeChecker.isPrime(50));
|
||||
assertFalse(optimisedPrimeChecker.isPrime(1001));
|
||||
}
|
||||
|
||||
private final PrimesPrimeChecker primesPrimeChecker = new PrimesPrimeChecker();
|
||||
|
||||
@Test
|
||||
public void whenPrimesCheckIsPrime_thenTrue() {
|
||||
assertTrue(primesPrimeChecker.isPrime(2));
|
||||
assertTrue(primesPrimeChecker.isPrime(13));
|
||||
assertTrue(primesPrimeChecker.isPrime(1009));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPrimesCheckIsPrime_thenFalse() {
|
||||
assertFalse(primesPrimeChecker.isPrime(50));
|
||||
assertFalse(primesPrimeChecker.isPrime(1001));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue