diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..b752b89 Binary files /dev/null and b/.DS_Store differ diff --git a/AP1403 - WarmUp/src/main/java/Exercises.java b/AP1403 - WarmUp/src/main/java/Exercises.java index 923d44a..3a27947 100644 --- a/AP1403 - WarmUp/src/main/java/Exercises.java +++ b/AP1403 - WarmUp/src/main/java/Exercises.java @@ -1,45 +1,41 @@ public class Exercises { - /* - complete this function to check if the input number is prime or not - */ public boolean isPrime(long n) { - // todo - return false; + if(n == 2) return true; + if(n < 2) return false; + for(int i = 2; i <= Math.sqrt(n); i++) { + if(n % i == 0) return false; + } + return true; } - /* - implement an algorithm to find out the index of input number in a fibonacci sequence starting from 0, 1 - e.g. 0, 1, 1, 2, 3, 5, ... - indices start from 0, e.g. 3 is the index 4 of this sequence - if the input is not a fibonacci number with description above, return -1 - */ public long fibonacciIndex(long n) { - // todo + long a = 0, b = 1 ,c ; + long counter = 0; + while(counter < n){ + c = a + b; + a = b; + b = c; + counter ++; + if(n == a) return counter; + } return -1; } - /* - you should create a triangle with "*" and return a two-dimensional array of characters based on that - the triangle's area is empty, which means some characters should be " " - - example 1, input = 3: - * - ** - *** - - example 2, input = 5: - * - ** - * * - * * - ***** - - the output has to be a two-dimensional array of characters, so don't just print the triangle! - */ public char[][] generateTriangle(int n) { - // todo - return null; + char[][] triangle = new char[n][]; + + for (int i = 0; i < n; i++) { + triangle[i] = new char[i + 1]; + for (int j = 0; j <= i; j++) { + if (j == 0 || j == i || i == n - 1) { + triangle[i][j] = '*'; + } else { + triangle[i][j] = ' '; + } + } + } + return triangle; } public static void main(String[] args) {