More efficient isPrime() method
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user