Wednesday, July 22, 2009

Project euler code!

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package projecteuler;

/**

A palindromic number reads the same both ways.
* The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

Find the largest palindrome made from the product of two 3-digit numbers.

*/
import java.util.*;
public class palindrome {

public static void main(String [] args)
{
//discover whether a number is a palindrome...

int first3digit = 1000;
int second3digit = 1000;

boolean keepGoing = true;
int counter = 0;
ArrayList arrayList = new ArrayList();
for(int i = 1000; i > 0; i--)
{
for(int j = 1000; j > 0; j--)
{
if(isPalindrome(i*j))
arrayList.add(i*j);
}
}
int largest = arrayList.get(0);
for(int x = 0; x< arrayList.size(); x++)
{
if(arrayList.get(x) > largest)
{
largest = arrayList.get(x);
}
}
System.out.println(largest + " is the largest");
/*
while(keepGoing == true)
{
if(isPalindrome(first3digit * second3digit))
{
System.out.println(first3digit);
System.out.println(second3digit);
counter++;
}

if(counter % 2 == 1)
{
--first3digit;
}
else
{
--second3digit;
}

if(counter == 1000000000)
{
keepGoing = false;
}

if(first3digit < 0 || second3digit < 0)
{
keepGoing = false;
}


// System.out.println(first3digit);
// System.out.println(second3digit);
counter++;
}
*/

}


public static boolean isPalindrome(Integer c)
{
String a = c.toString();
boolean b = true;
for(int i = 0; i < a.length()-1; i++)
{
if(a.charAt(i) == a.charAt(a.length()-i-1))
{
//System.out.println("We've got a match!");
}
else
{
b = false;
}
}
if(b == true)
{
System.out.println("This " + a + " is a palindrome.");
return true;
}
else
{
//System.out.println("This " + a + " is not a palindrome.");
return false;
}
}
}

No comments: