New answers tagged

0

C (gcc), Precision limited by built-in types, 42 bytes __uint128_t f(__uint128_t n){n/=sqrtl(2);} Try it online! Floor for the most part but the last output is ceiling. Uses GCC's __uint128_t type; largest integer type available, unsigned, and shorter in text length than unsigned long. Stay tuned for 6-8 weeks to get arbitrary precision.


0

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


2

8087 FPU machine code, 13 bytes Unassembled listing: DF 07 FILD WORD PTR [BX] ; ST(1) = N = *BX D9 E8 FLD1 ; load 1 constant D8 C0 FADD ST, ST(0) ; ST = 1+1 = 2 D9 FA FSQRT ; ST = SQRT(2) DE F9 FDIV ; ST = N / ST DF 1F FISTP WORD PTR [BX] ; *BX = ROUND(ST) C3 RET ; ...


0

C (gcc), 23 17 bytes f(x){x/=sqrt(2);} Try it online! Saved 6 bytes thanks to a'_'!!!


0

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


1

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 ...


1

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 ...


1

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 ...


2

PHP, 16 bytes $argn/sqrt(2)|0; Try it online! (with highest value supported on TIO) 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 ...


2

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 ...


4

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.


7

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


4

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 ...


3

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 ...


5

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!


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)


2

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


10

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


2

Mathematica, 17 14 bytes / 12 characters Round[#/√2]& Try it online -3 bytes because Mathematica accepts the char √, which I copied from this MathGolf answer.


3

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 /ï # Divide and cast to integer 🍟🍅 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. ...


22

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

Perl 5 -Mbignum=bexp -p, 12 bytes $_=bexp 1,$_ Try it online!


0

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


0

Mathematica, 7 bytes N[E,#]& My answer is a further golfing of this answer. Try it online! For some reason, the output over at TIO looks funny but I tested it on my machine and the code works just fine.


1

Brainf*ck, 36 bytes Cell layout: cells 1 and 2 are input, cell 3 is where the final answer is built, cell 4 is auxiliary ,->,-<[->[->+>+<<]>>[-<<+>>]<<<]>>-. Or, with words: ,- read a and decrement > move to cell 2 ,- read b and decrement < ...


1

JavaScript (V8), 13 bytes a=>b=>a*b-a-b Try it online! Same solution as other answers, I almost didn't post it but for once I found a question without a JS answer, so may as well. In fact this is the exact same as Kevin Cruijssen's answer, replacing Java's lambda -> with Javascript's =>. I've included a very basic testing framework in my TIO ...


0

Both programs pretty much just evaluate the input & sort it. Min takes the first item of the sorted list, and Max takes the last item of the sorted list. GolfScript, 4 bytes Min ~$0= Try it online! GolfScript, 5 bytes Max ~$-1= Try it online! Total: 9 bytes


1

APL (Dyalog Unicode), 14 bytesSBCS ⊣(⊢,-)(⊢×-)⍣¯1 Try it online! A tacit function whose left arg is the sum and the right is the product. Finds one solution to the quadratic equation using the built-in numerical solver, and then gives both solutions based on the result. The "power operator" f⍣n runs the function f n times, but if n is negative, runs the ...


0

naz, 64 bytes 9a5m2x1v3a2x2v1x1f1o0m1a1o0x1x2f1o0x1x3f1r3x1v1e3x2v2e0m1a1o0x3f Explanation (with 0x commands removed) 9a5m2x1v # Set variable 1 equal to 45 ("-") 3a2x2v # Set variable 2 equal to 48 ("0") 1x1f1o0m1a1o # Function 1 # Output once, set the register equal to 1, and output ...


1

J, 12 bytes 1-@>@{p.@,&1 Try it online!


0

wx d, 14 12 bytes Just W with a few instructions added. Ë█⑳⓺ak⟰⑯⯑⑹⓷v Decompressed: a*S4*-Q+2/:aS-


0

TI-BASIC, 13 bytes sum(Ans=seq(X!,X,1,69 Input is an integer in Ans. Output is 1 if the input is a factorial, 0 if not. Due to precision in = checks being limited to 10 decimal places, this program will produce erroneous answers for numbers whose length is \$>10\$ digits. Explanation: seq(X!,X,1,69 ;generate a list of all factorial numbers ...


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

Fortran (GFortran), 57 bytes read*,i k=1 do j=1,i k=k*j if(i==k)l=1 enddo print*,l end Try it online! On some compilers may result in gibberish on false-y due to uninitialised l. Can be solved with 3 bytes (l=0) or compiler flags (e.g. -finit-integer=0). Strictly 1 is not a truthy in Fortran, but spec specified 1/0 for true/false.


0

Burlesque, 13 bytes riJro?!jFi0>= Try it online! If you accept -1 as falsey (technically isn't in burlesque) and >0 as truthy can save 3 bytes. riJ # Read as int and duplicate on stack ro # Range from [1,N] ?! # Factorial each jFi # Find the index of the element == N 0>= # Found index


0

GolfScript, 17 bytes ~.),{,1\{)*}/}%?) Try it online! Explanation ~ # Evaluate the input . # Duplicate the value ) # Increment the value , # Generate range from 1 to input { }% # Map every item , # Generate a to-0 range 1\ # Make the initial ...


1

05AB1E, 7 bytes (ŸãnOQO Try it online or verify the test cases in the range \$[0,100]\$. Explanation: ( # Get the negative of the (implicit) input-integer Ÿ # Push a list in the range [(implicit) input-integer, -input] ã # Get the cartesian product of this list, creating all possible pairs n # Square each value in each pair ...


1

MathGolf, 9 bytes ╤■mæ²Σk=Σ Try it online. Explanation: ╤ # Take the (implicit) input-integer, and push a list in the range [-input, input] ■ # Take the cartesian product of this, creating a list of all possible pairs mæ # Map these pairs to, using the following four commands: ² # Take the square of both values in the pair ...


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

TI-BASIC, 3 bytes Ans-0 Very simple and works the same way as the top J answer. Input is an integer in Ans. Examples: 4:Ans-0 4 4:0-Ans -4


5

GAP So my previous approach works indeed when we replace the definition of nfa with this: nfa := Automaton("epsilon", 25, 5, [[[1,6,7],[2,8,9],[3,10,11],4,5,[6,18], [7,19],8,9,10,11,[8,22],[9,23],10,11, 0,0,18,19,22,23,0,0,0,0], [[2,6,13],[3,8,15],[4,10,17],5,0,[8,18], [13,21],10,15,0,17,[6,22],[15,25],8,17, 10,0,0,...


1

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


1

GolfScript, 8 bytes Idea copied from the gs2 solution. ~,\>{*}* Try it online! Explanation ~ # Dump the input, e.g. 2 4 , # Generate range from 0 to input: 2 [0 1 2 3 4] \ # Swap the stack : [0 1 2 3 4] 2 > # Keep all those that are larger: [2 3 4] {*}* # Reduce by multiplication : 24


Top 50 recent answers are included