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
1 change: 1 addition & 0 deletions AP1403 - WarmUp/.idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 43 additions & 4 deletions AP1403 - WarmUp/src/main/java/Exercises.java
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...
Expand All @@ -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;
}

Expand All @@ -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");
}
}
}