1
$\begingroup$

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?

$\endgroup$
1
  • $\begingroup$ It is customary to stay responsive to comments as it is to provide feedback for the answers posted. $\endgroup$
    – Syed
    Commented Nov 7, 2022 at 11:48

2 Answers 2

2
$\begingroup$

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.

$\endgroup$
5
  • $\begingroup$ I believe that this is a solution! $\endgroup$
    – YaGoi Root
    Commented Nov 7, 2022 at 14:02
  • 1
    $\begingroup$ Appreciate the answer. It could be even more straightforward with "Character8" format. $\endgroup$
    – Ben Izd
    Commented Nov 7, 2022 at 14:18
  • $\begingroup$ @BenIzd thanks, updated answer with your suggestion. $\endgroup$
    – Nasser
    Commented Nov 7, 2022 at 14:24
  • 1
    $\begingroup$ You can use the Export's third argument to specify the format to make it even better ;) $\endgroup$
    – Ben Izd
    Commented Nov 7, 2022 at 14:28
  • $\begingroup$ @BenIzd Yes, this is even better, I did not know about "character8", that is useful format to know. $\endgroup$
    – Nasser
    Commented Nov 7, 2022 at 15:05
2
$\begingroup$
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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.