Can you give me half?
Challenge idea taken from: Eliseo D'Annunzio
Task
Provide code that evaluates to 0.5 numerically, i.e. the output must be recognized by your chosen language as a numeric value (Number, float, double, etc), not as a string.
The catch, the characters 0 through to 9 cannot be used.
One example that fits the brief would be the following:
((++[[]][[~~""]]<<++[[]][[~~""]]))**((--[[]][[~~""]]))
which works out to 0.5 in JavaScript.
Scoring
This is a code challenge, where scoring is done in terms of number of unique characters used in the submission. Ties are broken by lowest bytecount.
dc, 3 unique bytes, 138 bytes …
3y ago
[C (gcc)], 8 7 unique c …
2y ago
Ruby, 7 unique, 12 bytes `$ …
3y ago
Embed ESCR, 6 unique character …
3y ago
[Python 3], 5 unique bytes, 28 …
2y ago
Clojure - 15 characters total, …
3y ago
Perl, 3 distinct, 12 bytes …
3y ago
J, 4 3 distinct, 6 bytes `` …
3y ago
[Python 3], 7 distinct charact …
3y ago
Lua 5.4, 3 unique characters …
2y ago
J, 4 unique, 4 char ``` -: …
2y ago
[Julia 1.0], 16 bytes …
3y ago
Ruby, 10 distinct, 35 chars ` …
3y ago
Raku (also Perl polyglot), 5 u …
3y ago
Scala, 15 bytes ```scala '#' …
3y ago
Japt, 1 byte ½ Test …
3y ago
Javascript, 5 Distinct charact …
3y ago
JS, 15 bytes `!![]/(-[]-[]) …
3y ago
[Extended] Dyalog APL, 4 bytes …
1y ago
Golfscript, 8 6 bytes, 5 uniqu …
2y ago
Vyxal, 1 byte ``` . ``` Tr …
2y ago
Fig, 2 unique, 2 total ``` H …
2y ago
MATL, 3 distinct, 3 total ``` …
2y ago
23 answers
Python 3, 5 unique bytes, 28 total
(()==())/((()==())+(()==()))
(
, )
, =
, /
, +
Uses (()==())
to get True
with only 3 unique characters, as well as making the later grouping of (...+...)
free.
0 comment threads
dc, 3 unique bytes, 138 bytes
This challenge is pretty easy if you aim for just 4 unique bytes. For example, you could push the stack depth repeatedly to get ascending integers, set the precision to something positive, divide and then print.
zzzzk/p
And surely it can't go any lower, right? After all, we have to increase the precision (k
), print (p
, n
, f
), do a floating-point operation (v
, ^
, /
, .
), and still get a number from somewhere...
Luckily though, there is actually a narrow path forward thanks to a
and x
. a
pops a number off the stack, casts it to a long
, and pushes its LSB back as a single-character string. Using x
we can then execute that string as a command. This leaves us to choose a third command which should give us some helpful numbers that we can feed into a
. The only options that can give us a range of numbers are z
as well as the hex digits A
through F
. I haven't found a way to tame z
, since it buries every intermediate result you get, so let's just see what useful characters we can get if we choose one of the hex digits.
Program | Resulting character |
---|---|
AAa |
n |
AAAAa |
f |
AAAAAAa |
F |
CCCa |
4 |
DDDDa |
k |
FFFFFFa |
i |
At first, this might not look too promising, but if we choose F
as our third command, we can use the i
to change the input base to 15 and basically get a reroll on our commands. 🤞
Program | Resulting character |
---|---|
FxFFFFFFaxFFFFFa |
/ |
FxFFFFFFaxFFFFFFFa |
? |
The ?
doesn't help us, since we don't get any input, the /
however is game-changing. By carefully placing the right constants on the stack before and after the base change we can later use /
to efficiently calculate the ASCII value of any command. We use this to first set the precision to 15 and then sum, divide and print three pre-placed F
s (as in F k F F F + / n
) to do the actual calculation.
Finally, here is an annotated version of the complete program:
F F F # three 15s for calculating 0.5
FFFFFFFFFFFFF # "+" numerator
FFFFFF # "+" denominator 1
F # argument for k
FFFFFFFFFFF # "k" numerator
FFFFF # "k" denominator 1
F # argument for i
FFFFFFax # execute "i" to switch base
FFFFFax # "k" division 1
F # "k" denominator 2
FFFFFax # "k" division 2
ax # execute "k" to set precedence
FFFFFax # "+" division 1
F # "+" denominator 2
FFFFFax # "+" division 2
ax # execute "+" to add two 15s
FFFFFax # divide 15 by 30
FFFFFFFFFFFF # "n" numerator 1
FFF # "n" denominator 1
FFFFFax # "n" division 1
FFFF # "n" denominator 2
FFFFFax # "n" division 2
ax # execute "n" to print the result
We can get rid of any whitespace by placing x
s between constants since they act as no-ops on numbers.
FxFxFxFFFFFFFFFFFFFxFFFFFFxFxFFFFFFFFFFFxFFFFFxFxFFFFFFaxFFFFFaxFxFFFFFaxaxFFFFFaxFxFFFFFaxaxFFFFFaxFFFFFFFFFFFFxFFFxFFFFFaxFFFFxFFFFFaxax
0 comment threads
C (gcc), 8 7 unique
cos(cos-cos)/(cos(cos-cos)-(-cos(cos-cos)))
-1 unique character thanks to @orthoplex
Ruby, 7 unique, 12 bytes
$$.fdiv$$+$$
Ruby, 8 unique, 13 bytes:
$$.fdiv $$+$$
Ruby, 9 unique, 22 bytes:
"i".size.fdiv"ii".size
Ruby, 9 unique, 24 bytes:
Math::E./Math::E+Math::E
0 comment threads
Embed ESCR, 6 unique characters, 19 bytes
[/ [*] [+ [*] [*]]]
Each [] pair of brackets encloses an in-line function. The first token in a function is the function name, and subsequent tokens, if any, the parameters. Functions can be nested.
The * function multiplies a series of numbers. The product is initialized to 1 before the first term. A side-effect is that just "*" without any parameter returns 1. The "+" function adds two 1s to make 2. The "/" function divides the first parameter by the second, so 1 divided by 2. The result is 1/2.
The spaces are enforced for readability, and to simplify the parsing rules. This NOT a golfing language.
Perl, 3 distinct, 12 bytes
s//yy//y/y//
Prepends yy
to the start of $_
, and then transliterates them, dividing the count of each.
0 comment threads
J, 4 3 distinct, 6 bytes
-:_-:_
Half of _ -: _
which is asks if infinity matches infinity. Thanks to @xash in the APL farm discord for saving the 3 unique bytes and orthoplex for catching my blunder.
Clojure - 15 characters total, 5 unique
Inspired by the Embed ESCR
solution above, I realized I could do something very similar in Clojure:
(/(*)(+(*)(*)))
This returns 1/2, which is a Ratio in Clojure.
(*) - *
with no arguments returns 1.
(+...) - +
returns the sum of its arguments
(/...) - /
divides the first argument by the second, returning a Ratio.
0 comment threads
Scala, 15 bytes
'#'.toFloat/'F'
Characters in Scala kinda get casted to integers. This is usually annoying, but it's useful when golfing. #
is 35 and F
is 70, and the .toFloat
prevents it from doing integer division and truncating to 0.
0 comment threads
J, 4 unique, 4 char
-:=_
How it works:
-: results in half of what's on the right
= results in signum of what's on the right
_ is infinity
0 comment threads
Japt, 1 byte
½
Jelly, Vyxal, 1 byte
.
Yeah, golf langs still kind of kill this challenge.
JS, 15 bytes
!![]/(-~[]-~[])
Basically typed random characters into js console and it worked.
Raku (also Perl polyglot), 5 unique, 13 bytes
!()/(!()+!())
() or empty list coerces to its length which is 0 in math operations and !0 is True which coerces to 1
0 comment threads
Lua 5.4, 3 unique characters
#'#'/#'##'
[...] scoring is done in terms of number of unique characters used in the submission.
- Hashtag #
- Single quote '
- Slash /
0 comment threads
Fig, 2 unique, 2 total
H!
Halves the negation of the empty string when no input is given.
0 comment threads
Golfscript, 8 6 bytes, 5 unique
!).!(?
Explanation:
! # negate , no value makes it 1
) # increment -> 2
. # duplicate 2
! # negate 2 = 0
( # decrement -> -1
? # exponentiate, 2^-1 = 1/2
Original solution:
!)""!((?
2 comment threads