Results 1 to 4 of 4
Thread: Math Related Problem
- 03-19-2008, 10:40 PM #1
Member
- Join Date
- Feb 2008
- Posts
- 9
- Rep Power
- 0
Math Related Problem
I'm trying to solve this problem
Here is my codeAn irrational decimal fraction is created by concatenating the positive integers:
0.123456789101112131415161718192021...
It can be seen that the 12th digit of the fractional part is 1.
If dn represents the nth digit of the fractional part, find the value of the following expression.
d1 × d10 × d100 × d1000 × d10000 × d100000 × d1000000
I waited about 40 mins and the code showed no output :ehh:Java Code:public class Test { public static void main(String[] args) { String strNbr=""; for(long i=1;i<1000000;i++) strNbr= strNbr + i; //Find the digits int d1=1,d10,d100,d1000,d10000,d100000,d1000000; int product=0; d10=Integer.parseInt(strNbr.substring(9, 10)); d100=Integer.parseInt(strNbr.substring(99, 100)); d1000=Integer.parseInt(strNbr.substring(999, 1000)); d10000=Integer.parseInt(strNbr.substring(9999, 10000)); d100000=Integer.parseInt(strNbr.substring(99999, 100000)); d1000000=Integer.parseInt(strNbr.substring(999999, 1000000)); product = d1 * d10 * d100 * d1000 * d10000 * d100000 * d1000000; System.out.println(product); } }
I know the loop is so long and there must be a better method to do this, but I cant think of a better method than using a string to store the number, since using a integer or double would lead to an extremely large number and would be very hard to locate d10-d100-d1000....
so what do you think I should do to solve this problem?
- 03-19-2008, 11:32 PM #2
d1 × d10 × d100 × d1000 × d10000 × d100000 × d1000000
Using the properties of numbers we can write this
d1 * d1 * d1 * d1 * d1 * d1 * d1 * Math.pow(10, 1000000000000000000000)
Java Code:public class Test { public static void main(String[] args) { // d1 × d10 × d100 × d1000 × d10000 × d100000 × d1000000 int terms = 7; int n = 1*10+1; long value = n; long powersOfTen = 0; for(int i = 1; i < terms; i++) { value *= n; for(int j = 0; j < i; j++) powersOfTen += 1; } System.out.printf("value = %d * powersOfTen = %d%n" + "expression evaluates to %d * 10^%d%n", value, powersOfTen, value, powersOfTen); System.out.printf("value should = %d " + "powersOfTen should = %d%n", (long)Math.pow(n, 7), 6+5+4+3+2+1); } }
- 03-20-2008, 05:13 PM #3
Member
- Join Date
- Feb 2008
- Posts
- 9
- Rep Power
- 0
no no no
you got it all wrong
d1 ... d1000000.... are variable names
d1 is 1
the others are numbers between 0 and 9
the value you provided is wrong...
- 03-20-2008, 05:22 PM #4
Member
- Join Date
- Feb 2008
- Posts
- 9
- Rep Power
- 0
ok here's how i solved it..
this is not a good way but it solved it
i looped till 100000 (a missing zero) which will not take so long to end, got the numbers d1,d10....d100000
so I still have d1000000 to find
since d1000000 is between 0 and 9
I tried all the numbers and found out that 210 is the correct answer
Similar Threads
-
Math Related Problem
By perito in forum Advanced JavaReplies: 1Last Post: 03-21-2008, 08:53 AM -
Issue related to browser
By sachindanayak in forum Java ServletReplies: 0Last Post: 02-03-2008, 02:25 AM -
Exceptions related to DynaValidatorForm
By rameshraj in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 12-27-2007, 11:44 AM -
Exceptions related to DynaValidatorForm
By rameshraj in forum Java ServletReplies: 0Last Post: 12-26-2007, 10:43 AM -
Bean related actions in JSP
By Java Tip in forum Java TipReplies: 0Last Post: 12-24-2007, 10:04 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks