Alignment issue when printing formatted prime numbers in J language

72 views Asked by At

I am a relatively new programmer in J-Lang and recently discovered its efficacy in Code Golfing, where it excels in scoring. I am currently working on a coding problem that involves printing all prime numbers under 100. While I have successfully implemented the code for this task, I am encountering difficulties in formatting the output as desired.

I have referred to the documentation to gain a better understanding of the language, but there are several concepts that I find challenging to grasp. My primary focus is on solving the given problem rather than deeply immersing myself in the intricacies of the language.

The code I have written currently looks like this:

echo p:i.25 1

I was expecting the output to resemble the following:

2
3
5
7
11
13
...

However, the actual output is:

 2
 3
 5
 7
11
13

I have attempted to address the alignment issue by trying different prime modes:

echo [X] p:i.25 1

as well as experimenting with string formatting:

echo ":p:i.25 1

Unfortunately, none of these solutions have resolved my problem, and I am seeking assistance in achieving the desired left-aligned output. Any guidance or suggestions would be greatly appreciated.

2

There are 2 answers

0
Eelvex On BEST ANSWER

You need to 'echo' the atomic results, therefore you should format (":) every item ("0) of p:

echo ":"0 p:i.25
2
3
5
7
11
13
...
0
pmf On

You can use Foreign 8 to format the items.

With p:i.25 1, you produce an array of rank 2 (a two-dimensional array, aka a table or matrix), so you need 8!:1 to format a column. Omitting the format phrase automatically applies a "minimum width" to the representations of the numbers. The formatting is boxed, so use > to open (aka unbox) it:

echo > 8!:1 p:i.25 1

If you chose to produce the numbers in an array of rank 2 in order to make them "vertically aligned" (having single-columned items on rows), know that this is not necessary to get to your desired result. Just produce a single list of atoms (array of rank 1, or array of scalars) by omitting the the second dimenstion (just write p:i.25), and then accordingly use 8!:0 to format the atoms:

echo > 8!:0 p:i.25

Output:

2 
3 
5 
7 
11
13
...