# How high can Julia count? (code golf)

**URL:** <https://discourse.julialang.org/t/how-high-can-julia-count-code-golf/11763>\
**Category:** General Usage\
**Created:** [June 18, 2018, 7:28pm UTC](https://discourse.julialang.org/t/how-high-can-julia-count-code-golf/11763 "2018-06-18T19:28:05Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [June 18, 2018, 7:28pm UTC](https://discourse.julialang.org/t/how-high-can-julia-count-code-golf/11763/1 "2018-06-18T19:28:05Z")

</div>

(I came across a [fun code golf question](https://codegolf.stackexchange.com/questions/124362/how-high-can-you-count/) 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](https://codegolf.stackexchange.com/users/31516/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](https://codegolf.stackexchange.com/a/166889/8774) 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:

```julia-auto
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. 🙂 )

---

<div class="post-metadata">

**Author:** ![louisponet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/louisponet/32/2070_2.png) [@louisponet](https://discourse.julialang.org/u/louisponet)\
**Post date:** [June 18, 2018, 10:41pm UTC](https://discourse.julialang.org/t/how-high-can-julia-count-code-golf/11763/2 "2018-06-18T22:41:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [June 19, 2018, 1:04am UTC](https://discourse.julialang.org/t/how-high-can-julia-count-code-golf/11763/3 "2018-06-19T01:04:45Z")

</div>

I just try to push your submission a little further 🙂

```julia
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` ?)

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [June 19, 2018, 7:16am UTC](https://discourse.julialang.org/t/how-high-can-julia-count-code-golf/11763/4 "2018-06-19T07:16:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [June 19, 2018, 10:20am UTC](https://discourse.julialang.org/t/how-high-can-julia-count-code-golf/11763/5 "2018-06-19T10:20:32Z")

</div>

> [@Liso](#):
>
> I just try to push your submission a little further 🙂

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.)

> [@Per](#):
>
> It seems you skipped the “all characters must be encoded using a single byte” rule. So we are allowed to use unicode operators ?

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.

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [June 19, 2018, 10:35am UTC](https://discourse.julialang.org/t/how-high-can-julia-count-code-golf/11763/6 "2018-06-19T10:35:38Z")

</div>

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 `ω`).

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [June 19, 2018, 12:52pm UTC](https://discourse.julialang.org/t/how-high-can-julia-count-code-golf/11763/7 "2018-06-19T12:52:12Z")

</div>

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

```julia

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

```

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [June 19, 2018, 1:31pm UTC](https://discourse.julialang.org/t/how-high-can-julia-count-code-golf/11763/8 "2018-06-19T13:31:24Z")

</div>

Julia 0.7 on a 64-bit system:

```julia
π\π
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

```

---

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [June 19, 2018, 1:45pm UTC](https://discourse.julialang.org/t/how-high-can-julia-count-code-golf/11763/9 "2018-06-19T13:45:59Z")

</div>

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

```julia
"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

```julia
Char.size

```

BTW. If you like then feel free to post to [codegolf.stackexchange.com](http://codegolf.stackexchange.com) our improved version! 🙂

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [June 19, 2018, 2:02pm UTC](https://discourse.julialang.org/t/how-high-can-julia-count-code-golf/11763/10 "2018-06-19T14:02:41Z")

</div>

I am not sure if a character repeats though…

```julia
π\π
√∛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

```

---

<div class="post-metadata">

**Author:** ![kdyrhage](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kdyrhage/32/2326_2.png) [@kdyrhage](https://discourse.julialang.org/u/kdyrhage)\
**Post date:** [June 19, 2018, 2:07pm UTC](https://discourse.julialang.org/t/how-high-can-julia-count-code-golf/11763/11 "2018-06-19T14:07:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bernhard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernhard/32/2619_2.png) [@bernhard](https://discourse.julialang.org/u/bernhard)\
**Post date:** [June 19, 2018, 2:22pm UTC](https://discourse.julialang.org/t/how-high-can-julia-count-code-golf/11763/12 "2018-06-19T14:22:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [June 19, 2018, 3:00pm UTC](https://discourse.julialang.org/t/how-high-can-julia-count-code-golf/11763/13 "2018-06-19T15:00:04Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [June 20, 2018, 4:50am UTC](https://discourse.julialang.org/t/how-high-can-julia-count-code-golf/11763/14 "2018-06-20T04:50:39Z")

</div>

> [@Liso](#):
>
> post to [codegolf.stackexchange.com](http://codegolf.stackexchange.com) our improved version

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

---

<div class="post-metadata">

**Author:** ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)\
**Post date:** [June 20, 2018, 5:01am UTC](https://discourse.julialang.org/t/how-high-can-julia-count-code-golf/11763/15 "2018-06-20T05:01:31Z")

</div>

> [@Per](#):
>
> 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 😉

---

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [June 20, 2018, 8:16am UTC](https://discourse.julialang.org/t/how-high-can-julia-count-code-golf/11763/16 "2018-06-20T08:16:26Z")

</div>

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

Changing prompt is easy as

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

```

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

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

```

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [June 20, 2018, 8:41am UTC](https://discourse.julialang.org/t/how-high-can-julia-count-code-golf/11763/17 "2018-06-20T08:41:14Z")

</div>

> [@Keno](#):
>
> just pretend that the source is really latin-1

Does latin-1 really have the `\euler` `ℯ` ? 🙂

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [June 20, 2018, 8:45am UTC](https://discourse.julialang.org/t/how-high-can-julia-count-code-golf/11763/18 "2018-06-20T08:45:30Z")

</div>

> [@Liso](#):
>
> I wanted to use use `|` with `|>` to save parenthesis

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…

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [June 20, 2018, 8:46am UTC](https://discourse.julialang.org/t/how-high-can-julia-count-code-golf/11763/19 "2018-06-20T08:46:24Z")

</div>

To 16:

```julia
π\π
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

```

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [June 20, 2018, 3:15pm UTC](https://discourse.julialang.org/t/how-high-can-julia-count-code-golf/11763/20 "2018-06-20T15:15:24Z")

</div>

```julia
π\π # 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

```

[Next page](https://discourse.julialang.org/t/how-high-can-julia-count-code-golf/11763.md?page=2)
