19
\$\begingroup\$

The task is to 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.

PLEASE NOTE: This IS NOT a golfing challenge, this is a popularity contest, so creative answers are encouraged. The more obscure and convoluted answers are also encouraged.

One example that fits the brief would be the following:

((++[[]][[~~""]]<<++[[]][[~~""]]))**((--[[]][[~~""]])) which works out to 0.5 in JavaScript.

A bounty of 150 will be awarded as an added incentive for the most voted creative answer. Good luck!

Any questions, feel free to ask.

\$\endgroup\$
11
  • 4
    \$\begingroup\$ TBH, @AviFS it surprises me that people forget there are more different categories of challenges than just code golf... I want my fellow golfers to embrace their inner goofball and provide more esoteric and convoluted answers based on their language of choice... And there was an objective, output half without numeric digits... ;) \$\endgroup\$ Commented Jun 21, 2021 at 9:22
  • 11
    \$\begingroup\$ It's not that the challenge is not code golf, it's that it's the blandest, most generic kind of popularity contest -- a trivial task where we're asked to be creative with "obscure and convoluted answers". At that point all pop cons might as be merged into one challenge that says "write whatever you want, upvote every answer". \$\endgroup\$
    – xnor
    Commented Jun 21, 2021 at 10:08
  • 12
    \$\begingroup\$ @WheatWizard I haven't seen a challenge get 40 answers the day-of in a long time. I wonder if we're not missing pretty clear signs that this is the way to ramp up engagement. It's certainly more inviting, and the barrier to entry is less rigid. Perhaps we're elitists in our ivory towers to be demanding objectivity & strictly defined winners everywhere we go. \$\endgroup\$
    – AviFS
    Commented Jun 21, 2021 at 10:23
  • 6
    \$\begingroup\$ @AviFS 40 answers in a day is not a good sign. In fact it is a very bad sign. \$\endgroup\$
    – Wheat Wizard
    Commented Jun 21, 2021 at 10:26
  • 9
    \$\begingroup\$ Code trolling also caused a massive spike of activity back in 2014 @AviFS, that doesn't mean they were quality questions. More answers doesn't not equal a quality question. Pop-cons have been a controversial topic for years, but it is possible to run a pop-con that isn't just "Do this but creatively" \$\endgroup\$ Commented Jun 21, 2021 at 10:28

52 Answers 52

1
2
3
\$\begingroup\$

Desmos, 23 bytes


\frac{\{\}}{\{\}+\{\}}

(newline is required)

Looks crazy, but it really isn't once you figure out that \{\} is just 1.

The actual crazy part is that it doesn't render properly on Desmos, though it still outputs 0.5.

Try It On Desmos!

\$\endgroup\$
3
  • \$\begingroup\$ Using forward slash: ¶\{\}/(\{\}+\{\}) ( is newline) \$\endgroup\$ Commented Jun 21, 2021 at 6:30
  • 1
    \$\begingroup\$ @fireflame241 Yes, I know that works, but I wanted to have more brackets in the code to make it look more crazy :P. \$\endgroup\$
    – Aiden Chow
    Commented Jun 21, 2021 at 6:46
  • \$\begingroup\$ Oh it's a pop-con. Forgot to read the question, oh well. \$\endgroup\$ Commented Jun 21, 2021 at 6:47
3
\$\begingroup\$

RAMDISP, 19 bytes

[P[[[[]]][/[[]]];]]

RAMDISP is my own programming language, but i made it before this challenge, so i think it's allowed. [i also had to fix a bug i left in it, which i only found after trying to do this challenge]

explanation:

[P - pipes through the following array
  [
    [[[]]] - the value 2.
    [/ - divide
      [[]] - the value 1.
    ] - by the incoming number.
    ; - print the result
  ]
]
\$\endgroup\$
2
\$\begingroup\$

Python 3, 25 bytes

print(len("l")/len("pr"))

Try it online!

Short, sweet, and with only 11 distinct bytes too.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Taking advantage of the fact that int() returns 0, you can get 9 distinct bytes: print(~int()/~(~int()//~int())) \$\endgroup\$
    – orthoplex
    Commented Feb 8, 2022 at 12:25
  • \$\begingroup\$ You can also get 18 bytes total, but it's not very interesting imo: print(-True/~True) \$\endgroup\$
    – orthoplex
    Commented Feb 8, 2022 at 12:26
2
\$\begingroup\$

Charcoal, 57 bytes

I⁻Lα⊘L







https://www.youtube.com/watch?v=AyOqGRjVtls

Try it online!

\$\endgroup\$
5
  • \$\begingroup\$ Interesting, though as I said, this isn't code golf, so whether you golf it down isn't the point... :) \$\endgroup\$ Commented Jun 21, 2021 at 2:38
  • 1
    \$\begingroup\$ @EliseoD'Annunzio Sorry, I'm so used to code golf... \$\endgroup\$
    – Neil
    Commented Jun 21, 2021 at 8:11
  • \$\begingroup\$ No worries, I'm trying to come up with a doozy of a gold challenge over the next week or so. Watch this space. \$\endgroup\$ Commented Jun 21, 2021 at 8:12
  • 1
    \$\begingroup\$ @EliseoD'Annunzio Is this better? \$\endgroup\$
    – Neil
    Commented Jun 21, 2021 at 9:26
  • \$\begingroup\$ I just "tried it online!" You got me... Really? XD \$\endgroup\$
    – Nate T
    Commented Sep 11, 2021 at 2:20
2
\$\begingroup\$

05AB1E, 18 bytes

тT*;тT*·<ŸтT*/ΩΔ;t

Try it online!

Choose a random float in \$[0.5, 2)\$, and iterate \$x\mapsto \sqrt{x \over 2}\$ until convergence.

In theory the iteration converges to \$0.5\$ for all positive real numbers, but there might be floating point issues leading to results close to \$0.5\$.


05AB1E, 7 bytes

XΔNÌoz-

Try it online!

$$ 1-\sum^\infty_{k=2}{1 \over 2^k} = 1-{1 \over 2} = {1 \over 2} $$

X        # start at 1
 Δ       # until the result doesn't change:
  NÌ     #   iteration index N + 2
    o    #   2 ** (N+2)
     z   #   1 / 2**(N+2)
      -  #   subtract this from the current value
\$\endgroup\$
2
  • \$\begingroup\$ Estimating the sum with the first 100 terms would be enough with the floating point precision: тL>ozO \$\endgroup\$
    – ovs
    Commented Jun 21, 2021 at 9:03
  • \$\begingroup\$ True, most languages tend to hit underflow by that number of iterations... \$\endgroup\$ Commented Jun 21, 2021 at 9:44
2
\$\begingroup\$

<>^v

≈:) :) :) :) :( :( :( )≈)?/;
≈    Duplicate top of stack (by default stack is [0])
then the :) and :( do nothing, because : executes only the next characted if the top two elements of the stack are not equal, so it skips over the parentheses.

) Increment top of stack -> stack is now [0, 1]
≈ Duplicate top of stack -> stack is now [0, 1, 1]
) Increment top of stack -> stack is now [0, 1, 2]
? Reverse stack -> stack is now [0, 2, 1]
/ Set top of stack to top of stack / second element of stack -> stack is now [0, 1, 0.5]
; Print top of stack

run online

\$\endgroup\$
1
  • \$\begingroup\$ Not sure why, but looking at this code makes me smile. \$\endgroup\$
    – Adám
    Commented Jun 24, 2021 at 13:08
2
\$\begingroup\$

Barrel, 2 bytes

½

Evaluates to, and prints, 0.5. Not necessarily terribly imaginative, but probably one of the best solutions to this problem in Barrel.

\$\endgroup\$
2
\$\begingroup\$

R, 57 bytes

function(`+`=function(x)as.double(as.roman(x)))+'I'/+'II'

Try it online!

As odd as it may sound, R has a partial support for Roman numerals.

So, in this code we:

  • abuse the plus sign by turning it into a function accepting Roman numerals strings and returning the numeric value
  • we aesthetically compute +I / +II (we actually calculate 1 / 2 because the abused plus sign act as a roman-to-double parser)
  • then we return the result
\$\endgroup\$
1
\$\begingroup\$

Red, 82 bytes

print divide length? next system/locale/days to-float length? system/locale/months

Try it online!

Divides the number of days in a week minus one to the number of months. The current Red built doesn't need to-float.

\$\endgroup\$
1
\$\begingroup\$

R, 82 bytes

function()round(mean(runif(sqrt(exp(exp(pi)))) - rnorm(sqrt(exp(exp(pi))))),pi/pi)

Try it online!

Generates the mean of (\$\sqrt{e ^ {e ^ \pi}}\$ random uniform numbers in the range (0, 1) minus \$\sqrt{e ^ {e ^ \pi}}\$ random numbers from a normal distribution with mean 0 and standard deviation 1) and rounds it to 1 decimal place.

\$\endgroup\$
2
  • \$\begingroup\$ characters 0-9 cannot be used. thats the whole point of the challenge \$\endgroup\$
    – user100752
    Commented Jun 21, 2021 at 7:00
  • \$\begingroup\$ @EliteDaMyth oops fixed now \$\endgroup\$ Commented Jun 21, 2021 at 7:19
1
\$\begingroup\$

ZX Spectrum Basic, 8 bytes

PRINT SGN PI/INT EXP SGN PI

This is actually a well-known compression technique for numeric constants. Plain PRINT 0.5 would require 10 bytes (1 byte for the PRINT keyword, 3 bytes for the ASCII string 0.5, 6 bytes for the float constant)

\$\endgroup\$
2
  • \$\begingroup\$ Which would be great if we were voting on bytes. This is a code popularity contest, not a code golf challenge. \$\endgroup\$ Commented Jun 21, 2021 at 9:20
  • 2
    \$\begingroup\$ @EliseoD'Annunzio Yes, I know. However, I consider my answer not too bad - in particular, because I independently "discovered" the SGN PI expression decades ago, when most programs I saw used PI/PI. And it is somewhat elegant as well. \$\endgroup\$ Commented Jun 21, 2021 at 10:47
1
\$\begingroup\$

Vyxal

\/C⌐\\C/-

Explanation:

\/C          . gets the codepoint of / 
   ⌐         . 1 - x
    \\C      . gets the codepoint of \
       /     . division (to get -0.5)
        -    . subtract from implicit 0

Try it Online!

\$\endgroup\$
1
\$\begingroup\$

C# (Visual C# Interactive Compiler), 30 bytes

Console.Write('$'/(float)'H');

Try it online!

Division of Ascii Codes: '$' = 36, 'H'(alf) = 72

\$\endgroup\$
1
\$\begingroup\$

Python 3, 203 bytes

((True+True)*(True+True+True)**(True+True+True+True)*(True+True+True+True+True)/(True+True+True+True)-(True+True)**(True+True+True)*(True+True+True+True+True)**(True+True))/((True+True)*(True+True)+True)

Heh.

\$\endgroup\$
1
  • \$\begingroup\$ This is invalid as it's a code snippet, not a full program or function \$\endgroup\$
    – emanresu A
    Commented Sep 10, 2021 at 22:18
1
\$\begingroup\$

Python 3

len('check')/len('check-mate')
# Output: 0.5
\$\endgroup\$
2
  • \$\begingroup\$ NICE!!! Definitely creative. The only one so far to use string manipulation to arrive at the result. \$\endgroup\$
    – Nate T
    Commented Jun 21, 2021 at 23:17
  • \$\begingroup\$ This is invalid as it's a code snippet, not a full program or function \$\endgroup\$
    – emanresu A
    Commented Sep 10, 2021 at 22:18
1
\$\begingroup\$

Ruby, 27 bytes

->{($.**=$.).to_f/($.<<$.)}

Try it online!

Using the variable $. which is initially set to zero, and the interesting property 0⁰==1.

\$\endgroup\$
1
\$\begingroup\$

Python 3, 14 bytes

d=True;d/(d+d)

True is basically just 1 in Python, so this is basically just

1/(1+1)

which obviously evaluates to 0.5.

\$\endgroup\$
1
  • \$\begingroup\$ Welcome to the site! Your program should output the value, so you should include print(..) in your answer \$\endgroup\$ Commented Jun 21, 2021 at 17:23
1
\$\begingroup\$

JavaScript (Node.js), 61 bytes

~[] / ~~(Math[`SQRT${-~-~[]}`] * Math[`SQRT${-~-~[]}`]) * ~[]

Try it online!

The ~~ just before the Math.SQRT2 multiplication is needed because Math.SQRT2 * Math.SQRT2 = 2.0000000000000004.

\$\endgroup\$
1
\$\begingroup\$

Python 3

Not very creative but ehh

ord('!')/ord('B')

edit: as pointed out by EasyasPi (thanks), doesn't work for python 2

\$\endgroup\$
2
  • 8
    \$\begingroup\$ Python 2 would do integer division here. \$\endgroup\$
    – EasyasPi
    Commented Jun 21, 2021 at 3:19
  • 2
    \$\begingroup\$ Python 0.66667? \$\endgroup\$
    – Adám
    Commented Jun 21, 2021 at 8:34
0
\$\begingroup\$

Dyalog APL, 5 bytes

÷⌊*≡⍬

Not very creative, but golfy! Try it online.

÷       Inverse              ->   1/2
⌊       Floor                ->   2
*       Exponent [1]         ->   2.71828
≡       Depth                ->   1
⍬       Empty list           ->   []

[1]: Default base is e, so e^1
\$\endgroup\$
5
  • \$\begingroup\$ It sure is. Thanks @Sisyphus! \$\endgroup\$
    – AviFS
    Commented Jun 21, 2021 at 7:28
  • 1
    \$\begingroup\$ Inverse of log natural to int... I'd say that's pretty creative. Clever, at the very least! \$\endgroup\$
    – Nate T
    Commented Jun 21, 2021 at 23:23
  • \$\begingroup\$ Or that isnt it, is it? Your solution uses e. Muh bad. Make way, noob stumbling through. XD \$\endgroup\$
    – Nate T
    Commented Jun 21, 2021 at 23:29
  • \$\begingroup\$ @NateT Thanks! I was actually outgolfed though by Adám, although it's less interesting: ÷≢⍬⍬. But it means this is neither as golfy, nor as creative, as it could be. This post can't really decide what it wants to be! \$\endgroup\$
    – AviFS
    Commented Jun 21, 2021 at 23:31
  • \$\begingroup\$ Haha, no worries! Any attention (read validation) is good attention : ) \$\endgroup\$
    – AviFS
    Commented Jun 21, 2021 at 23:31
0
\$\begingroup\$

JAVA

As a simple java project. I haven't looked at the others, so it is entirely possible (and likely) that this is the umpteenth answer to contain this solution. This code, placed inside src/Run.java of a fresh project, will return o.5.

The actual algorithm is only 2 (sorry, two XD) lines; The rest is just getting it to print. I know you asked for whimsical, but I was impressed with the simplicity of this, so I used it, for better or worse....

This algorithm takes advantage of the fact that any number, when divided by (itself plus itself) will give one half.

public class Run {

    public static void main(String[] args) {
        double x = Math.random();
        double y = x/(x+x);
        System.out.println(y); 
       
    }

}

Btw, Im not sure how to do the giant text in md, so I just used a title hash. Ill be back to fix once I figure it out.

EDIT: Although I guess using java for this two line solution is a bit whimsical! LOL

\$\endgroup\$
8
  • \$\begingroup\$ This is invalid, the code throws a syntax error; If that is fixed Math.random can output 0, throwing an error. \$\endgroup\$
    – emanresu A
    Commented Sep 10, 2021 at 22:15
  • \$\begingroup\$ @emanresu Didnt realize it needed to be fail proof. I was going on the impression that this was more of a "battle of the concepts" type of thing. I wrote this in about 45 seconds just as a way to have fun and blow off steam. didn't realize we were giving points for "most robust." I'll fix it though. Which IDE threw the syntax error. I believe I compiled in STS 4.7? I won't use Intellij products. anything else and I'll pull it up. What is the error? \$\endgroup\$
    – Nate T
    Commented Sep 11, 2021 at 1:28
  • \$\begingroup\$ The { after public class Run is unmatched, and Math.random can return 0, meaning that you get (0+0)/0. \$\endgroup\$
    – emanresu A
    Commented Sep 11, 2021 at 1:34
  • \$\begingroup\$ @emanresu The { was a copy paste error, and as far as the divide by zero error, I believe that is why I put it as a double.. When run, x goes 12 digits past the decimal point (0.000000000001 precision). Yes, if all 12 of those digits end up zeroes, then it will error out, so I added the if block just in case. Just for you. \$\endgroup\$
    – Nate T
    Commented Sep 11, 2021 at 1:48
  • 1
    \$\begingroup\$ You still have to have it completely deterministic. You could use something like 'a'.length() or similar. Please test this, as I don't know Java very well. \$\endgroup\$
    – emanresu A
    Commented Sep 11, 2021 at 1:59
0
\$\begingroup\$

JavaScript

t=true;
console.log((t) / ((t) ^ (t + t) ^ (t)) ** (t));

JavaScript works well, here is a simple numerically deficient example getting you the half or it.

\$\endgroup\$
1
2

Not the answer you're looking for? Browse other questions tagged or ask your own question.