# Math Related Problem

• 03-19-2008, 11:40 PM
perito
Math Related Problem
I'm trying to solve this problem
Quote:

An 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
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 :ehh:
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-20-2008, 12:32 AM
hardwired
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)
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, 06:13 PM
perito
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, 06:22 PM
perito
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