Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why are the answers different?

 
Mahendra Kariya
Greenhorn
Posts: 26
Firefox Browser Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was solving Problem 40 of Project Euler. I tried it in two different ways. But I was getting an incorrect answer for one of the ways. Can someone tell me what is the mistake?

My code follows:

">
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the condition at line 50 correct?

digits can have a max value of 5 and milestone has a value of 10 and increases only.

Regards
 
Mahendra Kariya
Greenhorn
Posts: 26
Firefox Browser Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himanshu Kansal wrote:Is the condition at line 50 correct?

digits can have a max value of 5 and milestone has a value of 10 and increases only.



The output shows that only the last one is incorrect. I mean the millionth digit. Rest all are correct. I cross checked with the second method.
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think it should be:


well i don't know what the actual problem is, but I am just trying to shoot the trouble through your code.
 
Mahendra Kariya
Greenhorn
Posts: 26
Firefox Browser Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himanshu Kansal wrote:i think it should be:



This will be a frequent case. Suppose we want to find the 100th digit. We have the milestone set to 100. If we check digits<milestone, then this condition will be true for every case. But if it is what I have done, this condition will be true once the milestone is reached. And once we reach a particular milestone, we increase it.

I guess your condition will result in an infinite loop!
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please post the problem statement?
 
Mahendra Kariya
Greenhorn
Posts: 26
Firefox Browser Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himanshu Kansal wrote:Could you please post the problem statement?


Problem statement is available at http://projecteuler.net/index.php?section=problems&id=40

Still I have put it here for convenience.

------------------------
An irrational decimal fraction is created by concatenating the positive integers:

0.123456789101112131415161718192021...

It can be seen that the 12^(th) digit of the fractional part is 1.

If d(n) represents the n^(th) digit of the fractional part, find the value of the following expression.

d(1) × d(10) × d(100) × d(1000) × d(10000) × d(100000) × d(1000000)
-------------------------
 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

I'm not sure if this makes a difference, but line 27 - 28

if n < 1000000 then number of digits could be 6 as well??

Dunno if that helps though.

Sean
 
Mahendra Kariya
Greenhorn
Posts: 26
Firefox Browser Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sean Clark wrote:
I'm not sure if this makes a difference, but line 27 - 28

if n < 1000000 then number of digits could be 6 as well??


Thanks Sean, this was the error. At line 27-28, it should be something like this.


Thank you Sean and Himanshu.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic