Skip to content
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

Added Armstrong number algorithm and tests for it #350

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}