61
\$\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

40 Answers 40

1
2
2
\$\begingroup\$

Java and Groovy, 50 bytes

double f(int a){return Math.pow(a, 1/2==0?2:0.5);}

Try Java online!

Try Groovy online!

Java squares, Groovy roots. Inspiration taken from this answer, although it is unfortunate that Java does not have type coercion like Python and that Groovy has different lambdas than Java does. This works because in Java, 1/2 is equal to 0, while in Groovy it is 0.5.

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

C and C#, 56 bytes

double f(double x){return//\
System.Math.Sqrt(x);
x*=x;}

C# doesn't use \ as a line continuation character, at least not for line comments.

Try it in C!
Try it in C#!

I was originally going to do Swift + something else, since /*/**/this// */ is only a comment in Swift, but it ended up being shorter to abuse C's line continuation and use another language with the same syntax.

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

Jelly / Pip, 6 bytes

EDIT: It's a byte shorter to reverse operations.

RTa
²

Try Jelly online!

Jelly starts execution at the bottom of the code (its 'main link') and sees if it needs anything higher: it sees the command to square and takes care of input and output implicitly.

Try Pip online!

Pip executes the top line, squaring the (implicitly read from the cmd line) var a and implicitly prints that. The bottom line is ignored.

\$\endgroup\$
1
  • \$\begingroup\$ Alternative 6-byter: PRTaVS. \$\endgroup\$
    – steenbergh
    Apr 12, 2017 at 8:32
1
\$\begingroup\$

Wolfram Language / PHP, 25 bytes

Get the square of a number in Wolfram Language and get the square root in PHP;

n^2
echo sqrt(_GET["n"]);

First line is Wolfram Language. First, you are the ones to change n in the searchbar in Wolfram Alpha so the code is also the input. Then it's will generate the answer upon pressing enter

n^2

Second line is PHP, It gets the square root of the n which is to be inputted in the address bar (eg. ppcg.php.net?n=213, where 213 is n's value)

echo sqrt($_GET["n"]);
\$\endgroup\$
5
  • 1
    \$\begingroup\$ Welcome to PPCG! However, you must take input somehow. You can't assume the number is stored in a variable. Sorry about that. You can view the list of acceptable i/o methods here. (positive scores on the answers mean it's allowed, negative means not allowed) \$\endgroup\$
    – Riker
    Apr 12, 2017 at 16:15
  • \$\begingroup\$ Got it. I'll just edit my answer. :) Another thing, I'll just explain why adding another code for input in wolfram is not applicable. \$\endgroup\$ Apr 12, 2017 at 16:18
  • \$\begingroup\$ Happy to help! Hope you stick around in ppcg! \$\endgroup\$
    – Riker
    Apr 12, 2017 at 16:21
  • \$\begingroup\$ Wolfram|Alpha is not a valid language. \$\endgroup\$
    – user61980
    Apr 12, 2017 at 22:59
  • 1
    \$\begingroup\$ Wolfram Language is based on mathematica which needs mathematical formula to be type in a non-natural way. Another thing, the answerer clears it out, its the website that's not the language but the wolfram language is the language that supports it. Wolfrsm Language and WolframAlpha is related to each ofher but not the same. The'yre totally different. \$\endgroup\$ Apr 13, 2017 at 2:36
1
\$\begingroup\$

PHP 5.6 and PHP 7, 55 bytes

function f($n){list($a[],$a[])=[.5,2];echo $n**$a[0];}

PHP 5.6 produces n^2, PHP 7 produces sqrt(n).

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

Swift 5.0 and Swift 5.1, 112 bytes

func f(_ x:Any)->Double{let y=x as!Double
return y*y}
func f<T>(_ x:T)->Double{return(x as!Double).squareRoot()}

SwiftFiddle link

Swift 5.1 changed overload resolution for functions with unlabelled arguments, so f(_:) resolves as the square function in Swift 5.0 and as the square root in Swift 5.1. The permissive argument types are required for this to work; if you don't pass it a double, this will compile but crash at runtime.

The lack of argument labels is required: if you remove the underscores to make this f(x:), it will print the square root in both versions.

Swift 4 and Swift 5, 125 113 bytes

import Foundation
func s(x:Double)->Double{return pow(x,Double("\(try?{()throws->Int?in 0}())".count)*0.15-1.15)}

SwiftFiddle link

The function s(x:) computes the square in Swift 4 and the square root in Swift 5.

The original answer had the throwing function on its own line, rather than being an inline IIFE.

This was harder than I expected.

The try? operator is one of three legal prefixes for a call to a throwing function -- the others being try and try!. try requires a catch, try! promotes errors to fatal errors, and try? ignores errors and returns nil instead. For example, consider the following code snippet:

func foo() throws -> Int {
    // ...
}

let bar = try? foo()

The type of bar is now Optional<Int>, since it might be nil.

What if foo() already returns Optional<Int>? In Swift 5, the optionals are flattened, and bar is still Optional<Int>. In Swift 4, this is not the case, and bar is now Optional<Optional<Int>>.

When you convert an optional value to a string, it wraps it in "Optional("...")". So, the following code:

func foo() throws -> Int? {
    return 0
}

print("\(try? foo())")

Prints Optional(0) in Swift 5 and Optional(Optional(0)) in Swift 4. (If you don't wrap it in the string interpolation, it fails to compile. I have no idea why.)

From there, I use .count to get the length of the string, and do some simple arithmetic to get 2.0 in Swift 4 and 0.5 in Swift 5.

Swift 4 and Swift 5, cheating, 85 bytes

func s(x:Double)->Double{#if swift(>=5)
return x.squareRoot()#else
return x*x#endif
}

I shouldn't have to explain this.

\$\endgroup\$
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, 2017 at 13:39
  • 1
    \$\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, 2017 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\$
    – Riker
    Apr 13, 2017 at 18:46
  • \$\begingroup\$ @Riker for now, it uses ascii/utf-8. Thanks. \$\endgroup\$
    – user58826
    Apr 13, 2017 at 19:28
0
\$\begingroup\$

Python 2 and ><> 23 bytes

#n*:i
print input()**.5

Python: Try it online!

><>: Try it online!

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.