Results 1 to 4 of 4
Thread: Help with type casting arrays
- 02-01-2011, 02:00 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 56
- Rep Power
- 0
Help with type casting arrays
I am doing Project Euler, a mathematical challange for programmers. Problem 16 wants me to find the sum of the digits of the integer 2^1000. This program will find the number, but I am having trouble converting the character array to an integer array.
The output of the program: 21430172143725346418968500981200036211228096234110 672148875007767407021022498722Java Code:import java.math.BigInteger; import java.lang.Integer; class euler16 { public static void main(String[] args) { BigInteger n = BigInteger.valueOf(2); BigInteger two = BigInteger.valueOf(2); boolean done = false; int times = 0; int num; String c; while(!done) //works out 2^1000 { n = n.multiply(two); times++; if(times == 1000) done = true; } String numbers = n.toString(); //Converts 2^1000 to a string System.out.println(numbers); System.out.println(); char[] numberArray = numbers.toCharArray(); //Converts the string to a character for(int e = 0; e < numberArray.length; e++) { System.out.print(numberArray[e]); } System.out.println(); System.out.println(); int[] sumArray = new int[numberArray.length]; //Creates an integer array that is the same length as the character array for(int e = 0; e < numberArray.length; e++) //Here lies the problem, this is supposed to convert the charcters to integers and put them into the int array { sumArray[e] = (int)(numberArray[e]); } for(int e = 0; e < sumArray.length; e++) //When the array is outputted, it is completely different { System.out.print(sumArray[e]); } } }
44986396757631391716255189345835106293650374290571 384628087196915514939714960786
91355496484619708421492101247422837559083643060929 499671638825347975351183310878
92154125829142392955373084335320859663305248773674 411336138752
21430172143725346418968500981200036211228096234110 672148875007767407021022498722
44986396757631391716255189345835106293650374290571 384628087196915514939714960786
91355496484619708421492101247422837559083643060929 499671638825347975351183310878
92154125829142392955373084335320859663305248773674 411336138752
50495251484955504952515550535152545249565754565348 485756495048484851545049495050
56485754505152494948545550495256565553484855555455 524855485049485050525756555050
52525756545157545553555451495157495549545053534956 575152535651534948545057515453
48515552505748535549515652545056485655495754574953 534952575157554952575448555654
57495153535257545256525449575548565250495257504948 495052555250505651555353574856
51545251485448575057525757545549545156565053515255 575553515349495651514948565556
57504953524950535650574952505157505753535155514856 525151535150485653575454515148
53505256555551545552524949515154495156555350Press any key to continue . . .
Thanks!
- 02-01-2011, 02:16 AM #2
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 4
What kind of trouble you are encountering in converting character to integer?
Have you tried Integer.parseInt?
- 02-01-2011, 02:33 AM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Like stated by the poster above, if you use Integer.parseInt it is much simpler. Use the biginteger.toString() and think how you can use Integer.parseInt to solve this. Another thing, you don't need to import java.lang.Integer, all java.lang package items come standard and never need to be imported
Check out the methods for BigInteger, the loop you use to find 2^1000 is not needed because of a bigint method.
Finally, with this problem you can also create a method for finding digitsum which uses / and %, or, for BigInt .mod and .divide.
- 02-01-2011, 03:44 AM #4
Member
- Join Date
- Sep 2010
- Posts
- 56
- Rep Power
- 0
Similar Threads
-
Type casting
By kiros88 in forum New To JavaReplies: 17Last Post: 09-02-2010, 01:58 PM -
Type Casting
By Shaheen Mohamed in forum New To JavaReplies: 6Last Post: 08-17-2010, 07:56 PM -
help with type casting.
By ramsrocker in forum Java AppletsReplies: 15Last Post: 02-26-2009, 11:28 PM -
type casting
By alvations in forum New To JavaReplies: 1Last Post: 10-13-2008, 07:07 PM -
Type Casting Help
By rhm54 in forum New To JavaReplies: 2Last Post: 02-07-2008, 12:06 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks