Advertisement
Guest User

Untitled

a guest
May 4th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. public class Palindrome {
  2.  
  3. private static boolean isPalindrome(long n) {
  4. return reverse(n) == n;
  5. }
  6.  
  7. private static long reverse(long n) {
  8. long rev = 0L;
  9. while (n > 0) {
  10. long d = n % 10;
  11. n = n / 10;
  12. rev = rev * 10 + d;
  13. }
  14. return rev;
  15. }
  16.  
  17. /**
  18. * Finds the largest palindrome number not greater than n.
  19. */
  20. public long largest(long n) {
  21. long max = 0;
  22. for (int j = 100; j < 1000; j++) {
  23. for (int k = 100; k < 1000; k++) {
  24. long p = j * k;
  25. if (p >= n) {
  26. break;
  27. }
  28. if (isPalindrome(p)) {
  29. max = Math.max(max, p);
  30. }
  31.  
  32. }
  33. }
  34. return max;
  35. }
  36.  
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement