Results 1 to 4 of 4
  1. #1
    perito is offline Member
    Join Date
    Feb 2008
    Posts
    9
    Rep Power
    0

    Default Math Related Problem

    I'm trying to solve this problem
    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
    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 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?

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    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);
                                  
        }
    }

  3. #3
    perito is offline Member
    Join Date
    Feb 2008
    Posts
    9
    Rep Power
    0

    Default

    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...

  4. #4
    perito is offline Member
    Join Date
    Feb 2008
    Posts
    9
    Rep Power
    0

    Default

    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

  1. Math Related Problem
    By perito in forum Advanced Java
    Replies: 1
    Last Post: 03-21-2008, 08:53 AM
  2. Issue related to browser
    By sachindanayak in forum Java Servlet
    Replies: 0
    Last Post: 02-03-2008, 02:25 AM
  3. Exceptions related to DynaValidatorForm
    By rameshraj in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 12-27-2007, 11:44 AM
  4. Exceptions related to DynaValidatorForm
    By rameshraj in forum Java Servlet
    Replies: 0
    Last Post: 12-26-2007, 10:43 AM
  5. Bean related actions in JSP
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-24-2007, 10:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •