codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
public class euler4 { public static class GlobalData { // Silly java global constructs... public static int bigO = 0; } public static String reverseIt(String source) { //returns the string reversed. int i, len = source.length(); StringBuffer dest = new StringBuffer(len); for (i = (len - 1); i >= 0; i--){ GlobalData.bigO++; dest.append(source.charAt(i)); } return dest.toString(); } public static boolean is_pal(int n) { String nStr = "" +n; if (nStr.equals(reverseIt(nStr))) { // This number is a palindrome, so return true return true; } return false; } public static boolean has_3_digit_factors(int n) { //String nStr = "" + n; for (int i=100; i<999; i++){ GlobalData.bigO++; if (n % i == 0){ String iStr = "" + n/i; if (iStr.length()==3){ System.out.println(n + " is the largest palindrome composed of two 3-digit factors: " + i + ", " + n/i); System.out.println("Number of iterations: " +GlobalData.bigO); return true; } } } return false; } public static void main(String[] args) { long timer = System.currentTimeMillis(); int n=999*999; for (int i=n; i>=0; i--) { GlobalData.bigO++; if ((i%11==0) && (is_pal(i)) && (has_3_digit_factors(i))){ //Java interprets multiple &&'s left to right. i=0; } } System.out.print( System.currentTimeMillis()-timer + " uSec "); } } //------------------------------ Output -------------------------------------- // // 906609 is the largest palindrome composed of two 3-digit factors: 913, 993 // Number of iterations: 223870 // 33 uSec
Private
[
?
]
Run code
Submit