Stand With Ukraine

Sunday, January 10, 2010

How to save data to file in J

J is a very interesting, concise and efficient language that I often use for mathematical computaions.
In this post I will write the different ways of doing things in J.

I needed to generate a lot of primes in the range (1e8, 1e9), and then save them to file in order to play with them in Python. I tried to play with it in J but there it tries to create the matrix for this procedure which requires a lot of memory. Here is how I wrote the data to file in J:

primes =: p:i.6e7 .NB Generate primes from 2 to 1190494771
primes =: ((primes < 1e9)*.(primes > 1e8)) # primes .NB select primes with 9 digits only
primes =: ": primes .NB Convert the list of primes to the list of strings
(primes,LF) 1!:2 <'/home/san/primes.txt' .NB write the primes to the file

So this way I got a text file of the size 430 MB :)
And I processed this file using python in 687.204969883 seconds.

No comments: