Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6366917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:29:00+00:00 2026-05-25T00:29:00+00:00

package testing.project; public class PalindromeThreeDigits { public static void main(String[] args) { int value

  • 0
package testing.project;

public class PalindromeThreeDigits {

    public static void main(String[] args) {
        int value = 0;
        for(int i = 100;i <=999;i++)
        {
            for(int j = i;j <=999;j++)
            {
                int value1 = i * j;
                StringBuilder sb1 = new StringBuilder(""+value1);
                String sb2 = ""+value1;
                sb1.reverse();
                if(sb2.equals(sb1.toString()) && value<value1) {
                    value = value1;

                }

            }
        }

        System.out.println(value);
    }
}

This is the code that I wrote in Java… Is there any efficient way other than this.. And can we optimize this code more??

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T00:29:01+00:00Added an answer on May 25, 2026 at 12:29 am

    We suppose the largest such palindrome will have six digits rather than five, because 143*777 = 111111 is a palindrome.

    As noted elsewhere, a 6-digit base-10 palindrome abccba is a multiple of 11. This is true because a*100001 + b*010010 + c*001100 is equal to 11*a*9091 + 11*b*910 + 11*c*100. So, in our inner loop we can decrease n by steps of 11 if m is not a multiple of 11.

    We are trying to find the largest palindrome under a million that is a product of two 3-digit numbers. To find a large result, we try large divisors first:

    • We step m downwards from 999, by 1’s;
    • Run n down from 999 by 1’s (if 11 divides m, or 9% of the time) or from 990 by 11’s (if 11 doesn’t divide m, or 91% of the time).

    We keep track of the largest palindrome found so far in variable q. Suppose q = r·s with r <= s. We usually have m < r <= s. We require m·n > q or n >= q/m. As larger palindromes are found, the range of n gets more restricted, for two reasons: q gets larger, m gets smaller.

    The inner loop of attached program executes only 506 times, vs the ~ 810000 times the naive program used.

    #include <stdlib.h>
    #include <stdio.h>
    int main(void) {
      enum { A=100000, B=10000, C=1000, c=100, b=10, a=1, T=10 };
      int m, n, p, q=111111, r=143, s=777;
      int nDel, nLo, nHi, inner=0, n11=(999/11)*11;
    
      for (m=999; m>99; --m) {
        nHi = n11;  nDel = 11;
        if (m%11==0) {
          nHi = 999;  nDel = 1;
        }
        nLo = q/m-1;
        if (nLo < m) nLo = m-1;
    
        for (n=nHi; n>nLo; n -= nDel) {
          ++inner;
          // Check if p = product is a palindrome
          p = m * n;
          if (p%T==p/A && (p/B)%T==(p/b)%T && (p/C)%T==(p/c)%T) {
            q=p; r=m; s=n;
            printf ("%d at %d * %d\n", q, r, s);
            break;      // We're done with this value of m
          }
        }
      }
      printf ("Final result:  %d at %d * %d   inner=%d\n", q, r, s, inner);
      return 0;
    }
    

    Note, the program is in C but same techniques will work in Java.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

package org.study.algos; public class Study { public static void main(String[] args) { A a
package pack; public class sample{ public static void main(String input[]) { NumberFormat numberFormat =
package test; import java.awt.*; import java.awt.event.*; import java.awt.geom.Ellipse2D; import java.awt.image.BufferedImage; import javax.swing.*; public class
package { import mx.controls.LinkButton; import flash.text.TextLineMetrics; public class multiLineLinkButton extends LinkButton { override protected
I am getting these strange white package icons in my testing project. From what
I'm testing the ogl package in wxpython. I want to drag a shape. And
VMware server 1.0.7 installed with vmware-package Debian GNU/Linux testing (lenny) Kernel 2.6.26-1-686 There were
I started a test project just to get this down. No changes to main.xml.
I'm working on getting my ASP.Net project working on Linux. I've been testing my
I have a custom project type based on the C# Class Library project type

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.