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
15 changes: 11 additions & 4 deletions AP1403 - WarmUp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,31 @@
</properties>

<dependencies>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.11.4</version>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.2</version>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.2</version>
<version>2.22.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>

</project>
</project>
Binary file added AP1403 - WarmUp/src/main/java/Exercises.class
Binary file not shown.
64 changes: 56 additions & 8 deletions AP1403 - WarmUp/src/main/java/Exercises.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@ 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 <= 1) {
return false;
}

for (long i = 2; i * i <= n; i++) {
if (n % i == 0) {
return false;
}
}

return true;
}

/*
Expand All @@ -15,8 +24,25 @@ public boolean isPrime(long n) {
if the input is not a fibonacci number with description above, return -1
*/
public long fibonacciIndex(long n) {
// todo
return -1;
if (n < 0) {
return -1;
}

long a = 0, b = 1;
long index = 1;

if (n == 0) {
return 0;
}

while (b < n) {
long temp = a + b;
a = b;
b = temp;
index++;
}

return (b == n) ? index : -1;
}

/*
Expand All @@ -37,12 +63,34 @@ public long fibonacciIndex(long n) {

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] = ' ';
}
}
}

if (n == 0) {
triangle = new char[0][];
}

return triangle;
}


public static void main(String[] args) {
// you can test your code here, but then it should be checked with test cases
}
}
}