From c39940584097f4a8173e86aaed04752a0947e929 Mon Sep 17 00:00:00 2001 From: U7112358 Date: Sun, 30 Oct 2022 22:12:11 +1100 Subject: [PATCH 1/2] Added Armstrong number algorithm and tests for it --- .../algorithms/math/ArmstrongNumber.java | 23 +++++++++++++++++++ .../algorithms/math/ArmstrongNumberTest.java | 19 +++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 src/main/java/com/williamfiset/algorithms/math/ArmstrongNumber.java create mode 100644 src/test/java/com/williamfiset/algorithms/math/ArmstrongNumberTest.java diff --git a/src/main/java/com/williamfiset/algorithms/math/ArmstrongNumber.java b/src/main/java/com/williamfiset/algorithms/math/ArmstrongNumber.java new file mode 100644 index 000000000..ceefd9538 --- /dev/null +++ b/src/main/java/com/williamfiset/algorithms/math/ArmstrongNumber.java @@ -0,0 +1,23 @@ +package com.williamfiset.algorithms.math; + +public class ArmstrongNumber { + /* + * An Armstrong number is the one where + * the sum of each digit of the number to the + * power of number of the digits in that number + * will be equal to that number for example + * 153 (n = 3 digits) is equal to: + * 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153 + */ + public static boolean isArmstrongNumber(int number){ + int numberLength = (String.valueOf(number)).length(); + int tracker = number; + int answer = 0; + while (tracker !=0){ + int lastDigit = (int) tracker % 10; + answer = answer + (int) Math.pow(lastDigit , numberLength); + tracker = Math.floorDiv(tracker , 10); + } + return (answer == number); + } +} diff --git a/src/test/java/com/williamfiset/algorithms/math/ArmstrongNumberTest.java b/src/test/java/com/williamfiset/algorithms/math/ArmstrongNumberTest.java new file mode 100644 index 000000000..8ec08ce4f --- /dev/null +++ b/src/test/java/com/williamfiset/algorithms/math/ArmstrongNumberTest.java @@ -0,0 +1,19 @@ +package com.williamfiset.algorithms.math; + +import static com.williamfiset.algorithms.math.ArmstrongNumber.isArmstrongNumber; +import static org.junit.jupiter.api.Assertions.*; +import org.junit.*; + + +public class ArmstrongNumberTest { + @Test + public void armstrongNumberTest(){ + assertTrue(isArmstrongNumber(1)==true); + assertTrue(isArmstrongNumber(153)==true); + assertTrue(isArmstrongNumber(1634)==true); + assertTrue(isArmstrongNumber(371)==true); + assertFalse(isArmstrongNumber(10)==true); + assertFalse(isArmstrongNumber(100)==true); + assertFalse(isArmstrongNumber(1000)==true); + } +} \ No newline at end of file From 0b9e5a0161c3673718cefccdd89f6f64e5852ad8 Mon Sep 17 00:00:00 2001 From: U7112358 Date: Sun, 30 Oct 2022 23:23:52 +1100 Subject: [PATCH 2/2] #338 added standard deviation and tests for it --- .../algorithms/math/StandardDeviation.java | 17 +++++++++++++++++ .../math/StandardDeviationTest.java | 19 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/main/java/com/williamfiset/algorithms/math/StandardDeviation.java create mode 100644 src/test/java/com/williamfiset/algorithms/math/StandardDeviationTest.java diff --git a/src/main/java/com/williamfiset/algorithms/math/StandardDeviation.java b/src/main/java/com/williamfiset/algorithms/math/StandardDeviation.java new file mode 100644 index 000000000..cbe73efce --- /dev/null +++ b/src/main/java/com/williamfiset/algorithms/math/StandardDeviation.java @@ -0,0 +1,17 @@ +package com.williamfiset.algorithms.math; + +public class StandardDeviation { + + public static double SD(double arr[]) { + double sum = 0, StandardDeviation = 0; + int length = arr.length; + for (double num : arr) { + sum += num; + } + double mean = sum / length; + for (double num : arr) { + StandardDeviation = StandardDeviation + Math.pow(num - mean, 2); + } + return Math.sqrt((StandardDeviation / length)); + } +} diff --git a/src/test/java/com/williamfiset/algorithms/math/StandardDeviationTest.java b/src/test/java/com/williamfiset/algorithms/math/StandardDeviationTest.java new file mode 100644 index 000000000..255c780b9 --- /dev/null +++ b/src/test/java/com/williamfiset/algorithms/math/StandardDeviationTest.java @@ -0,0 +1,19 @@ +package com.williamfiset.algorithms.math; +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.Test; +public class StandardDeviationTest { + + @Test + public void StandardDeviation4(){ + double[] array = {-1, -2, -3, -4, -5}; + double result = StandardDeviation.SD(array); + assertEquals(1.4142135623730951, result); + } + + @Test + public void StandardDeviation2(){ + double[] array = {3, 5, 7, 20, 55, 12, 1, 3, 5, 4}; + double result = StandardDeviation.SD(array); + assertEquals(15.428869044748549, result); + } +}