New answers tagged

1

AWK, 19 bytes 1+($0=int($0/2^.5)) The 1+(...) is solely for the purpose of evaluating 0 or 1. Otherwise the expression evaluates to false and doesn't print anything. Try it online!


1

Ruby, 20 bytes ->n{(n/2**0.5).to_i} Try it online!


1

Haskell, 32 bytes h n=last[x|x<-[0..n],2*x^2<=n^2] This submission works for arbitrary precision integers, but is very slow because it checks every number \$x\$ below \$n\$ for whether it satisfies the equation \$ 2x^2\leq n^2\$, then takes the last one. Thus the runtime is exponential in the bit length of the input number. Try it online! Haskell, ...


1

Pari/GP, 17 bytes Another arbitrary-precision answer. n->sqrtint(n^2\2) Try it online!


0

GolfScript, 17 bytes ~3/{~@@>{}{;}if}% Try it online! Explanation ~ # Evaluate the input 3/ # Split the input into chunks of 3 { }% # Map every item in the input ~@@> # Is the 1st item < second item? {}{;}if # If false, discard the current item


0

GolfScript, 3 bytes Input is to be taken as two separate lines. In GolfScript, a preceding 0 is allowed for numbers. n/0 Try it online! Explanation # Two inputs taken with newlines n/ # Split the input (separated by newlines) with newlines 0 # Add a 0 to prevent a preceding 0 # Implicit concatenation GolfScript, 3 bytes ~+) Try it online! ...


2

naz, 56 bytes 6a8m2x1v1x1f1r3x1v2e0m1o0x1x2f2r3x1v3e0m1o0x1x3f1a1o0x1f Takes two natural numbers as input, separated by a space. Explanation (with 0x commands removed) 6a8m2x1v # Set variable 1 equal to 48 ("0") 1x1f1r3x1v2e0m1o # Function 1 # Read the first byte of input, removing it from the input string # Jump ...


0

R, 19 bytes function(i)i%/%2^.5 Try it online!


1

Charcoal, 46 bytes ≔⁰θ≔⁰ηF↨÷XN²¦²¦⁴«≔⁺×θ⁴ιθ≦⊗η¿›θ⊗η«≧⁻⊕⊗ηθ≦⊕η»»Iη Try it online! Link is to verbose version of code. Performs an arbitrary-precision floored integer square root of n²/2 using the binary square root algorithm as demonstrated e.g. by Dr. Math. Explanation: ≔⁰θ≔⁰η Set the accumulator and result to zero. F↨÷XN²¦²¦⁴« Loop over the base 4 ...


2

Burlesque, 8 bytes @2r@|/R_ Try it online! @2 # Push 2.0 r@ # Sqrt it |/ # Cast input to number, divide input by 2 R_ # Round to nearest


3

C (gcc), Precision limited by built-in types, 42 36 bytes __int128 f(__int128 n){n/=sqrtl(2);} Try it online! Floor for the most part but the last output is ceiling. Uses GCC's __int128 type: shorter in text length than unsigned long, can represent every value in unsigned long, and determined to not be a builtin type. Stay tuned for 6-8 weeks to get ...


1

cQuents, 11 bytes #|1:A_/2^.5 Try it online! Explanation #|1 output the first term : mode: sequence each term equals: A input _/ // 2 2 ^ ** .5 .5


10

8087 FPU machine code, 11 bytes Unassembled listing: D9 E8 FLD1 ; load a 1 constant (need to make a 2) D8 C0 FADD ST, ST(0) ; ST = 1+1 = 2 D9 FA FSQRT ; ST = SQRT(2) DE F9 FDIVP ST(1), ST ; ST = N / ST DF 1F FISTP QWORD PTR [BX] ; *BX = ROUND(ST) C3 RET ; return ...


3

C (gcc), 23 \$\cdots\$ 53 50 bytes typedef unsigned long long L;L f(L x){x/=sqrt(2);} Try it online! Saved 6 bytes thanks to a'_'!!! Added 38 bytes to fix type error kindly pointed out by S.S. Anne. Saved 3 bytes thanks to rtpax!!!


2

JavaScript (Node.js) arbitrary-precision, 62 58 bytes Thanks to Arnauld saving 4 bytes (n,v=n*n/2n,m=x=>x-(y=v/x+x>>1n)>>1n?m(y):y)=>v<2n?v:m(1n) Try it online! This is sqrt(n*n/2) after golfing the iterative Newton method sqrt() from https://stackoverflow.com/a/53684036.


2

PowerShell, 67 bytes param([uint64]$n)($n/[math]::Sqrt(2)).ToString("G17")-replace'\..*' Try it online! .NET (and thus, by extension, PowerShell) doesn't have a BigDecimal, so we're limited to Double or Decimal. However, the [math]::Sqrt() function only works on Double, so there we're stuck. So far, so standard. We then specify precision with G17, which ...


4

dc, 5 bytes d*2/v Try it online! Takes input and leaves output on the stack. dc automatically uses arbitrary-precision integers, and supports a precision of 0 decimal places by default, thus automatically "rounding". So taking the square-root of 2 will yield 1. Instead, this solution squares the input, by duplicating it and * multiplying both the items ...


2

Whitespace, 122 103 bytes [S S T T N _Push_-1][S S S N _Push_0][S N S _Dupe_0][T N T T _Read_STDIN_as_integer][T T T _Retrieve_input][S N S _Dupe_input][N T S T N _If_0_Jump_to_Label_ZERO][N S S N _Create_Label_LOOP][S N T _Swap_top_two][S S S T N _Push_1][T S S S _Add][S N T _Swap_top_two][S N S _Dupe_input][S N S _Dupe_input][T ...


3

PHP, 17 bytes <?=$argn/2**.5|0; Try it online! Uses @Niphram's truncate method (which in PHP also has the ability to convert the float to an int) I know it's trendy to say PHP is to be hated, but I kinda came to like its oddities, and it gives me a chance to add an original answer EDIT: saved 4 bytes using <?= php tag (no need to echo) EDIT2: ...


3

wx, 3 bytes It's W, with just one instruction added: square root. Turns out that this is very useful! (P.S. the built-in was added before the challenge.) 2Q/ Explanation 2Q % Find the square root of 2 a / % Divide the input by it % If one operand is an integer, % the program will automatically % try to trunctuate to an integer


2

CJam, 9 bytes CJam has mQ, but unfortunately it trunctuates to an integer ... Another port of Lyxal's answer. q~2 .5#/i Try it online! Explanation q~ e# Take input & evaluate 2 e# Take 2 to the power of ... .5# e# ... 0.5 (equal to square root) / e# Divide the input by it i e# Convert to integer


3

Pyth, 6 bytes The division auto-casts the number to a decimal!? (In seriousness, is there a square root function in Pyth?) /Q@2 2 Try it online! Explanation @2 2 to the power of 2 1/2 (effectively calculates math.sqrt(2)) /Q Divide the (evaluated) input by that number


2

TI-BASIC, 5 bytes int(Ans√(2⁻¹ Built-ins are great. Input is a number in Ans. Output is what is specified in the challenge. Explanation: √(2⁻¹ ;get the square root of 1/2 Ans ;get the input (Ans) ;implicit multiplication int( ;truncate ;implicit print of Ans Note: TI-BASIC is a tokenized ...


6

Japt, 3 bytes z2q Try it z is the floor division method and q is the nth-root method, defaulting to square root when it's not passed an argument.


12

JavaScript (ES6), 12 bytes i=>i/2**.5|0 Uses a binary or to truncate the result Try it online!


6

Jelly, 15 bytes ³²:2_²:Ẹ¡:2+µƬṪ Try it online! An arbitrary precision Jelly answer that uses the Newton-Raphson method to find the correct answer. Uses only integer arithmetic operations so the intermediate values are all Python big ints rather than getting cast as floats which would lose precision. The integer result equates to the floor of what would be ...


8

Java 8, 18 bytes n->n/=Math.sqrt(2) Limited to a maximum of \$9{,}223{,}372{,}036{,}854{,}775{,}807\$ (signed 64-bit integer). Try it online. Explanation: n-> // Method with long as both parameter and return-type n/= // Divide the input by: Math.sqrt(2) // The square-root of 2 // The `/=` sets the divided ...


14

Scratch 3.0, 7 blocks/62 bytes Try it online Scratch! As SB Syntax: when gf clicked ask()and wait say(round((answer)/([sqrt v]of(2 It's always fun to usual visual languages! At least I have built-ins this time.


2

MathGolf, 4 bytes 2√/i Try it online. Explanation: 2√ # Take the square-root of 2 / # Divide the (implicit) input-integer by this i # Cast it to an integer, truncating any decimal values # (after which the entire stack joined together is output implicitly as result)


3

APL (Dyalog Extended), 5 bytesSBCS Full program. Prompts stdin for zero or more numbers. ⌈⎕÷√2 Try it online! ⌈ ceiling of ⎕ console input ÷ divided by √ the square root of 2 two


14

Python 3, 19 17 bytes A different python answer lambda x:x//2**.5 -2 bytes thanks to @Mukundan try it online


2

Haskell, 20 bytes f n=round$n/(sqrt 2) Try it online


6

Mathematica, 17 14 13 bytes / 12 7 characters ⌊#/√2⌋& Try it online -3 bytes because Mathematica accepts the char √, which I copied from this MathGolf answer. -1 byte, -5 characters, as per @Mark S. suggestion, by using ⌊⌋. For just one more byte (but 5 more characters) I can always round to the nearest integer with Round[#/√2]&


5

05AB1E, 3 bytes 2t÷ Try it online! -1 byte thanks to @Grimmy Yet another port of my Keg answer for the sake of completion. Explained 2t÷ 2t # Push the square root of two ÷ # Integer division 🍟🍅 Still no ketchup.


2

Python 3, 22 21 bytes lambda x:int(x/2**.5) Try it online! -1 byte thanks to @RGS. Thanks for reminding me that implicit decimals exist Just a port of my Keg answer. Nothing special here.


2

Keg, 6 bytes 21½Ë/ℤ Try it online! This defines the function f as: Taking a single parameter, then Calculating the square root of 2 by raising it to the power of 0.5, then Dividing the parameter by root 2, then Casting the result to an integer (truncating / flooring the result) and returning it. The footer is to define the test cases in a nice way. ...


45

Regex (ECMAScript+(?*)), 1169 929 887 853 849 bytes Regex was never designed to do mathematics. It has no concept of arithmetic. However, when input is taken in the form of bijective unary, as a sequence of identical characters in which the length represents a natural number, it is possible to do a wide range of operations, building up from the simple ...


0

Haskell, 48 bytes f=fromEnum;g a b=toEnum(div((f a)+(f b))2)::Char Try it online


0

Brainf*ck, 39 bytes Try it online! ,>,>,<[->+<]<[->>-<<]>,[->-<]>[-->+<]>. The input/output are the ASCII values of characters. Using "aaz0" gives "%" as output, as it corresponds to the case 97 122 97 48 which is solved by moving 37 units of liquid, the % symbol. Could shave a couple of bytes if the ...


0

Burlesque, 33 bytes riXX<-J2ENj2en2?***{XX++}ms10.%z? Try it online! ri # Read input as int XX # Explode into digits <- # Reverse J2EN # Duplicate and take every 2nd starting from 1 j2en # Take the other half 2?* # Multiply each digit by 2 ** # Remerge arrays {XX++}ms # Sum the digits of ...


1

MathGolf, 4 bytes ‼/%] Inputs in the order b a, output as a list containing two integers. Try it online. Explanation: ‼ # Apply the following two commands to the stack separately: / # Division (which will be integer division if the given arguments are integers) % # Modulo # (both builtins will use the implicit input-integers for their two ...


1

Whitespace, 68 bytes [S S S N _Push_0][S N S _Dupe_0][T N T T _Read_STDIN_as_integer][T T T _Retrieve_input1][S N S _Dupe_input1][S N S _Dupe_input1][T N T T _Read_STDIN_as_integer][T T T _Retrieve_input2][T S T S _Integer_divide][T N S T _Print_as_integer][S S S T S T S N _Push_10_newline][T N S S _Print_as_character][S S S N ...


1

Perl 6 Raku, 12 11 bytes (*-*-*+*)/2 Try it online! Edit: I realized that I could take the parameters in any order. Four parameters are: liquid in container 1, liquid in container 2, volume of container 1, volume of container 2.


0

Rogex, 15 bytes 701300001600200 Try it online! The actual language isn't on TIO yet, so a python 3.8 interpreter with the program in the code section and the python interpreter split in the header and footer is provided Rogex is a newly developed language that I've recently discovered, so I though I'd give it a crack and write an answer. Explained ...


0

W, 3 bytes You know, we can't beat the built-ins. 1S/ Explanation a1S % Stack : 1 a / % Divide.(1/a) % Output.


0

PHP, 165 156 110 bytes $o=[$j=$i=$s=$m=0];for(;$i<strlen($n=$argn);)$o[]=$n[$i++]=='<'?++$m:--$s;for(;$j<=$i;)echo$o[$j]-$s,$n[$j++]; Try it online! Ungolfed <?php $min=0; $max=0; $current=0; $order = [0]; for ($i=0;$i<strlen($argn);$i++){ $compare=$argn[$i]; if ($compare=='<') { $max++; $current=$max; } ...


0

W r, 3 bytes /@m Explanation abab % Implicit ops / % Divide a by b. Stack: a b (a/b) @ % Roll down to show 2 operands. Stack: (a/b) a b m % Modulo the operands. Stack: (a/b) (a%b) % Output the whole stack ```


0

Haskell, 95 bytes s=show ('<':y)!(a:b)=s a++'<':y!b (x:y)!z=s(last z)++x:y!init z _!(y:_)=s y f x=x![0..length x] Try it online!


1

Burlesque, 9 bytes )**avavL[ Try it online! If I'm allowed to report the ceiling, can save 2 chars )**foL[ fo is defined as avpd which calculates the average and then ceilings it. )** # Map ord(a) av # Calculate average av # Floor the resulting double L[ # Int to Char


1

Ahead, 7 bytes ii+2/o@ Try it online!


Top 50 recent answers are included