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?
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...
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...
making a python code for this: A palindrome is a sequence that reads the same backwards...
making a python code for this: A palindrome is a sequence that reads the same backwards as forwards. Numbers can also be palindromes if we consider their digits as a sequence, for example 12121 and 8228 are palindromes. We can find palindromes from an initial seed number using the reverse and add method: choose a number, reverse its digits and add it to the original. If the sum is not a palindrome (which means, it is not the same number...
A palindrome is a string that reads the same forward and backward, i.e., the letters are...
A palindrome is a string that reads the same forward and backward, i.e., the letters are the same whether you read them from right to left or from left to right.      Examples: radar à is a palindrome Able was I ere I saw Elba à is a palindrome good à not a palindrome Write a java program to read a line of text and tell if the line is a palindrome. Use a stack to read each non-blank character...
#Python: A palindrome is a sequence of characters that reads the same backwards as forwards. For...
#Python: A palindrome is a sequence of characters that reads the same backwards as forwards. For example, ‘Eve’, ‘madam’, and 20502, are palindromes. Write a function called testPalindrome() that asks the user to input a string and returns if that string is a palindrome with the output as follows, without red: >>> Please enter a string: eve Your string "eve" is a palindrome. >>> testPalindrome() Please enter a string: end Your string "end" is not a palindrome. >>> testPalindrome() Please...
A palindrome is a word or phrase, which reads the same backward or forward. Write a...
A palindrome is a word or phrase, which reads the same backward or forward. Write a program that prompts the user for a string of characters terminated by a period and determines whether the string (without the period) is a palindrome. IMP: Assume that the input contains only letters and blanks. Assume also that the input is at most 30 characters long. Use an array of characters of size 30 to store the input! Disregard blanks when deciding if the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT