|
-
Mar 19th, 2008, 04:16 PM
#1
Thread Starter
Addicted Member
Math Related Problem
I'm trying to solve this problem
An irrational decimal fraction is created by concatenating the positive integers:
0.12345678910 1112131415161718192021...
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
Here is my code
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 waited about 40 mins and the code showed no output
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?
Im using Visual Studio 2008 Professional Edition
.Net Framework 3.5
-
Mar 20th, 2008, 04:57 AM
#2
Hyperactive Member
Re: Math Related Problem
ur first mistake is on this line
strNbr= strNbr + i;
on this line, if u add a number on the current string it will be like
1, then 12, then 123, then 1234
so u will end up will something massive in the end hence the processing time is big. imo first store this number as an int then convert it to string later.
Regards,
Please go to the Thread Tools menu and click Mark Thread Resolved when your post is answered 
If someone helped you today then please consider rating their post.
-
Mar 20th, 2008, 11:11 AM
#3
Thread Starter
Addicted Member
Re: Math Related Problem
yea, thats exactly what i want
1 then 12 then 123... building a HUGE number, so I thought putting as an integer wont be a smart idea
look at the question
An irrational decimal fraction is created by concatenating the positive integers:
0.123456789101112131415161718192021...
the numbers are lining up near each other...
anyone have any idea how to solve this problem?
Im using Visual Studio 2008 Professional Edition
.Net Framework 3.5
-
Mar 20th, 2008, 11:21 AM
#4
Thread Starter
Addicted Member
Re: Math Related Problem
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
Im using Visual Studio 2008 Professional Edition
.Net Framework 3.5
-
Mar 20th, 2008, 12:47 PM
#5
Hyperactive Member
Re: Math Related Problem
oh ic..sorz i miread the initial question, hehe
Please go to the Thread Tools menu and click Mark Thread Resolved when your post is answered 
If someone helped you today then please consider rating their post.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|