Question

In: Computer Science

A palindromic number reads the same both ways (left-to-right and right-to-left). The largest palindrome made from...

A palindromic number reads the same both ways (left-to-right and right-to-left).

The largest palindrome made from the product of two 2-digit numbers is 9,009 = 91 × 99.

The largest palindrome made from the product of two 3-digit numbers is 906,609 = 913 × 993.
The largest palindrome made from the product of two 4-digit numbers is 99,000,099 = 9,901 × 9,999.

1. Write a function IN JAVASCRIPT to find the largest palindrome made from the product of two 7-digit numbers.

2. What is that product?

3. How long (execution time) does it take your solution to calculate this answer?

Solutions

Expert Solution

ANSWER :

Code

public class Main {

    private int largestPalindrome(int n) {
        long startTime = System.nanoTime();
        int upperBound = (int) Math.pow(10, n) - 1;
        int lowerBound = (int) Math.pow(10, n - 1);
        int maximum = Integer.MIN_VALUE;
//        int digit1 = 0, digit2 = 0;
        for (int upperIndex = upperBound; upperIndex >= lowerBound; upperIndex--) {
            for (int lowerIndex = upperIndex; lowerIndex >= lowerBound; lowerIndex--) {
                int product = upperIndex * lowerIndex;
                if (product < maximum) {
                    break;
                }
                if (product > maximum && check(product)) {
//                    digit1 = upperIndex;
//                    digit2 = lowerIndex;
                    maximum = product;
                }
            }
        }
        long endTime = System.nanoTime();
        System.out.println("Execution time in milliseconds : " + (endTime - startTime) / 1000000);
//        System.out.println("Digit1 = " + digit1 + " Digit2 = " + digit2);
        return maximum;
    }

    private boolean check(int number) {
        int reverse = 0, curr = number;
        while (number != 0) {
            reverse = reverse * 10 + number % 10;
            number /= 10;
        }
        return curr - reverse == 0;
    }

    public static void main(String[] args) {
        int n = 7;
        Main main = new Main();
        System.out.print(main.largestPalindrome(n));
    }
}
Output

// Uncomment the commented line to print the both the numbers

( PLEASE VOTE FOR THIS ANSWER )

I THINK IT WILL BE USEFULL TO YOU ......

PLZZZZZ COMMENT IF YOU HAVE ANY PROBLEM ........

THANK YOU ..........


Related Solutions

(Palindrome number - A number is a palindrome if it reads the same from right to...
(Palindrome number - A number is a palindrome if it reads the same from right to left and from left to right, for example 676 is a palindrome number) Write a program that prompts the user to enter a three-digit integer number and determines whether it is a palindrome number or not In Java Please
1. A is called a palindrome if it reads the same from left and right. For...
1. A is called a palindrome if it reads the same from left and right. For instance, 13631 is a palindrome, while 435734 is not. A 6-digit number n is randomly chosen. Find the probability of the event that (a) n is a palindrome. (b) n is odd and a palindrome. (c) n is even and a palindrome.
A is called a palindrome if it reads the same from left and right. For instance,...
A is called a palindrome if it reads the same from left and right. For instance, 13631 is a palindrome, while 435734 is not. A 6-digit number n is randomly chosen. Find the probability of the event that (a) n is a palindrome. (b) n is odd and a palindrome. (c) n is even and a palindrome.
A word that reads the same from left to right and right to left is a...
A word that reads the same from left to right and right to left is a palindrome. For example, "I", "noon" and "racecar" are palindromes. In the following, we consider palindromic integers. Note that the first digit of an integer cannot be 0. How many positive palindromic integers are 5-digit long and contain 7 or 8?
A Palindromic number is one that reads the same backwards and forwards. Write a MATLAB function...
A Palindromic number is one that reads the same backwards and forwards. Write a MATLAB function (call it palin.m) that takes as input a positive integer, and returns 1 (true) if it is palindromic, 0 (false) if it is not.  
A palindrome prime is a prime number that reads the same forwards or backwards. An example...
A palindrome prime is a prime number that reads the same forwards or backwards. An example of a palindrome prime is 131. Write a method with the following signature for determining if a given number is a palindrome prime. public static boolean isPallyPrime(int nVal) Note: For this assignment you are not allowed to use the built in Java class Array as part of your solution for any of these questions. Your Method signatures must be the same as given here.
HTML 7.20 A palindrome is a number or a text phrase that reads the same backward...
HTML 7.20 A palindrome is a number or a text phrase that reads the same backward and forward. For example, each of the following five-digit integers is a palindrome: 12321, 55555, 45554 and 11611. Write a script that reads in a five-digit integer and determines whether it’s a palindrome. If the number is not five digits long, display an alert dialog indicating the problem to the user. Allow the user to enter a new value after dismissing the alert dialog....
In C++: This is the problem: [(1)] A palindrome is a string that reads the same...
In C++: This is the problem: [(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate [(2)] Let Q...
JAVA Palindrome Detector A palindrome is any word, phrase, or sentence that reads the same forward...
JAVA Palindrome Detector A palindrome is any word, phrase, or sentence that reads the same forward or backward. Here are some well-known palindromes: Able was I, ere I saw Elba A man, a plan, a canal, Panama Desserts, I stressed Kayak Write a boolean method that users recursion to determine where a String argument is a palindrome. The method should return true if the argument reads the same forward and backward. Demonstrate the method in a program. Include the following...
IN JAVA - [(1)] A palindrome is a string that reads the same forwards as backwards....
IN JAVA - [(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate [(2)] Let Q be a non-empty...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT