-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLargestPalindromeProduct.java
76 lines (63 loc) · 1.62 KB
/
LargestPalindromeProduct.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package tenprojecteulersolutionsinjava;
/*
* https://projecteuler.net/problem=4
* Problem 4
* Largest Palindrom Product
*
*
* Largest palindrome product
Problem 4
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.
* */
public class LargestPalindromeProduct
{
public static boolean isPalindrome(int n)
{
int remainder;
int sum=0;
int temp;
temp = n;
while(n > 0)
{
remainder = n % 10;
sum=(sum * 10) + remainder;
n=n/10;
}
if(temp == sum) return true;
else return false;
}//close isPalindrome()
public static void main(String[] args)
{
int[] twoDigitNumbers = new int[1000];
java.util.List<Integer> palProductsList =
new java.util.ArrayList<Integer>();
for (int i = 0; i < 1000; i++)
{
twoDigitNumbers[i] = i;
} // close for
for (int j = 0; j < 1000; j++)
{
for (int x = 0; x < 1000; x++)
{
int temp = twoDigitNumbers[j] * x;
if(isPalindrome(temp)) // originally String.valueOf(temp)
{
//System.out.println(temp + " is a palindrome");
palProductsList.add(temp);
//if(temp == 9009) System.out.println("temp is " + temp)
}
} // close inner for x
} // close outer for j
//Find and print out min and max values
int min = palProductsList.get(0);
int max = palProductsList.get(0);
for(Integer y: palProductsList)
{
if(y < min) min = y;
if(y > max) max = y;
}
System.out.println("min = " + min);
System.out.println("max = " + max);
} // close main()
} // close class