How high can Julia count? (code golf)

(I came across a fun code golf question that wasn’t just about minimizing byte count, and thought people here might enjoy it too.)

Challenge:

Your task is to write as many programs / functions / REPL snippets as you can, where each one outputs / prints / returns an integer. The first program must output the integer 1, the second one 2 and so on.

You can not reuse any characters (except whitespace) between the programs. So, if the first program is: one(Int), then you may not use the characters’o’, ‘n’, ‘e’, ‘(’, ‘I’, ‘n’, ‘t’, and ‘)’ again in any of the other programs. Note: It’s allowed to use the same character many times in one program.

Scoring:

The winner will be the submission that counts the highest. In case there’s a tie, the winner will be the submission that used the fewest number of bytes in total.

Rules:

  • REPL snippets are allowed!
  • The output must be in decimal. You may not output it with scientific notation or some other alternative format. Outputting floats is OK, as long as all digits that are shown behind the decimal point are 0. So, 4.000 is accepted. Inaccuracies due to FPA is accepted, as long as it’s not shown in the output.
  • ans = , leading and trailing spaces and newlines etc. are allowed.
  • You may disregard STDERR, as long as the correct output is returned to STDOUT
  • You may choose to output the integer to STDERR, but only if STDOUT is empty.
  • Letters are case sensitive a != A.
  • The programs must be independent
  • You must use ASCII-digits in the output

Explanations are encouraged!

(The challenge text above is almost entirely from the linked StackExchange post by Stewie Griffin, edited for clarity+to remove rules that didn’t make sense/made it (subjectively) less fun in Julia.)

I made an attempt at it and got up to 13 (but I’d encourage you to attempt it yourself before being influenced by my answer).

For eg., the following would be a valid answer:

2÷2

using Primes
prime(one(Int))

3
4
5
6
7
8
9
10

This counts as a series of REPL snippets that count up to 10. (If you use a multiline snippet for any number, like the snippet for outputting 2 here, I suggest using empty line before and after it for clarity.)

In your post, please mention if you’re posting REPL snippets or programs, and whether it’s in Julia 0.6 or 0.7.

(To moderators: This seemed the forum category that seemed to fit more than the others, so I’ve posted it on Usage. It doesn’t exactly fit under “Off Topic” since it is about Julia and its usage - but I’ll be happy if this inspires a new “Fun with Julia” or similar forum category. :slight_smile: )

3 Likes

Oh I’m looking forward to the recursive metaprogramming madness.

EDIT: maybe I misunderstood the rules, if I write one metaprogram that barfs out many other programs, maybe with the same characters, does that count or is that not allowed

I just try to push your submission a little further :slight_smile:

pi\pi
!false<<!false
3
2^2
5
6
7
8
9
count((Any>:Any,Any>:Any,Any>:Any,Any>:Any,Any>:Any,Any>:Any,Any>:Any,Any>:Any,Any>:Any,Any>:Any))
11
4+4+4
'O'-'B'
z=FFTW==FFTW;T=[z z z z z z z z z z z z z z]*[z;z;z;z;z;z;z;z;z;z;z;z;z;z];T[z]

instead of FFTW we could probably use shorter name to reduce number of bytes… (maybe rm ?)

2 Likes

It seems you skipped the “all characters must be encoded using a single byte” rule. So we are allowed to use unicode operators ?

Good work! I especially like the supertype check one, wouldn’t have occurred to me at all.

Note that I spelt it out as pi in my post there since there was a rule about only using single-byte characters, but I’ve taken that rule out here, so you can for eg. use π\π and save the ‘p’ and ‘i’ characters for later. (Also, I used the ldivide since I was hoping to use / later somewhere - but I couldn’t figure out a way to do that.)

Yes. That rule makes sense to keep things fair when different languages are competing, since languages that allow more number of distinct (multibyte) characters will have an inherent advantage. Here, everyone would have that advantage, so I think it makes for a more fun challenge to allow them.

Suggestion: you can use countnz(((),(),(),(),(),(),(),(),(),())) for 10 (at the cost of another deprecation warning in 0.7, for countnz), and avoid using up ‘A’,‘y’,‘>’ and ‘:’ (14 should use a different variable name than z then, perhaps a Unicode character like ω).

some alternatives. Maybe someone can get it to 15…?


pi\pi
∛8 #!false<<!false 
3 #√9
2^2
5 #22>>2 #11>>1
6
7
!false<<!false<<!false<<!false
9
countnz(((),(),(),(),(),(),(),(),(),()))
11 #55/5 works too
4+4+4
'O'-'B'
z=FFTW==FFTW;T=[z z z z z z z z z z z z z z]*[z;z;z;z;z;z;z;z;z;z;z;z;z;z];T[z]

#of course we would have
[0xa+0,0xb+0,0xc+0,0xd+0,0xe+0] #all of them work with "-0" too
15==5+5+5
16==!false<<!false<<!false<<!false<<!false

Julia 0.7 on a 64-bit system:

π\π
true<<true
3
∛Sys.WORD_SIZE
5
6
7
8
9
log(min(ℯ)min(ℯ)min(ℯ)min(ℯ)min(ℯ)min(ℯ)min(ℯ)min(ℯ)min(ℯ)min(ℯ))
11
4+4+4
'U'-'H'
c=cp==cp;T=[c c c c c c c c c c c c c c]*[c;c;c;c;c;c;c;c;c;c;c;c;c;c];T[c]
0xF|0
2 Likes

I wanted to use use | with |> to save parenthesis in call - for example:

"aaa" |> length

But I had problem to find true without > so with your countnz help it could be used?

I was hoping also to use dot

Char.size

BTW. If you like then feel free to post to codegolf.stackexchange.com our improved version! :slight_smile:

I am not sure if a character repeats though…

π\π
√∛Sys.WORD_SIZE
3
4
22>>2
6
7
8
9
log(min(ℯ)min(ℯ)min(ℯ)min(ℯ)min(ℯ)min(ℯ)min(ℯ)min(ℯ)min(ℯ)min(ℯ))
11
0xC|0
'U'-'H'
c=cp==cp;T=[c c c c c c c c c c c c c c]*[c;c;c;c;c;c;c;c;c;c;c;c;c;c];T[c]
5+5+5
!false<<!false<<!false<<!false<<!false

s and l are both used twice, in Sys and false, and log and false, respectively.

1 Like

Thanks. in that case one could replace !false with true in the last line.

function findrepeats(str)
    allchars = Char[]
    repchars = Char[]
    for linechars in unique.(split(str, "\n"))
        append!(repchars, allchars ∩ linechars)
        append!(allchars, linechars)
    end
    repchars
end
1 Like

The rules on stackexchange do not allow unicode, so the code would have to be rewritten, and probably not count as high.

Well, technically the rules are saying that each character has to be encoded as a single byte, so just pretend that the source is really latin-1 and as long as no byte is repeated you’re fine :wink:

1 Like

Or we could change REPL to interpret one letter lines as numbers :wink:

Changing prompt is easy as

Base.active_repl.interface.modes[1].prompt="$VERSION> "

how difficult could be inject this function somewhere into REPL machinery?

function replacer(x) 
  if length(x)==1  # if length is 1
    string(UInt32(x[1])-55) 
  else x 
  end
end

Does latin-1 really have the \euler ? :slight_smile:

That’s not a problem if you switch from or to xor. Maybe you can use that to get to 17. Another way of writing |> length is |> find |> pop!, but that also reuses characters…

To 16:

π\π
true<<true
3
∛Sys.WORD_SIZE
5
6
7
8
9
log(min(ℯ)min(ℯ)min(ℯ)min(ℯ)min(ℯ)min(ℯ)min(ℯ)min(ℯ)min(ℯ)min(ℯ))
11
0xC⊻0
'U'-'H'
c=cp==cp;T=[c c c c c c c c c c c c c c]*[c;c;c;c;c;c;c;c;c;c;c;c;c;c];T[c]
4+4+4+4/4+4/4+4/4
2^2^2
1 Like
π\π # 1 
#alternative spelling: φ*φ%φ
true<<true # 2
3 #3
∛Sys.WORD_SIZE # 4
γ/γ + γ/γ+ γ/γ+ γ/γ+ γ/γ # 5
6 # 6
7 # 7
8 # 8
9 # 9
log(min(ℯ)min(ℯ)min(ℯ)min(ℯ)min(ℯ)min(ℯ)min(ℯ)min(ℯ)min(ℯ)min(ℯ)) # 10
11 # 11
4444 & 44 #12 
'U'-'H' #13 
c=cp==cp;T=[c;c;c;c;c;c;c;c;c;c;c;c;c;c]⋅[c;c;c;c;c;c;c;c;c;c;c;c;c;c];T[c] #14
0xF ⊻ 0 #15
2^2^2 # 16
555>>5 #17
3 Likes