Can someone help me with the solution to Project Euler #4 in Java? -
Can someone help me with the solution to Project Euler #4 in Java? -
okay guys, know not elegant solution project euler's 4th problem, i'm proud of because did myself... until now.
here problem reference: http://projecteuler.net/index.php?section=problems&id=4
public class problem4 { public void projecteuler4() { int reply = 0; (int = 1; < 1000; i++) { (int j = 1; j < 1000; j++) { if (i * j >= answer) { string stringnum = "" + answer; string firsthalf = ""; string secondhalf = ""; if (stringnum.length() % 2 == 0) { (int k = 0; k < stringnum.length() / 2; k++) { firsthalf = firsthalf + ("" + stringnum.charat(k)); } (int k = stringnum.length(); k < stringnum.length() / 2; k--) { secondhalf = secondhalf + ("" + stringnum.charat(k)); } if (integer.parseint(firsthalf) == integer.parseint(secondhalf)) { reply = * j; system.out.println(answer); } } } } } } }
this code for(int k = stringnum.length(); k < stringnum.length()/2; k--)
you have comparing messed (< instead of >) , start stringnum.length(), while lastly character @ position stringnum.length() - 1.
however there lot of things can improve codewise:
no need"abc"+(""+'d')
, can straight "abc"+'d'
no need integer.parseint("123") == integer.parseint("123")
, can straight "123".equals("123")
java
Comments
Post a Comment