Repository navigation
NumberTheory, Sequences, Statistics on double[], math and string property tests #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8986229
3e44852
cf32d44
4e027c5
7a66409
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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); | ||
| } | ||
|
|
||
| /** | ||
| * 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 |
|---|---|---|
|
|
@@ -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; | ||
| } | ||
|
|
||
|
|
@@ -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) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 MEDIUM RISK Handle
|
||
| Arrays.sort(data); | ||
|
|
||
| final int middle = data.length / 2; | ||
|
|
@@ -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) { | ||
|
|
@@ -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) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||
| double mean = mean(data); | ||
|
|
||
| double sum = 0; | ||
| for (int value : data) { | ||
| for (double value : data) { | ||
| sum += Math.abs(value - mean); | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
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
fibonaccibefore evaluating either Binet formula.TIPReact 👍 if this review is right, or reply with what's wrong and how it could be better to help improve the reviewer.