From 4508f8196b6be3afdd77a0f0b2b463e083ecc45a Mon Sep 17 00:00:00 2001 From: arya Date: Sat, 1 Mar 2025 23:08:17 +0330 Subject: [PATCH 1/3] commiting isPrime --- .DS_Store | Bin 0 -> 6148 bytes AP1403 - WarmUp/.idea/.name | 1 + AP1403 - WarmUp/src/main/java/Exercises.java | 18 ++++++------------ 3 files changed, 7 insertions(+), 12 deletions(-) create mode 100644 .DS_Store create mode 100644 AP1403 - WarmUp/.idea/.name diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..b752b8901c3db26f3b889203e1038094bc6d31e7 GIT binary patch literal 6148 zcmeHKO-lno41LiaDjw|7V%^9gQjj6*|6I$P9KhM_7X0^O$Bssz!E-__|8t23zS6FLxO0*c_>GYR~s{&g`PlrVFA+hp%^CGr7>lX`$RE{|&1IfTP z1KRsi273Na`O9=R`4*BxGLQ`XR|ce4&*}-kF3#33->GM9q25qcG_F;HLVN2Hzz5w& f4sFusi`tB<0$WF|qJB#!=0(5=$srl|1qR*#_9HEu literal 0 HcmV?d00001 diff --git a/AP1403 - WarmUp/.idea/.name b/AP1403 - WarmUp/.idea/.name new file mode 100644 index 0000000..961c0bf --- /dev/null +++ b/AP1403 - WarmUp/.idea/.name @@ -0,0 +1 @@ +Exercises.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..2ad8683 100644 --- a/AP1403 - WarmUp/src/main/java/Exercises.java +++ b/AP1403 - WarmUp/src/main/java/Exercises.java @@ -1,21 +1,15 @@ 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 return -1; } From 5202d1a231ec4ad4224f0206fd51a9003e3335a3 Mon Sep 17 00:00:00 2001 From: arya Date: Sat, 1 Mar 2025 23:30:26 +0330 Subject: [PATCH 2/3] all problems solved --- AP1403 - WarmUp/.idea/.name | 1 - AP1403 - WarmUp/src/main/java/Exercises.java | 44 ++++++++++---------- 2 files changed, 23 insertions(+), 22 deletions(-) delete mode 100644 AP1403 - WarmUp/.idea/.name diff --git a/AP1403 - WarmUp/.idea/.name b/AP1403 - WarmUp/.idea/.name deleted file mode 100644 index 961c0bf..0000000 --- a/AP1403 - WarmUp/.idea/.name +++ /dev/null @@ -1 +0,0 @@ -Exercises.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 2ad8683..d536a1d 100644 --- a/AP1403 - WarmUp/src/main/java/Exercises.java +++ b/AP1403 - WarmUp/src/main/java/Exercises.java @@ -10,30 +10,32 @@ public boolean isPrime(long n) { } public long fibonacciIndex(long n) { + 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][]; // Jagged array instead of fixed [n][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) { From c8bacf3366c78b04c97dba640fa55d59ef3eafb3 Mon Sep 17 00:00:00 2001 From: arya Date: Sat, 1 Mar 2025 23:32:14 +0330 Subject: [PATCH 3/3] bug fix --- AP1403 - WarmUp/src/main/java/Exercises.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/AP1403 - WarmUp/src/main/java/Exercises.java b/AP1403 - WarmUp/src/main/java/Exercises.java index d536a1d..3a27947 100644 --- a/AP1403 - WarmUp/src/main/java/Exercises.java +++ b/AP1403 - WarmUp/src/main/java/Exercises.java @@ -21,9 +21,9 @@ public long fibonacciIndex(long n) { } return -1; } - + public char[][] generateTriangle(int n) { - char[][] triangle = new char[n][]; // Jagged array instead of fixed [n][n] + char[][] triangle = new char[n][]; for (int i = 0; i < n; i++) { triangle[i] = new char[i + 1];