1

I would like to be able to generate data files like the ones provided with NIST SP 800-22 (binary expansions of irrational constants), which contain data akin to

1010011111001

According to the documentation the following Mathematica code is used to generate the data:

BinExp[num_,d_] := Module[{n,L},
                     If[d > $MaxPrecision, $MaxPrecision = d];
                        n = N[num,d];
                        L = First[RealDigits[n,2]]
                     ];

SE = BinExp[E,302500]; Save["data.e",{SE}];

However, when I run said code I get a file that begins with

SE = {1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, ...

What can I do to make the saved file like th first example of just the 1s and 0s?

YaGoi Root
  • 45
  • 3
  • It is customary to stay responsive to comments as it is to provide feedback for the answers posted. – Syed Nov 07 '22 at 11:48

2 Answers2

2

This works. But may be there is better way

SetDirectory[NotebookDirectory[]]
binExp[num_, d_] := 
  Module[{n, L}, If[d > $MaxPrecision, $MaxPrecision = d];
   n = N[num, d];
   L = First[RealDigits[n, 2]]];

SE = binExp[E, 302500]

Export["data.txt", StringJoin[ToString[#] & /@ SE]];

Screen shot of the file (the line is very long, over 1 million digits) this the start of it:

enter image description here

Update

Another option instead of explicitly converting the output to string is to use Character8 for format thanks to suggestion by Ben Izd below.

 Export["data.txt", SE, "Character8"]

Both methods give the same exact output file.

Nasser
  • 143,286
  • 11
  • 154
  • 359
2
BinExp[num_, d_] := IntegerString[Round[2^d num], 2]
SE = BinExp[E, 302500];
Export["data.txt", SE]

Maybe use Floor instead of Round, depending on what you need for the last digit.

Roman
  • 47,322
  • 2
  • 55
  • 121