write a program to print the highest palindrome value that is the multiplication of two numbers lies between 100 to 1000.

Recommended Answers

All 15 Replies

We won't just do your homework for you. Please show us what you've tried yourself, and where you're stuck.

ok..accept my apologies.Here it is-

package com;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

class A
{
    String j ;
    long  i;
    long reversedNum=0;
    List  array1;
    public void A(){

    for( i=10000;i<1000000;i++){
long input=i;
while (input != 0) {
    reversedNum = reversedNum * 10 + input % 10;
    input = input / 10;   
}
if(i==reversedNum){
    Integer k=(Integer)i;
     array1=new ArrayList();
    //Need code here...
    System.out.print(i);
}        
    }
 j= (String)Collections.max(array1);//need inprovement here too..
        }
    public void show(){
         System.out.println("The higesht palindrom numbers  multiplication is"+" "+array1);
    }
public static void main(String []argv)
{
A aaa= new A();
aaa.show();
}
}

If you only want the highest palindrome then why are you making a list of all of them? You should just start from the highest palindrome and work downward until you find one that meets your needs, then print that.

You could start at 1000000 and work your way down to 10000, but you shouldn't be testing every number to see if it is a palindrome. You should be generating palindromes. You could start with 999999 and work your way down to 10001.

@bguild-Hi The thing with the situation is-
1:I don't know which is the highest palindrome value between 10pw4-10power6.
2: I need to find a value that is made by multpilying two numbers between 100 to 999 that is the highest value of palindrome lies between those numbers for all cases of multiplication.

The thing that i had used here is --
1: I checked every number lies between 10000 to 1000000 to be palindrom . if they are palindorm values then put them into an array.After that check the highest value on that array and that will be answer,but I am facing some sort of programming skills problems.Check the code for details.

Part1: see bguild's answer.

Part2: If you start by finding palindromes then you are left with the problem of finding out if each palindrome is the product of of two numbers 100-999 (not so easy).
If you start by multiplying every possible combination of two numbers 100-999. that's only 500,000 multiplications, which will take no time at all. Then just check each one for being a palindrome, and keep the largest. Provided your code to test for palindromes isn't too slow the whole thing will run in well under a second (17mSec on my 3.2GHz core 2)

Coded it half optimized in Python and it works in there in 17.6 ms, little more optimized in 4.67 ms. You only need to keep best solution and go down in inner loop based on best solution so far. My program stops going down with loop counters at 953 and 952.

Nice one Tony. Works for Java too - count downwards and break inner loop when product < best so far ... 4.8 mSec
Interesting just how well optimised modern languages are, all converging to the same, presumably optimum, speed

It did go to 3.15 ms (average of 1000 calls) when I noticed to add breaking out of inner loop after first palindrome found (half ms saving) and inlined the palindrome check (1 ms saving).

I just tried in-lining the palindrome check (previously a method call) and now I'm looking at 1.7mSec. Seems the call itself was a full 65% of the execution time. I didn't expect that.

Come on csk19, we're having great fun with this, join in!

@James- Hi. Could you please share your code with me.

Nice try, but no way. This isn't a homework cheating service!
If you post your complete working solution then I'll share mine, but only if you show that you have done this yourself first.

But here's a more detailed breakdown ofhow you could implement this yourself

  1. Write a little method to check if a number is a palindrome - you'll find examples of how to do that on the web - there are ones that do cunning arithmetic, and ones that simply convert the number to a String, reverse that, and see if it's the same. It really doesn't matter which you chose.

  2. Write a double nested loop - outer loop i goes from 100 to 999, inner loop j ditto. Inside the inner loop multiply i by j, then use your method to see if that's a palindrome. If it is, compare it with the highest palindrome you have found so far. If it's bigger then that becomes the new biggest palindrome.

  3. Print the highest palindrome and that's it!

(The way I described is the simplest / most literal way to do it. That's the right place to start. Once you have it working you can do various optimisations like Tony & I were doing, but that's optional)

I am not sure how many number the question is asking for??? Is it only 1 highest palindrome number which is the result of multiplication of 2 numbers in [100, 1000]? Or is it the highest palindrome numbers that are the result of 2 numbers pair?

Also, the minimum number could be changed if at least one palindrome number is found in a cycle of the outter loop if the answer is looking for only 1 highest number. The minimum number could be changed to the smaller number of the found palindrome number pair. The reason is that there shouldn't be any multiplication which could be greater than the current found pair in a loop.

Let say A and B are the first pair found in a loop. The A is greater than B. Because the loop goes from the highest to the lowest, there should not be any pair number such that thier multiplication of a number between A and B and a number below B could be greater than A and B multiplication.

yup. I also got that. Thanks everyone. But bytheway I want to know how to calculate timetaken in the program execution.

System.nanoTime() returns the time in nanoSeconds. Take its value before you start and after you finish and subtract the two.

commented: the program takes takes 23ms. +0

Below is my result (forgot to add). Not very fast though :P

Mininum:100 | Maximum:1000
Found at 993 x 913 = 906609
Total time: 16 millisecond(s)
Mininum:100 | Maximum:10000
Found at 9999 x 9901 = 99000099
Total time: 6 millisecond(s)
Mininum:100 | Maximum:100000
Found at 99979 x 99681 = 9966006699
Total time: 139 millisecond(s)
Mininum:100 | Maximum:1000000
Found at 999999 x 999001 = 999000000999
Total time: 357 millisecond(s)
Mininum:100 | Maximum:10000000
Found at 9998017 x 9997647 = 99956644665999
Total time: 37458 millisecond(s)
Mininum:100 | Maximum:100000000
Found at 99999999 x 99990001 = 9999000000009999
Total time: 41729 millisecond(s)

commented: very impressive.! +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.