J code golf

64 views
Skip to first unread message

Joseph Novakovich

unread,
May 10, 2025, 6:35:24 AM5/10/25
to fo...@jsoftware.com
Hello,

I came across https://code.golf and tried out a few of the problems. For several of them I'm able to get 1 character longer than the best solutions. For example my solution to https://code.golf/prime-numbers#j is 

echo&>p:i.25

though the best manage 11 characters. Is there some way of echoing with fewer chars? Is there some other trick I'm missing?

Thanks,

Joseph

Marcin Żołek

unread,
May 10, 2025, 7:24:09 AM5/10/25
to fo...@jsoftware.com
It can be done in 11 characters with echo. You are very close to the solution:


Thanks,
Marcin

To unsubscribe from this group and stop receiving emails from it, send an email to forum+un...@jsoftware.com.

Joseph Novakovich

unread,
May 10, 2025, 8:37:39 AM5/10/25
to fo...@jsoftware.com
Fantastic, thanks!


On May 10, 2025, at 07:24, Marcin Żołek <marcin...@students.mimuw.edu.pl> wrote:

It can be done in 11 characters with echo. You are very close to the solution:

Devon McCormick

unread,
May 10, 2025, 2:30:02 PM5/10/25
to fo...@jsoftware.com
Why is "echo" necessary?  Doesn't "p:i.25" give the same result?
--

Devon McCormick

Flâneur


Luke D

unread,
May 10, 2025, 2:47:48 PM5/10/25
to fo...@jsoftware.com
Devon,

c.g requires the results to come from stdout, which isn't implicit in how J is run on the site.


Best Regards, 
Luke De La Cruz

Henry Rich

unread,
May 10, 2025, 3:23:56 PM5/10/25
to fo...@jsoftware.com

Should we define a 1-letter synonym for echo in stdlib?

Henry Rich

Raul Miller

unread,
May 11, 2025, 11:37:21 PM5/11/25
to fo...@jsoftware.com
Single letters are precious, but most things override the z locale.
Still... what would the letter be?

(That said, concise shortcuts for reading from stdin and writing to
stdout Seem Like A Good Idea. I'm kind of not sure why they set J up
there so that default output isn't to stdout. That said, I also expect
any updates to propgate to that site after a period of years.)

--
Raul

Luke D

unread,
May 12, 2025, 2:46:53 PM5/12/25
to fo...@jsoftware.com
James does a good job ensuring language versions stay fairly up to date, and J is no exception, thankfully. A big reason that languages, do not have implicit output that can solve holes (see R for example) is because competitiveness and consistency are valuable to the community.



Best Regards, 
Luke De La Cruz

Henry Rich

unread,
May 12, 2025, 3:29:21 PM5/12/25
to fo...@jsoftware.com
I would love to have something simple that I could put into sentences to display and optionally save values.  The result of the verb needs to match y.  I suggest

E_z_ =: ([ echo) : {{ (x) =: E y }}  NB. display & return y; if x given, assign value of y to x~

Henry Rich

Jan-Pieter Jacobs

unread,
May 12, 2025, 4:17:23 PM5/12/25
to fo...@jsoftware.com
I would like that.

My use cases would be mostly in AoC.
Then one could also use a Boolean variable and E^:var to switch on/off many echo statements in bulk.
For instance if testing on a small sample, but also try on bigger ones, for which all the echo would clobber the output (or crash JAndroid, as it happens).
I can imagine the dyad to be useful too as e.g. 'foo'&E^:condition y to take a look at y when a certain condition holds.

Jan-Pieter

Marcin Żołek

unread,
May 12, 2025, 4:22:17 PM5/12/25
to fo...@jsoftware.com
I have only solved a few code golf tasks, but I'm not sure this change will have a positive impact on the community of code golfers. There are a lot of code golf solutions in J which use verbs from https://code.jsoftware.com/wiki/Guides/J_Shell_Script
Introducing 1-letter versions of these verbs (echo is just one of them) will make all these solutions obsolete. Code golfers will have to change their solutions they have developed over the years. Otherwise worse solutions, but using a shortcut, will take a better place.

E or any other 1-letter verb may confuse users, because primitives are 1-letter such as E. and stdlib currently contains meaningful names.

Marcin

Henry Rich

unread,
May 12, 2025, 5:23:11 PM5/12/25
to fo...@jsoftware.com
That argument has some merit.  Real golf forbids improvements to equipment that would make courses too easy.

Then again, if your argument is fully valid we should avoid making any changes that make it possible to write shorter code, which obviously we wouldn't accept.

I admit that my original idea was to help J gain a few places in the league tables.  I favor giving J a boost in any competition.

Even if my original motivation was wrong, as you suggest, J-P's and my other rationales for having an easy display verb are still worth considering.

Is there good reason that typeout doesn't go to stdout?  Would a command-line switch to send output to stdout be generally useful?

Henry Rich


bill lam

unread,
May 13, 2025, 2:50:38 AM5/13/25
to fo...@jsoftware.com
I believe Jconsole output to libedit or linenoise for editing.
Reply all
Reply to author
Forward
0 new messages