diff --git a/AP1403 - WarmUp/.idea/.name b/AP1403 - WarmUp/.idea/.name new file mode 100644 index 0000000..56530e0 --- /dev/null +++ b/AP1403 - WarmUp/.idea/.name @@ -0,0 +1 @@ +TestFib.java \ No newline at end of file diff --git a/AP1403 - WarmUp/src/main/java/Exercises.java b/AP1403 - WarmUp/src/main/java/Exercises.java index 923d44a..b25b2e8 100644 --- a/AP1403 - WarmUp/src/main/java/Exercises.java +++ b/AP1403 - WarmUp/src/main/java/Exercises.java @@ -3,11 +3,17 @@ public class Exercises { /* complete this function to check if the input number is prime or not */ - public boolean isPrime(long n) { + public boolean isPrime(long n) + { // todo - return false; - } + for (int i = 2; i * i <= 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, ... @@ -16,6 +22,18 @@ public boolean isPrime(long n) { */ public long fibonacciIndex(long n) { // todo + if (n<=0) + return -1 ; + int x=0 , z=1 , index=0 ; + while (x<=n) + { + if (x==n) + return index; + int temp=x+z; + x=z; + z = temp ; + index++; + } return -1; } @@ -39,10 +57,31 @@ public long fibonacciIndex(long n) { */ public char[][] generateTriangle(int n) { // todo - return null; + char[][] matrix = new char[n][]; + for (int a = 0; a< n; a++) + matrix[a]= new char [a+1]; + + for (int i = 0; i < n; i++) + for (int j = 0; j <=i; j++) { + if (j == 0 || j == i || i == n - 1) + matrix[i][j] = '*'; + else + matrix[i][j] = ' '; + } + + return matrix; } public static void main(String[] args) { // you can test your code here, but then it should be checked with test cases + char[][] mat = new Exercises().generateTriangle(5); + for (int i=0 ; i<5 ; i++) + { + for ( int j=0 ; j<=i ; j++ ) + { + System.out.print(mat[i][j]); + } + System.out.print("\n"); + } } }