53
\$\begingroup\$

You must make a that outputs the square of the input in one language and the square root of the input in another. The shortest answer in bytes wins!

You must have a precision of at least 3 decimal places, and the input will always be a positive float.

\$\endgroup\$
0

33 Answers 33

1
2
0
\$\begingroup\$

Octave / Cardinal, 28 bytes

This program squares the input in Cardinal and gets the square root in Octave

 %:=t.
disp(sqrt(input("")))

Try it online! (Octave)

Try it online! (Cardinal)

So % is single line comment in Octave so it just gets input and prints the square root

disp(sqrt(input("")))

So that the Cardinal program doesn't encounter a divide by 0 error and die, the program

%:=t.

has been shifted with a space, which is ignored by both programs

Explanation of the Cardinal program:

The program starts at the %
It receives input and stores the value as active :
It sets the inactive to be equal to the active =
It multiplies the active by the inactive t
Finally it outputs the active value .

\$\endgroup\$
0
\$\begingroup\$

PHP / JavaScript, 43 bytes

<body onload=alert(<?=$x*$x.')>'.sqrt($x)?>

Input goes like:

<?php $x = 10; ?>

Kinda self-explaining, but does it fit the rules? My first code golf tho :-)

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Welcome to code golf! Input must be a float or a string that is only a float. If I understand correctly, this requires separate input. \$\endgroup\$ – user58826 Apr 12 '17 at 13:39
  • \$\begingroup\$ Are you sure this runs as JavaScript? It looks like both versions need a PHP processing stage (JavaScript wouldn't know what to do with alert(<?=$x*$x.')>'.sqrt($x)?) \$\endgroup\$ – Dave Apr 12 '17 at 19:26
0
\$\begingroup\$

CGL (CGL Golfing Language) / JS (ES6), 13 bytes(non-competing)

Non-competing because:

  1. CGL was released after this question.
  2. CGL is not a valid language. Because of and , CGL is technically a valid language.

Code:

 x=>x**2
//-₂

JS:

Simple: an anonymous arrow function that returns its first argument squared. The unnamed language code is commented out.

CGL:

The non-breaking space before the first line acts like a comment. The /s are no-ops. The - in the second line means to decrement the current stack number, which by default is 0. That then sets it to -1, where input is stored. The replaces the first item in the current stack with its square root, which is now where input is placed. By default, the current stack is outputted, outputting the square root of the input.

\$\endgroup\$
2
  • \$\begingroup\$ This is 13 bytes. I dunno about CGL, but I'm fairly certain JS uses ascii/utf-8. CGL would probably also use UTF-8, unless it has it's own codepage. \$\endgroup\$ – Rɪᴋᴇʀ Apr 13 '17 at 18:46
  • \$\begingroup\$ @Riker for now, it uses ascii/utf-8. Thanks. \$\endgroup\$ – user58826 Apr 13 '17 at 19:28
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy