Compute the Palindrome of a number by adding the number composed of : BigInteger « Data Type « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Data Type » BigIntegerScreenshots 
Compute the Palindrome of a number by adding the number composed of
 
import java.math.BigInteger;

/** Compute the Palindrome of a number by adding the number composed of
 * its digits in reverse order, until a Palindrome occurs.
 * e.g., 42->66 (42+24); 1951->5995 (1951+1591=3542; 3542+2453=5995).
 * <P>TODO: Do we need to handle negative numbers?
 @author Ian Darwin, http://www.darwinsys.com/
 @version $Id: PalindromeBig.java,v 1.3 2004/02/09 03:33:57 ian Exp $.
 */
public class PalindromeBig {

  public static boolean verbose = true;

  public static void main(String[] argv) {
    for (int i=0; i<argv.length; i++)
      try {
        BigInteger l = new BigInteger(argv[i]);
        if (l.compareTo(BigInteger.ZERO0) {
          System.err.println(argv[i" -> TOO SMALL");
          continue;
        }
        System.out.println(argv[i"->" + findPalindrome(l));
      catch (NumberFormatException e) {
        System.err.println(argv[i"-> INVALID");
      catch (IllegalStateException e) {
        System.err.println(argv[i"-> " + e);
      
  }

  /** find a palindromic number given a starting point, by
   * calling ourself until we get a number that is palindromic.
   */
  public static BigInteger findPalindrome(BigInteger num) {
    if (num.compareTo(BigInteger.ZERO0)
      throw new IllegalStateException("negative");
    if (isPalindrome(num))
      return num;
    if (verbose)
      System.out.println("Trying " + num);
    return findPalindrome(num.add(reverseNumber(num)));
  }

  /** A ridiculously large number  */
  protected static final int MAX_DIGITS = 255;

  /** Check if a number is palindromic. */
  public static boolean isPalindrome(BigInteger num) {
    String digits = num.toString();
    int numDigits = digits.length();
    if (numDigits >= MAX_DIGITS) {
      throw new IllegalStateException("too big");
    }
    // Consider any single digit to be as palindromic as can be
    if (numDigits == 1)
      return true;
    for (int i=0; i<numDigits/2; i++) {
      // System.out.println(
      //   digits.charAt(i) + " ? " + digits.charAt(numDigits - i - 1));
      if (digits.charAt(i!= digits.charAt(numDigits - i - 1))
        return false;
    }
    return true;
  }

  static BigInteger reverseNumber(BigInteger num) {
    String digits = num.toString();
      int numDigits = digits.length();
    char[] sb = new char[numDigits];
    for (int i=0; i<digits.length(); i++) {
      sb[i= digits.charAt(numDigits - i - 1);
    }
    // Debug.println("rev",
    //   "reverseNumber(" + digits + ") -> " + "\"" + sb + "\"");
    return new BigInteger(new String(sb));
  }
}



           
         
  
Related examples in the same category
1. BigInteger.isProbablePrime
2. Operating with Big Integer Values
3. Create BigInteger via string
4. Create BigInteger via long type variable
5. Multiply one BigInteger to another BigInteger
6. Subtract one BigInteger from another BigInteger
7. Divide one BigInteger from another BigInteger
8. Negate a BigInteger
9. Calculate the power on a BigInteger
10. Create BigInteger from byte array
11. Performing Bitwise Operations with BigInteger
12. Get the value of a bit
13. Set a bit for BigInteger
14. Clear a bit in a BigInteger
15. Flip a bit in a BigInteger
16. Shift the bits in a BigInteger
17. Shift right in a BigInteger
18. xor a BigInteger
19. and operation on BigInteger
20. not operation for BigInteger
21. or operation for BigInteger
22. antNot operation on BigInteger
23. Retrieve the current bits in a byte array in twos-complement form.
24. Parsing and Formatting a Big Integer into Binary
25. Parsing and Formatting a Big Integer into octal
26. Parsing and Formatting a Big Integer into decimal
27. Parse and format to hexadecimal
28. Parse and format to arbitrary radix <= Character.MAX_RADIX
29. Create a BigInteger using the byte array
30. Parsing and Formatting a Byte Array into Binary
31. Parsing and Formatting a Byte Array into Octal
32. Parsing and Formatting a Byte Array into decimal
33. Parsing and Formatting a Byte Array into Hexadecimal
34. Parse binary string
35. Get byte array from BigInteger
36. Parse octal string to create BigInteger
37. Parse decimal string to create BigInteger
38. Parse hexadecimal string to create BigInteger
39. Convert BigInteger into another radix number
40. Do math operation for BigInteger
41. Operate with big integer values in code
42. Demonstration of high-precision integer arithmetic with the BigInteger classDemonstration of high-precision integer arithmetic with the BigInteger class
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.