summaryrefslogtreecommitdiff
path: root/src/Number.java
blob: d96274fae1c632ef2b6e307b5b95d0e16e617f52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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;
  }
}