-
-
Save mariiaKolokolova/7f2d6863d677db70d2230dec2dc6ad75 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package maricka.kolokolova; | |
public class Main { | |
public static void main(String[] args) { | |
// TODO Auto-generated method stub | |
int n = 3; | |
maxPolidrom(n); | |
} | |
static void maxPolidrom(int n) { | |
int pl; | |
int maxPolidrom = 0; | |
int iMax = 0; | |
int jMax = 0; | |
for (int i = (int) Math.pow(10, (n - 1)); i < (int) Math.pow(10, n); i++) { | |
for (int j = i; j < (int) Math.pow(10, n); j++) { | |
pl = isPalidrom((i * j), n * 2); | |
if (pl > maxPolidrom) { | |
maxPolidrom = pl; | |
iMax = i; | |
jMax = j; | |
} | |
} | |
} | |
System.out.println("Max palidrom is " + iMax + " * " + jMax + " = " + maxPolidrom); | |
} | |
static int isPalidrom(int a, int s) { | |
int m = 0; | |
int k = 0; | |
int n; | |
for (int i = 0; i < s / 2; i++) { | |
n = s - i; | |
if (((int) ((a % Math.pow(10, n)) / (Math.pow(10, (n - 1))))) == ((int) ((a % Math.pow(10, i + 1)) | |
/ (Math.pow(10, i))))) { | |
m++; | |
} else { | |
k = 1; | |
} | |
} | |
if (m != 0 && k == 0) { | |
m = a; | |
} | |
return m; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment