Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Requires Java 25. Maven comes with the wrapper.
src/main/java/io/github/diegourban/algorithms/
datastructure/ array (shuffle), binarytree, linkedlist, queue, set, stack, vector
io/ Reader: fast tokenising stdin reader for judge-style input
math/ Algebra (gcd, lcm, primes, factorisation), Combinatorics, Geometry, Numbers, Statistics
math/ NumberTheory (gcd, lcm, primes, factorisation), Sequences (Fibonacci), Combinatorics, Geometry, Measurement, Numbers, Statistics
search/ BinarySearch.indexOf
sorting/ Sorter interface: Insertion, Merge, Quick and Selection sort
string/ StringUtils
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package io.github.diegourban.algorithms.math;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.*;

public class Algebra {
/**
* Number theory: greatest common divisor, least common multiple, primality and prime factorisation.
*/
public class NumberTheory {

/**
* Least common multiple (lcm) of two numbers.
Expand Down Expand Up @@ -137,85 +137,4 @@ private static void fillPrimeFactors(int number, Collection<Integer> factors) {
}
}

/**
* Calculates the nth Fibonacci number.
* Works with n <= 46
* <p>
* Solution: Using a bottom-up approach, starting with the 0th fibonacci number and iteratively computing subsequent numbers until we get to n.
* <p>
* Complexity: N time and O(1) space.
*
* @param n the nth number in the Fibonacci sequence to calculate
* @return the nth Fibonacci number
*/
public static int fibonacci(int n) {
// edge cases:
if (n < 0) {
throw new IllegalArgumentException("Index was negative. No such thing as a negative index in a series.");
} else if (n == 0 || n == 1) {
return n;
}

// we'll be building the fibonacci series from the bottom up
// so we'll need to track the previous 2 numbers at each step
int prevPrev = 0; // 0th fibonacci
int prev = 1; // 1st fibonacci
int current = 0; // Declare and initialize current

for (int i = 1; i < n; i++) {

// Iteration 1: current = 2nd fibonacci
// Iteration 2: current = 3rd fibonacci
// Iteration 3: current = 4th fibonacci
// To get nth fibonacci ... do n-1 iterations.
current = prev + prevPrev;
prevPrev = prev;
prev = current;
}

return current;
}

/**
* Calculates the nth Fibonacci number.
* Works with n <= 67
*
* @param n the nth number in the Fibonacci sequence to calculate
* @return the nth Fibonacci number
*/
public static double fibonacciSimpleBinetFormula(int n) {
if (n == 0) {
return 0;
}
final double phi = (1 + Math.sqrt(5)) / 2;
final double sqrt5 = Math.sqrt(5);
return ((Math.pow(phi, n) - Math.pow(-phi, -n)) / sqrt5);
}

/**
* Calculates the nth Fibonacci number.
* Works with n <= 71
*
* @param n the nth number in the Fibonacci sequence to calculate
* @return the nth Fibonacci number
*/
public static BigInteger fibonacciBinetFormula(int n) {
if (n == 0) {
return new BigInteger("0");
}
if (n == 1 || n == 2) {
return new BigInteger("1");
}
if (n == 3) {
return new BigInteger("2");
}

final double phi = (1 + Math.sqrt(5)) / 2;
BigDecimal sqrt5 = new BigDecimal(Math.sqrt(5));

BigDecimal pow = new BigDecimal(Math.pow(phi, n));
BigDecimal powInvrt = new BigDecimal(Math.pow(-phi, -n));
return pow.subtract(powInvrt).divide(sqrt5, RoundingMode.HALF_UP).toBigInteger();
}

}
96 changes: 96 additions & 0 deletions src/main/java/io/github/diegourban/algorithms/math/Sequences.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package io.github.diegourban.algorithms.math;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;

/**
* Number sequences.
*/
public class Sequences {

private Sequences() {
}

/**
* Calculates the nth Fibonacci number.
* Works with n <= 46
* <p>
* Solution: Using a bottom-up approach, starting with the 0th fibonacci number and iteratively computing subsequent numbers until we get to n.
* <p>
* Complexity: N time and O(1) space.
*
* @param n the nth number in the Fibonacci sequence to calculate
* @return the nth Fibonacci number
*/
public static int fibonacci(int n) {
// edge cases:
if (n < 0) {
throw new IllegalArgumentException("Index was negative. No such thing as a negative index in a series.");
} else if (n == 0 || n == 1) {
return n;
}

// we'll be building the fibonacci series from the bottom up
// so we'll need to track the previous 2 numbers at each step
int prevPrev = 0; // 0th fibonacci
int prev = 1; // 1st fibonacci
int current = 0; // Declare and initialize current

for (int i = 1; i < n; i++) {

// Iteration 1: current = 2nd fibonacci
// Iteration 2: current = 3rd fibonacci
// Iteration 3: current = 4th fibonacci
// To get nth fibonacci ... do n-1 iterations.
current = prev + prevPrev;
prevPrev = prev;
prev = current;
}

return current;
}

/**
* Calculates the nth Fibonacci number.
* Works with n <= 67
*
* @param n the nth number in the Fibonacci sequence to calculate
* @return the nth Fibonacci number
*/
public static double fibonacciSimpleBinetFormula(int n) {
if (n == 0) {
return 0;
}
final double phi = (1 + Math.sqrt(5)) / 2;
final double sqrt5 = Math.sqrt(5);
return ((Math.pow(phi, n) - Math.pow(-phi, -n)) / sqrt5);
Comment on lines +61 to +67

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 MEDIUM RISK

Add the same negative-index validation used by fibonacci before evaluating either Binet formula.

TIP React 👍 if this review is right, or reply with what's wrong and how it could be better to help improve the reviewer.

}

/**
* Calculates the nth Fibonacci number.
* Works with n <= 71
*
* @param n the nth number in the Fibonacci sequence to calculate
* @return the nth Fibonacci number
*/
public static BigInteger fibonacciBinetFormula(int n) {
if (n == 0) {
return new BigInteger("0");
}
if (n == 1 || n == 2) {
return new BigInteger("1");
}
if (n == 3) {
return new BigInteger("2");
}

final double phi = (1 + Math.sqrt(5)) / 2;
BigDecimal sqrt5 = BigDecimal.valueOf(Math.sqrt(5));

BigDecimal pow = BigDecimal.valueOf(Math.pow(phi, n));
BigDecimal powInvrt = BigDecimal.valueOf(Math.pow(-phi, -n));
return pow.subtract(powInvrt).divide(sqrt5, RoundingMode.HALF_UP).toBigInteger();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ public class Statistics {
* @param data
* @return
*/
public static double mean(int[] data) {
public static double mean(double[] data) {
if (data.length == 0) {
return 0.0;
}

double sum = 0.0;
for (int value : data) {
for (double value : data) {
sum += value;
}

Expand All @@ -30,7 +30,7 @@ public static double mean(int[] data) {
* @param data
* @return
*/
public static double median(int[] data) {
public static double median(double[] data) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 MEDIUM RISK

Handle data.length == 0 before indexing, using the intended empty-data contract.

TIP React 👍 if this review is right, or reply with what's wrong and how it could be better to help improve the reviewer.

Arrays.sort(data);

final int middle = data.length / 2;
Expand All @@ -48,8 +48,8 @@ public static double median(int[] data) {
* @param data
* @return
*/
public static int mode(int[] data) {
int maxValue = 0;
public static double mode(double[] data) {
double maxValue = 0;
int maxCount = 0;

for (int i = 0; i < data.length; ++i) {
Expand All @@ -74,11 +74,11 @@ public static int mode(int[] data) {
* @param data
* @return the Average Absolute Deviation from data
*/
public static double meanAbsoluteDeviation(int[] data) {
public static double meanAbsoluteDeviation(double[] data) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 MEDIUM RISK

Define and apply an explicit empty-array result before dividing by data.length.

TIP React 👍 if this review is right, or reply with what's wrong and how it could be better to help improve the reviewer.

double mean = mean(data);

double sum = 0;
for (int value : data) {
for (double value : data) {
sum += Math.abs(value - mean);
}

Expand Down
Loading
Loading