-6

I want to sum the digits of the first 100.000 digits of pie (I have text with the numbers), but I want to do it in a specific form.

I want to put the digits in a matrix (or whatever is convenient as a placeholder) with a small number of cells predefined by me and overlap the digits in those cells.

What I mean is this: lets say we have this list of numbers (31415926535)

and lets say we have 4 cells j,j1,j2,j3

in the first run j=3,j1=1,j2=4,j3=1 notice that each cell has been assigned with a number of the list and and the digits that are left in the list are (5926535) so I want to overlap them so that j=8 (since its value was 3 and now 5 is added), j1=10 (1 + 9) , j2=6 j3=7

And I want to do that for all my 100k digits

But I want to add an other twist on top of that

I want to print results after x amount of cycles (by cycles I mean one run like in the above example assigning j,j1,j2,j3 with 3,1,4,1 would be the 1st cycle) as well as the final result.

I would prefer a simple language such as python or haskell

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
papajo
  • 167
  • 1
  • 8

2 Answers2

1

This would be a complete implementation in Haskell

import Data.List.Split (chunksOf)
import Data.List
import Data.Char (digitToInt)

main :: IO ()
main = do digits <- map digitToInt <$> readFile "pifile.txt"
          let summator = foldl1' (zipWith (+)) . chunksOf 4
          print $ summator digits

I will update this with some explanation later this day.

Update @Comments

main = do digits <- map digitToInt <$> readFile "pifile.txt"
              let summator = scanl1' (zipWith (+)) . chunksOf 4
              mapM_ print $ take 100 $ summator digits
epsilonhalbe
  • 15,637
  • 5
  • 46
  • 74
  • Thanks I think I get most of whats happenning here :D only "blury" thing for me is the "(zipWith (+))" I suppose this adds each digit that lands over the individual chunk? – papajo Apr 27 '16 at 12:52
  • exacly `zipWith (+) [1,2,3,4] [5,6,7,8] => [6,8,10,12]` note if one list is shorter than the other `zipWith` shrinks the resultlist to the size of the smaller, so you can handle infinite lists as well `zipWith (+) [1] [1..] => [2]` – epsilonhalbe Apr 27 '16 at 12:56
  • I havent run the code yet (will do very soon when I go back home and give my feedback if necessary or select this as an answer if everything runs smooth ) I just have my doubts about the print seems to me that the result will be a list of sums of each chunk while I need something more than that (inbetween results of lets say the first 100 "zipWith"s – papajo Apr 27 '16 at 13:03
  • Also if I wanted to do an other zipWith in parraler (for example one that adds chunks of 4 and one that adds chunks of lets say 8 and then another of 12 etc and print all the result with the inbetween results included) how could I do all together at once instead of run the code several times? could I just add lines like let summator = foldl2' (zipWith (+)) . chunksOf 8 let summator = foldl3' (zipWith (+)) . chunksOf 12 And then what would the prin look like (having in mind also the "inbetween results" I meantioned above – papajo Apr 27 '16 at 13:03
  • and sorry for the bad format still didnt figured out how to draw new lines in the comment section :P I am new to stuckoverflow :P – papajo Apr 27 '16 at 13:04
0
bins = [0,0,0,0] # four bins
for i,n in itertools.izip(itertools.cycle([0,1,2,3]),my_digits):
    bins[i] += int(n)

I guess... and just print it sometimes

bins = [0,0,0,0] # four bins
num_bins = 4
for i,n in enumerate(my_digits):
    bins[i%num_bins] += int(n)
    if i % 100: print( bins ) # every 100 items print 
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • thanks but a big part of my problem is how to insert my 100k digits I have them in a txt format so how could I link "my_digits" to that file? or is there a sensible way to make python calculate pi digit by digit as it goes to fill it into the bins? – papajo Apr 27 '16 at 00:41
  • `my_digits = iter(lambda:my_open_file.read(1),None)` – Joran Beasley Apr 27 '16 at 04:52