diff options
Diffstat (limited to 'src/Number.java')
| -rw-r--r-- | src/Number.java | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/Number.java b/src/Number.java new file mode 100644 index 0000000..d96274f --- /dev/null +++ b/src/Number.java @@ -0,0 +1,46 @@ +package ca.mokhan.assignment1; + +import java.util.ArrayList; + +public class Number { + public static boolean isDivisibleBy5(int n) { + return isDivisibleBy(n, 5); + } + + public static boolean isDivisibleBy7(int n) { + return isDivisibleBy(n, 7); + } + + public static boolean isOdd(int n) { + return !isDivisibleBy(n, 2); + } + + public static boolean isPrime(int n) { + if (n <= 1) return false; + + for (int i = n - 1; i > 1; i--) if (isDivisibleBy(n, i)) return false; + + return true; + } + + public static boolean isDivisibleBy(int n, int denominator) { + return n % denominator == 0; + } + + public static ArrayList<String> iterate() { + ArrayList<String> items = new ArrayList<String>(); + ArrayList<String> row = new ArrayList<String>(); + for (Integer i = 0; i < 113; i++) { + row.clear(); + row.add(String.format("%d", i)); + + if (isOdd(i)) row.add(String.format("%d is odd", i)); + if (isDivisibleBy5(i)) row.add("hi five"); + if (isDivisibleBy7(i + (i + 1))) row.add("wow"); + if (isPrime(i)) row.add("prime"); + + items.add(String.join(",", row)); + } + return items; + } +} |
