Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-19-2008, 11:40 PM
Member
 
Join Date: Feb 2008
Posts: 8
perito is on a distinguished road
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?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 03-20-2008, 12:32 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,124
hardwired is on a distinguished road
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); } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-20-2008, 06:13 PM
Member
 
Join Date: Feb 2008
Posts: 8
perito is on a distinguished road
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...
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-20-2008, 06:22 PM
Member
 
Join Date: Feb 2008
Posts: 8
perito is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Math Related Problem perito Advanced Java 1 03-21-2008 09:53 AM
Issue related to browser sachindanayak Java Servlet 0 02-03-2008 03:25 AM
Exceptions related to DynaValidatorForm rameshraj JavaServer Pages (JSP) and JSTL 1 12-27-2007 12:44 PM
Exceptions related to DynaValidatorForm rameshraj Java Servlet 0 12-26-2007 11:43 AM
Bean related actions in JSP Java Tip Java Tips 0 12-24-2007 11:04 AM


All times are GMT +3. The time now is 01:26 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org