Skip to content

Instantly share code, notes, and snippets.

@mariiaKolokolova
Created May 6, 2020 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariiaKolokolova/7f2d6863d677db70d2230dec2dc6ad75 to your computer and use it in GitHub Desktop.
Save mariiaKolokolova/7f2d6863d677db70d2230dec2dc6ad75 to your computer and use it in GitHub Desktop.
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