Skip to content
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
Binary file added .DS_Store
Binary file not shown.
60 changes: 28 additions & 32 deletions AP1403 - WarmUp/src/main/java/Exercises.java
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down