-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
33 lines (28 loc) · 1.04 KB
/
Main.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package easy.isLucky;
public class Main {
static boolean solution(int n) {
// number of digits in a number
int len = (int)(Math.log10(n) + 1);
int firstHalf = 0;
int secondHalf = 0;
for(int i = 0; i <= len/2; i++)
secondHalf += (int)((n % Math.pow(10, i))/Math.pow(10, i-1));
for(int i = len/2 + 1; i <= len; i++)
firstHalf += (int)((n % Math.pow(10, i))/Math.pow(10, i-1));
return secondHalf == firstHalf;
}
static boolean solutionImproved(int n) {
// number of digits in a number
int len = (int)(Math.log10(n) + 1);
int firstHalf = 0;
int secondHalf = 0;
for(int i = 0; i <= len/2; i++) {
secondHalf += (int)((n % Math.pow(10, i))/Math.pow(10, i-1));
firstHalf += (int)((n % Math.pow(10, len-i+1))/Math.pow(10, len-i));
}
return secondHalf == firstHalf;
}
public static void main (String[] args){
System.out.println("1203 is lucky? : " + solutionImproved(1203));
}
}