Psst.. new poll here.
[email protected] webmail now available. Want one? Go here.
Cannot use outlook/hotmail/live here to register as they blocking our mail servers. #microsoftdeez
Obey the Epel!
Paste
Pasted as Java by Mark ( 14 years ago )
import java.io.*;
/*
Copyright Mark Campbell, distributed under any GPL license.
*/
public class Problem4
{
public static void main (String[] args)
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the number of digits that you wish to calculate to: ");
long digits = 0L;
try
{
digits = Long.parseLong(in.readLine());
}
catch (Exception ex)
{
System.out.println("\nNot a valid number. Going with 3 digits.");
digits = 3L;
}
digits = (long)Math.pow(10.0, digits);
long largest = 0L;
long count = 0L;
long largestI = 0L;
long largestJ = 0L;
long jCount = 0L;
//For timing
long time = System.currentTimeMillis();
for (long i = digits / 10L; i < digits; i++)
{
for (long j = (digits / 10L) + jCount; j < digits; j++)
{
if (isPalindrome(Long.toString(i * j)))
if (((long)(i)) * ((long)(j)) > largest)
{
count++;
largestI = i;
largestJ = j;
largest = i * j;
}
}
jCount++;
}
time = System.currentTimeMillis() - time;
System.out.println("\n\nThe largest number was:\t\t\t" + largest);
System.out.println("Number of 'largest' numbers found:\t" + count);
System.out.println("The two miraculous numbers found:\t" + largestI + " * " + largestJ);
System.out.println("\nTime it took for the computation:\t" + time + "ms");
System.out.print("Press any key to continue... ");
try
{
in.readLine();
}
catch (IOException ex)
{
}
}
public static boolean isPalindrome(String val)
{
char[] num = val.toCharArray();
int length = num.length;
//Check if it's even
if (length % 2 == 0)
{
for (int i = 0; i <= length / 2; i++)
{
if (num[length - i - 1] != num[0 + i])
{
return false;
}
}
}
else //for odd
{
for (int i = 0; i < length / 2; i++)
{
if (num[length - i - 1] != num[0 + i])
{
return false;
}
}
}
return true;
}
}
Revise this Paste
Children: 24872