Endless lazy numbers lists

So, I'm trying to do some number theory, and I used Mathematica, but I thought that Haskell would be more suitable for working with infinite lists (since AFAIK Mathematica does not have a lazy rating). I want Haskell to keep all 1 / x digits in an endless lazy list. So far, my search has not helped break up the coefficient into its digits, which returns a list of digits, not the actual floating point number.

+6
source share
2 answers

We can also implement this as a simple thread producer:

divDigits :: Int -> Int -> [Int] divDigits xy = x `div` y : divDigits (10 * (x `mod` y)) y 

In fact, there are libraries for this kind of "infinite" representation of a value-value using lazy lists, see the Haskell Wiki .

+6
source

Many thanks to Sam Yonn, the link he provided had the correct formula
Used formula:
The nth digit x / y is the first digit (10 ^ (n-1) * x mod y) / y = floor (10 * (10 ^ (n-1) * x mod y) / y) mod 10

The final code looked like this:

 nDigRat :: Int -> Int -> Int -> Int nDigRat num denom n = floor (fromIntegral (10*(10^(n-1)*num `rem` denom)) / fromIntegral denom) `rem` 10 decExpansionRecipRat :: Int -> [Int] decExpansionRecipRat n = map (nDigRat 1 n) [1..] 
+2
source

All Articles