-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEuler36.java
50 lines (44 loc) · 943 Bytes
/
Euler36.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package Euler;
public class Euler36 {
public static boolean isPal(int n){
int reverse = 0;
int num = n;
while(num > 0){
int mod = num % 10;
reverse = reverse * 10 + mod;
num/= 10;
}
if(reverse == n){
return true;
}
return false;
}
public static boolean isPalBin(int n){
int reverse = 0;
int num = n;
while(num > 0){
int mod = num % 2;
reverse = reverse * 2 + mod;
num/= 2;
}
if(reverse == n){
return true;
}
return false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int i = 1;
int sum = 0;
long s = System.currentTimeMillis();
for(;i < 1000000;i++){
if(isPal(i) && isPalBin(i)){
sum+=i;
System.out.println(i +" in binary: "+Integer.toBinaryString(i));
}
}
long e = System.currentTimeMillis();
System.out.println("\nSum of all Double base Palindrome is "+sum);
System.out.println("time taken "+(e-s)+" ms");
}
}