diff --git a/30 Days of Code/Day 25 - Running Time and Complexity/Solution.java b/30 Days of Code/Day 25 - Running Time and Complexity/Solution.java index ac2cb23..49e382f 100644 --- a/30 Days of Code/Day 25 - Running Time and Complexity/Solution.java +++ b/30 Days of Code/Day 25 - Running Time and Complexity/Solution.java @@ -18,9 +18,13 @@ public class Solution { private static boolean isPrime(int n) { if (n < 2) { return false; + } else if (n == 2) { // account for even numbers now, so that we can do i+=2 in loop below + return true; + } else if (n % 2 == 0) { // account for even numbers now, so that we can do i+=2 in loop below + return false; } int sqrt = (int) Math.sqrt(n); - for (int i = 2; i <= sqrt; i++) { + for (int i = 3; i <= sqrt; i += 2) { // skips even numbers for faster results if (n % i == 0) { return false; } diff --git a/Cracking the Coding Interview/Techniques, Concepts/Time Complexity - Primality/Solution.java b/Cracking the Coding Interview/Techniques, Concepts/Time Complexity - Primality/Solution.java index 3eac536..f30680b 100644 --- a/Cracking the Coding Interview/Techniques, Concepts/Time Complexity - Primality/Solution.java +++ b/Cracking the Coding Interview/Techniques, Concepts/Time Complexity - Primality/Solution.java @@ -4,6 +4,8 @@ import java.util.Scanner; +// Time Complexity: O(n^(1/2)) for each +// Space Complexity: O(1) public class Solution { public static void main(String[] args) { Scanner scan = new Scanner(System.in); @@ -18,9 +20,13 @@ public class Solution { public static boolean isPrime(int n) { if (n < 2) { return false; + } else if (n == 2) { // account for even numbers now, so that we can do i+=2 in loop below + return true; + } else if (n % 2 == 0) { // account for even numbers now, so that we can do i+=2 in loop below + return false; } int sqrt = (int) Math.sqrt(n); - for (int i = 2; i <= sqrt; i++) { + for (int i = 3; i <= sqrt; i += 2) { // skips even numbers for faster results if (n % i == 0) { return false; } diff --git a/Java/Advanced/Prime Checker/Solution.java b/Java/Advanced/Prime Checker/Solution.java index 48f712b..5679604 100644 --- a/Java/Advanced/Prime Checker/Solution.java +++ b/Java/Advanced/Prime Checker/Solution.java @@ -24,15 +24,19 @@ class Prime { boolean isPrime(int n) { if (n < 2) { return false; + } else if (n == 2) { // account for even numbers now, so that we can do i+=2 in loop below + return true; + } else if (n % 2 == 0) { // account for even numbers now, so that we can do i+=2 in loop below + return false; } int sqrt = (int) Math.sqrt(n); - for (int i = 2; i <= sqrt; i++) { + for (int i = 3; i <= sqrt; i += 2) { // skips even numbers for faster results if (n % i == 0) { return false; } } return true; - } + } } public class Solution { diff --git a/Java/Introduction/Java Date and Time/Solution.java b/Java/Introduction/Java Date and Time/Solution.java index 360c29a..2c83c25 100644 --- a/Java/Introduction/Java Date and Time/Solution.java +++ b/Java/Introduction/Java Date and Time/Solution.java @@ -6,16 +6,20 @@ import java.util.Scanner; import java.time.LocalDate; public class Solution { + public static String getDay(String day, String month, String year) { + int d = Integer.valueOf(day); + int m = Integer.valueOf(month); + int y = Integer.valueOf(year); + LocalDate date = LocalDate.of(y, m, d); + return date.getDayOfWeek().toString(); + } + public static void main(String[] args) { - /* Read input */ - Scanner scan = new Scanner(System.in); - int month = scan.nextInt(); - int day = scan.nextInt(); - int year = scan.nextInt(); - scan.close(); + Scanner in = new Scanner(System.in); + String month = in.next(); + String day = in.next(); + String year = in.next(); - /* Create LocalDate instance */ - LocalDate date = LocalDate.of(year, month, day); - System.out.println(date.getDayOfWeek()); + System.out.println(getDay(day, month, year)); } }