0

I'm working on Project Euler's problem 26, which involves finding repetitions in the decimal place when I do division such as 1/6, 1/7, 1/8 etc.

I'm having a hard time getting BigDecimal to give enough precision to find long patterns of repetition. I'd like to force precision of an arbitrary number of places; however, using round(x) still caps at around 18 places.

puts (BigDecimal("1.0") / 7).round(25).to_s("F")
puts (BigDecimal("1.0") / 7).round(25).precs

yields 0.142857142857142857 and [18, 27], respectively. What is the reason it is limited to 18 decimal places? Is there a way to get meaningful decimals past that, or even past the system limit of 27?

sawa
  • 165,429
  • 45
  • 277
  • 381
mikeappell
  • 363
  • 4
  • 17
  • I think this is the wrong approach. Think about how you could generate those digits one-by-one using only integer arithmetic. – Mark Dickinson Sep 18 '15 at 17:30
  • It did occur to me that you could iterate through 1/n, 10/n, 100/n taking only the first decimal place each time. But I was hoping to simply be able to do the division followed by perhaps a regex search for repetition. – mikeappell Sep 18 '15 at 17:39

0 Answers0