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

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...
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...
A palindrome is a word or a phrase that is the same when read both forward and backward.
6.7 LAB: PalindromeA palindrome is a word or a phrase that is the same when read both forward and backward. Examples are: "bob," "sees," or "never odd or even" (ignoring spaces). Write a program whose input is a word or phrase, and that outputs whether the input is a palindrome.Ex: If the input is:bobthe output is:bob is a palindromeEx: If the input is:bobbythe output is:bobby is not a palindromeHint: Start by removing spaces. Then check if a string is equivalent to it's reverse.This is my code:s =...
JAVA. A palindrome is a word or a phrase that is the same when read both forward and backward.
java please. A palindrome is a word or a phrase that is the same when read both forward and backward. Examples are: "bob," "sees," or "never odd or even" (ignoring spaces). Write a program whose input is a word or phrase, and that outputs whether the input is a palindrome.Ex: If the input is:bobthe output is:bob is a palindromeEx: If the input is:bobbythe output is:bobby is not a palindromeHint: Start by removing spaces. Then check if a string is equivalent to it's reverse.Hint: Start by just handling single-word...
1.list 4  Manifestations of Right and Left sided Failure and 4 Symptoms of both right and left...
1.list 4  Manifestations of Right and Left sided Failure and 4 Symptoms of both right and left sided failure. 2.Loop Diuretics are given as a drug of choice, i Name the two Diuretics that affect the loop of Henle. ii What is the dosage and route this would be administered? iiiName a contraindication for this medication. 3. List what assessment parameters must be assessed for CHF 4. Differentiate: Right, Left, Chronic Failure and Acute decompensated heart failure (ADHF).    5. What...
Two wires are made of the same material. If both are at the same temperature, but...
Two wires are made of the same material. If both are at the same temperature, but one has twice the diameter and three times the length of the other, which has the greater resistance, and by what factor? please explain
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT