Nairaland ForumWelcome, Guest: Join Nairaland / Login / Trending / Recent / NewStats: 1229469 members, 1614443 topics. Date: Friday, 29 August 2014 at 01:00 PM |
Nairaland Forum / Science/Technology / Programming / Java Challenge (1048 Views)
Please Could You Help Me With This Java Challenge (1) (2) (3) (4)
Java Challenge by logic101: 10:58pm On Oct 27, 2011 |
Hi guys i have a written a code which does the following. multiply two three digit numbers and finds the largest palindrome i have the code written but i would like the house to implement it in different ways so we can all learn public class LargestPalindrome { /* * a boolean method to check if a number is a multiple of 11 * it returns a true or false value */ public static boolean isMultipleOf11(int num){ return(num%11==0); } /* * a boolean method is palindrome which checks if a string/number is a palindrome * it returbs a true or false value */ public static boolean isPalindrome(String c) { for(int i=0;i<c.length()/2;i++) { if(c.charAt(i)!=c.charAt(c.length()-1-i)) return false; } return true; } // a method that multiply two numbers public static int multiply(int a,int b){ assert( (a>=100 && a<=999 )&& (b>=100 && b<=999)); return a*b; } // a boolean method which chekcs if any of the two 3 digit numbers is a multiple of 11 public static boolean can11DivideOneOFtheNumbers(int a, int b){ return (a%11==0 || b%11==0); } /*A method which gets the largest palindrome number from the product of two three * digit numbers. * it returns an int . * */ public static int LargestPalindrome(){ int a=999; int b=999; int sixdigitnumber=0; boolean notfound=false; while(notfound==false){ if(can11DivideOneOFtheNumbers(a,b)){ sixdigitnumber=multiply(a,b); if(isPalindrome(""+sixdigitnumber)){ notfound=true; } else { /* * checks if the int in b is still a three digit number * if true it just subtracts 1 from it * if not it subtracts 1 from int a and makes b=a */ if(b>=100) b--; else{ a=a-1; b=a; } // inner else } //else }//outermost if else // decreasing the b or a if(b>=100) b--; else{ a=a-1; b=a;} } //end of while return sixdigitnumber; }//method public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(LargestPalindrome()); } } |
Re: Java Challenge by Mobinga: 1:59pm On Oct 30, 2011 |
That is a lot of code for such a simple task! Its Project Euler 4 right? I've done this before. To speed this up efficiently, and since you want the largest : 1 - You have to use 900 and above as test cases since the largest palindrome would fall in that range. 2 - You should count down from 999 and below since the first palindrome gotten would be obviously the largest in the 3-digit range. Smart abi? Here. package euler; public class Pr4 { public static void main(String [] args){ long start = System.currentTimeMillis(); boolean bool = false; for(int o = 999; o>900; o--){ for(int n = 999; n>900; n--){ long vin = (long) o*n; String m = Long.toString(vin); String s2 = new StringBuffer(m).reverse().toString(); if( m.equals(s2)){ System.out.println(s2); bool = true; break; } } if(bool){ long stop = System.currentTimeMillis(); System.out.println("Time taken : " + " " + (stop - start) + " milliseconds " ); break;} } } } ------------------------------------------------- The output on my machine is 906609 Time taken : 5 milliseconds See it in action : http://ideone.com/1EB4A |
Re: Java Challenge by logic101: 4:01pm On Oct 30, 2011 |
Good one mobinga , I have a question for you , my code outputs the right result until when my two digits are 7 digit numbers but if i change my range for b it works.Question is there a blueprint to fix a range no matter the amount of digits. |
Re: Java Challenge by Fayimora(m): 5:54pm On Oct 30, 2011 |
Nice one Mob. You can even use less memory and gain more speed by not printing in a loop, remember IO is slow. You coud use a BufferedReader to append your results and print once. Might not be obvious for a trivial task but its worth it when you are doing something very deep like recursive backtracking and the likes, Offtopic: Did anyone here partake in the IEEEXtreme programming competition? |
Re: Java Challenge by naijaswag1: 10:09pm On Oct 30, 2011 |
Fayimora: when the folks on nairaland are anti-agorist? hmm,you want to find folks doing icpc,codejam,codesprint,topcoder etcetera look somewhere else not here, |
Re: Java Challenge by Mobinga: 10:58am On Oct 31, 2011 |
Fayimora: Yeah thanks for the tip! I'm only calling standard output once then it breaks out. logic101: I don't understand, please clarify. |
Re: Java Challenge by Fayimora(m): 11:13am On Oct 31, 2011 |
naija_swag: Lol u r crazy, |
Re: Java Challenge by logic101: 3:07pm On Nov 03, 2011 |
I tried implementing this code for a five digit number but its giving me the wrong output. public static boolean isMultipleOf11(int num){ return(num%11==0); } public static int Largest(){ int z=0;int big=0; ; for(int i=99999;i>90000;i--) { for(int j=i;j>90000;j--) { //z= j*i; if(isMultipleOf11(i)|| isMultipleOf11(j)){ if(isPalindrome(""+ j*i)) { if((i * j)>big){ big=j*i; } }//palindrome if }//outer if } } return big; } |
Re: Java Challenge by Mobinga: 3:38pm On Nov 04, 2011 |
Bros, because your using "is multiple of 11". Just study the code I posted. It works for all values. |
Re: Java Challenge by logic101: 2:16am On Nov 05, 2011 |
Mobinga:thanks bro, i have studied it but if you implement it for when the digits is equal to 5 . The output from your code is 69970996 which is a product of 99994 and 92902 but the desired output should be 9966006699 and the factors are 99979 and 99681. |
Re: Java Challenge by Mobinga: 11:23am On Nov 05, 2011 |
logic101: Wow thanks for this!! It's an Integer overflow. 9966006699 > Integer.MAX_INT = 2 147 483 647 You have to cast the multiplication to longs. I will edit the code above |
Re: Java Challenge by logic101: 5:23am On Nov 06, 2011 |
yup your right it was an integer overflow but lol bad news, it works up till six digits but it returns the wrong output for seven digits.The reason is due to the digits that get multiplied for e.g your code would have o=9999979 and n =9467731 and gives 94677111177649 but the correct output should be 9998017 * 999764 = 99956644665999 for(long o = 9999999; o>9000000; o--){ for(long n = 9999999; n>9000000; n--){ long vin = o * n; |
Re: Java Challenge by Mobinga: 1:30pm On Nov 07, 2011 |
Bros No! The code is correct. See google : http://www.google.com.ng/search?gcx=c&sourceid=chrome&ie=UTF-8&q=9998017+*+999764 Besides one is 7 digits while the other is 6 digits. It falls well behind the maximum value for Longs Long.MAX_VALUE == 9223372036854775807 |
Re: Java Challenge by yommysomguy(m): 2:06am On Nov 08, 2011 |
Mm |
Re: Java Challenge by logic101: 12:03pm On Nov 08, 2011 |
Mobinga:sorry its 9997647 not 999764 .Bro try the code and see the output . |
(0) (Reply)
Matlab Help Needed,please / Android App Developers Charges / Anybody Conversant With QT GUI Toolkit?
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (0) (1) (2) (3) (4) (5) (6) (7) (8) (9) Nairaland - Copyright © 2005 - 2014 Oluwaseun Osewa. All rights reserved. See Nairalist and How To Advertise. 127 |