-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem04.java
More file actions
49 lines (40 loc) · 2.16 KB
/
problem04.java
File metadata and controls
49 lines (40 loc) · 2.16 KB
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
/*
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 problem04 {
public static void main(String args[]) {
long startTime = System.currentTimeMillis(); // For analyzing execution time
int palindrome = 999999;
while (isProductOfThree(palindrome) == false) { // test the current palindrome
System.out.println("Oh too bad, " + palindrome + " is not a product of two 3-digit numbers."); // oops, it failed. Next!
palindrome = nextPalindrome(palindrome); // decrement to the next palindrome using our nextPalindrome() method
}
System.out.println(palindrome + " is a palindrome and a product of two 3-digit numbers! Hip hip hooray!"); // Success!
// Displaying our execution time
long elapsedTimeMillis = System.currentTimeMillis() - startTime;
System.out.println("\n\nProgram execution time: " + elapsedTimeMillis);
}
public static int nextPalindrome(int num) {
num = num - 1000; // 999999 becomes 998999
char[] numArray = String.valueOf(num).toCharArray(); // convoluted way to turn our int into a char array
numArray[5] = numArray[0];
numArray[4] = numArray[1];
numArray[3] = numArray[2];
num = Integer.parseInt(String.valueOf(numArray)); // turns our char array back into an int
// System.out.println("The next palindrome is: " + num);
return num;
}
public static boolean isProductOfThree(int num) { // tests whether the input int is a product of two 3-digit numbers
boolean foundOne = false; // assume false until we find out it's true, or run out of possibilities
// System.out.println("Testing " + num + " to see if it's the product of two 3-digit numbers.");
for (int i = 101; i <= 999; i++) {
// System.out.println("Is " + i + " a factor of " + num + "?");
if (num%i == 0 && (num/i)/100 < 10) { // if wefound a 3-digit factor, AND the other factor is also three digits
// System.out.println(num + " = " + i + "x" + (num/i) + ".");
foundOne = true;
}
}
return foundOne;
}
}