Square Root Distance from IntegersWithout using numbers, get the highest salary you can. But don't...
Why didn't Tom Riddle take the presence of Fawkes and the Sorting Hat as more of a threat?
Why is it that Bernie Sanders is always called a "socialist"?
Is this ordinary workplace experiences for a job in Software Engineering?
How to visualize the Riemann-Roch theorem from complex analysis or geometric topology considerations?
"We can't save the customer" error after Migration - Magento 2.3
Alien invasion to probe us, why?
GRASS not working with QGIS 3.6
Why is Agricola named as such?
Why do cars have plastic shrouds over the engine?
What is the purpose of easy combat scenarios that don't need resource expenditure?
Scripture(s) saying not to look at the sun during his rising and setting time
What is the difference between rolling more dice versus fewer dice?
If I delete my routers history can my ISP still provide it to my parents?
It took me a lot of time to make this, pls like. (YouTube Comments #1)
How would an AI self awareness kill switch work?
Has Britain negotiated with any other countries outside the EU in preparation for the exit?
New package vs new version?
Has any human ever had the choice to leave Earth permanently?
How much mayhem could I cause as a sentient fish?
Airplane generations - how does it work?
What happens when a creature with flying blocks my non-flying attacker?
False written accusations not made public - is there law to cover this?
Picture with grey box as background
A Missing Symbol for This Logo
Square Root Distance from Integers
Without using numbers, get the highest salary you can. But don't exaggerate!Calculate the square root only using ++Sorted Lexical Partition of a NumberReverse and squareThe fastest square root calculatorRobbers - square times square rootCops - square times square rootFermat's factorization helperMiller-Rabin Strong PseudoprimesExact change in fewest bills and coinsApproximate My Squares
$begingroup$
Given a decimal number k
, find the smallest integer n
such that the square root of n
is within k
of an integer. However, the distance should be nonzero - n
cannot be a perfect square.
Given k
, a decimal number or a fraction (whichever is easier for you), such that 0 < k < 1
, output the smallest positive integer n
such that the difference between the square root of n
and the closest integer to the square root of n
is less than or equal to k
but nonzero.
If i
is the closest integer to the square root of n
, you are looking for the first n
where 0 < |i - sqrt(n)| <= k
.
Rules
- You cannot use a language's insufficient implementation of non-integer numbers to trivialize the problem.
- Otherwise, you can assume that
k
will not cause problems with, for example, floating point rounding.
Test Cases
.9 > 2
.5 > 2
.4 > 3
.3 > 3
.25 > 5
.2 > 8
.1 > 26
.05 > 101
.03 > 288
.01 > 2501
.005 > 10001
.003 > 27888
.001 > 250001
.0005 > 1000001
.0003 > 2778888
.0001 > 25000001
.0314159 > 255
.00314159 > 25599
.000314159 > 2534463
Comma separated test case inputs:
0.9, 0.5, 0.4, 0.3, 0.25, 0.2, 0.1, 0.05, 0.03, 0.01, 0.005, 0.003, 0.001, 0.0005, 0.0003, 0.0001, 0.0314159, 0.00314159, 0.000314159
This is code-golf, so shortest answer in bytes wins.
code-golf number integer
$endgroup$
add a comment |
$begingroup$
Given a decimal number k
, find the smallest integer n
such that the square root of n
is within k
of an integer. However, the distance should be nonzero - n
cannot be a perfect square.
Given k
, a decimal number or a fraction (whichever is easier for you), such that 0 < k < 1
, output the smallest positive integer n
such that the difference between the square root of n
and the closest integer to the square root of n
is less than or equal to k
but nonzero.
If i
is the closest integer to the square root of n
, you are looking for the first n
where 0 < |i - sqrt(n)| <= k
.
Rules
- You cannot use a language's insufficient implementation of non-integer numbers to trivialize the problem.
- Otherwise, you can assume that
k
will not cause problems with, for example, floating point rounding.
Test Cases
.9 > 2
.5 > 2
.4 > 3
.3 > 3
.25 > 5
.2 > 8
.1 > 26
.05 > 101
.03 > 288
.01 > 2501
.005 > 10001
.003 > 27888
.001 > 250001
.0005 > 1000001
.0003 > 2778888
.0001 > 25000001
.0314159 > 255
.00314159 > 25599
.000314159 > 2534463
Comma separated test case inputs:
0.9, 0.5, 0.4, 0.3, 0.25, 0.2, 0.1, 0.05, 0.03, 0.01, 0.005, 0.003, 0.001, 0.0005, 0.0003, 0.0001, 0.0314159, 0.00314159, 0.000314159
This is code-golf, so shortest answer in bytes wins.
code-golf number integer
$endgroup$
add a comment |
$begingroup$
Given a decimal number k
, find the smallest integer n
such that the square root of n
is within k
of an integer. However, the distance should be nonzero - n
cannot be a perfect square.
Given k
, a decimal number or a fraction (whichever is easier for you), such that 0 < k < 1
, output the smallest positive integer n
such that the difference between the square root of n
and the closest integer to the square root of n
is less than or equal to k
but nonzero.
If i
is the closest integer to the square root of n
, you are looking for the first n
where 0 < |i - sqrt(n)| <= k
.
Rules
- You cannot use a language's insufficient implementation of non-integer numbers to trivialize the problem.
- Otherwise, you can assume that
k
will not cause problems with, for example, floating point rounding.
Test Cases
.9 > 2
.5 > 2
.4 > 3
.3 > 3
.25 > 5
.2 > 8
.1 > 26
.05 > 101
.03 > 288
.01 > 2501
.005 > 10001
.003 > 27888
.001 > 250001
.0005 > 1000001
.0003 > 2778888
.0001 > 25000001
.0314159 > 255
.00314159 > 25599
.000314159 > 2534463
Comma separated test case inputs:
0.9, 0.5, 0.4, 0.3, 0.25, 0.2, 0.1, 0.05, 0.03, 0.01, 0.005, 0.003, 0.001, 0.0005, 0.0003, 0.0001, 0.0314159, 0.00314159, 0.000314159
This is code-golf, so shortest answer in bytes wins.
code-golf number integer
$endgroup$
Given a decimal number k
, find the smallest integer n
such that the square root of n
is within k
of an integer. However, the distance should be nonzero - n
cannot be a perfect square.
Given k
, a decimal number or a fraction (whichever is easier for you), such that 0 < k < 1
, output the smallest positive integer n
such that the difference between the square root of n
and the closest integer to the square root of n
is less than or equal to k
but nonzero.
If i
is the closest integer to the square root of n
, you are looking for the first n
where 0 < |i - sqrt(n)| <= k
.
Rules
- You cannot use a language's insufficient implementation of non-integer numbers to trivialize the problem.
- Otherwise, you can assume that
k
will not cause problems with, for example, floating point rounding.
Test Cases
.9 > 2
.5 > 2
.4 > 3
.3 > 3
.25 > 5
.2 > 8
.1 > 26
.05 > 101
.03 > 288
.01 > 2501
.005 > 10001
.003 > 27888
.001 > 250001
.0005 > 1000001
.0003 > 2778888
.0001 > 25000001
.0314159 > 255
.00314159 > 25599
.000314159 > 2534463
Comma separated test case inputs:
0.9, 0.5, 0.4, 0.3, 0.25, 0.2, 0.1, 0.05, 0.03, 0.01, 0.005, 0.003, 0.001, 0.0005, 0.0003, 0.0001, 0.0314159, 0.00314159, 0.000314159
This is code-golf, so shortest answer in bytes wins.
code-golf number integer
code-golf number integer
edited yesterday
Stephen
asked yesterday
StephenStephen
7,45323397
7,45323397
add a comment |
add a comment |
15 Answers
15
active
oldest
votes
$begingroup$
Wolfram Language (Mathematica), 34 bytes
Min[⌈.5/#+{-#,#}/2⌉^2+{1,-1}]&
Try it online!
Explanation
The result must be of the form $m^2 pm 1$ for some $m in mathbb{N}$. Solving the inequations $sqrt{m^2+1} - m le k$ and $m - sqrt{m^2-1} le k$, we get $m ge frac{1-k^2}{2k}$ and $m ge frac{1+k^2}{2k}$ respectively. So the result is $operatorname{min}left({leftlceil frac{1-k^2}{2k} rightrceil}^2+1, {leftlceil frac{1+k^2}{2k} rightrceil}^2-1right)$.
$endgroup$
add a comment |
$begingroup$
Python, 42 bytes
lambda k:((k-1/k)//2)**2+1-2*(k<1/k%2<2-k)
Try it online!
Based on alephalpha's formula, explicitly checking if we're in the $m^2-1$ or $m^2+1$ case via the condition k<1/k%2<2-k
.
Python 3.8 can save a byte with an inline assignment.
Python 3.8, 41 bytes
lambda k:((a:=k-1/k)//2)**2-1+2*(a/2%1<k)
Try it online!
These beat my recursive solution:
50 bytes
f=lambda k,x=1:k>.5-abs(x**.5%1-.5)>0 or-~f(k,x+1)
Try it online!
$endgroup$
add a comment |
$begingroup$
05AB1E, 16 bytes
nD(‚>I·/înTS·<-ß
Port of @alephalpha's Mathematica answer, with inspiration from @Sok's Pyth answer, so make sure to upvote both of them!
Try it online or verify all test cases.
Explanation:
n # Take the square of the (implicit) input
# i.e. 0.05 → 0.0025
D(‚ # Pair it with its negative
# i.e. 0.0025 → [0.0025,-0.0025]
> # Increment both by 1
# i.e. [0.0025,-0.0025] → [1.0025,0.9975]
I· # Push the input doubled
# i.e. 0.05 → 0.1
/ # Divide both numbers with this doubled input
# i.e. [1.0025,0.9975] / 0.1 → [10.025,9.975]
î # Round both up
# i.e. [10.025,9.975] → [11.0,10.0]
n # Take the square of those
# i.e. [11.0,10.0] → [121.0,100.0]
TS # Push [1,0]
· # Double both to [2,0]
< # Decrease both by 1 to [1,-1]
- # Decrease the earlier numbers by this
# i.e. [121.0,100.0] - [1,-1] → [120.0,101.0]
ß # Pop and push the minimum of the two
# i.e. [120.0,101.0] → 101.0
# (which is output implicitly)
$endgroup$
$begingroup$
Neat, thanks for linking the answer that has the formula used. I was doing mental gymnastics trying to figure out the formula from 05AB1E's ever-odd syntax.
$endgroup$
– Magic Octopus Urn
23 hours ago
add a comment |
$begingroup$
JavaScript (ES7), 51 50 bytes
f=(k,n)=>!(d=(s=n**.5)+~(s-.5))|d*d>k*k?f(k,-~n):n
Try it online!
(fails for the test cases that require too much recursion)
Non-recursive version, 57 56 bytes
k=>{for(n=1;!(d=(s=++n**.5)+~(s-.5))|d*d>k*k;);return n}
Try it online!
Or for 55 bytes:
k=>eval(`for(n=1;!(d=(s=++n**.5)+~(s-.5))|d*d>k*k;);n`)
Try it online!
(but this one is significantly slower)
$endgroup$
add a comment |
$begingroup$
J, 39 29 bytes
[:<./_1 1++:*:@>.@%~1+(,-)@*:
NB. This shorter version simply uses @alephalpha's formula.
Try it online!
39 bytes, original, brute force
2(>:@])^:((<+.0=])(<.-.)@(-<.)@%:)^:_~]
Try it online!
Handles all test cases
$endgroup$
add a comment |
$begingroup$
Japt, 18 16 bytes
-2 bytes from Shaggy
_=¬u1)©U>½-½aZ}a
Try it online!
$endgroup$
$begingroup$
Might be shorter using Arnauld's solution
$endgroup$
– ASCII-only
yesterday
$begingroup$
A little shuffling saves a byte.
$endgroup$
– Shaggy
yesterday
$begingroup$
Oh... of course i could have reversed that :|. Also that%1 &&
is nasty, not sure if using Arnauld's solution would be shorter (maybe not)
$endgroup$
– ASCII-only
yesterday
$begingroup$
16 bytes by reassigningZ¬u1
toZ
at the beginning of the function.
$endgroup$
– Shaggy
yesterday
$begingroup$
The other method appears to be 26:[1,-1]®*U²Ä /U/2 c ²-Z} rm
$endgroup$
– ASCII-only
15 hours ago
add a comment |
$begingroup$
Perl 6, 33 bytes
-1 byte thanks to Grimy
{first $_>*.sqrt*(1|-1)%1>0,1..*}
Try it online!
$endgroup$
$begingroup$
-1 byte by replacing>=
with>
. Square roots of integers are either integer or irrational, so the equality case provably cannot happen.
$endgroup$
– Grimy
yesterday
1
$begingroup$
@Grimy Thanks, this seems to be allowed according to the challenge rules. (Though floating-point numbers are always rational, of course.)
$endgroup$
– nwellnhof
yesterday
add a comment |
$begingroup$
Pyth, 22 21 bytes
hSm-^.Ech*d^Q2yQ2d_B1
Try it online here, or verify all the test cases at once here.
Another port of alephalpha's excellent answer, make sure to give them an upvote!
hSm-^.Ech*d^Q2yQ2d_B1 Implicit: Q=eval(input())
_B1 [1,-1]
m Map each element of the above, as d, using:
^Q2 Q^2
*d Multiply by d
h Increment
c yQ Divide by (2 * Q)
.E Round up
^ 2 Square
- d Subtract d
S Sort
h Take first element, implicit print
Edit: Saved a byte, thanks to Kevin Cruijssen
$endgroup$
1
$begingroup$
I don't know Pyth, but is it possible to create[-1,1]
in 3 bytes as well, or do you need an additional reverse so it becomes 4 bytes? If it's possible in 3 bytes, you could do that, and then change the*_d
to*d
and the+d
to-d
. Also, does Pyth not have a Minimum builtin, instead of sort & take first?
$endgroup$
– Kevin Cruijssen
yesterday
1
$begingroup$
@KevinCruijssen The order of the two elements isn't important as we're taking the minimum, though I can't think of a way of creating the pair in 3 bytes. A good catch on changing it to- ... d
though, that saves me a byte! Thanks
$endgroup$
– Sok
yesterday
$begingroup$
@KevinCruijssen Also there isn't a single byte minimum or maximum function unfortunately :o(
$endgroup$
– Sok
yesterday
1
$begingroup$
Ah, of course. You map over the values, so it doesn't matter if it's[1,-1]
or[-1,1]
. I was comparing the*d
and-d
with my 05AB1E answer, where I don't use a map, but can subtract/multiply a 2D array from/with another 2D array, so I don't need a map. Glad I could help to save a byte in that case. :) And thanks for the inspiration for my 05AB1E answer.
$endgroup$
– Kevin Cruijssen
yesterday
add a comment |
$begingroup$
APL (Dyalog Unicode), 27 bytesSBCS
⌊/0~⍨¯1 1+2*⍨∘⌈+⍨÷⍨1(+,-)×⍨
Try it online!
Monadic train taking one argument. This is a port of alephalpha's answer.
How:
⌊/0~⍨¯1 1+2*⍨∘⌈+⍨÷⍨1(+,-)×⍨ ⍝ Monadic train
×⍨ ⍝ Square of the argument
1(+,-) ⍝ 1 ± that (returns 1+k^2, 1-k^2)
÷⍨ ⍝ divided by
+⍨ ⍝ twice the argument
∘⌈ ⍝ Ceiling
2*⍨ ⍝ Squared
¯1 1+ ⍝ -1 to the first, +1 to the second
0~⍨ ⍝ Removing the zeroes
⌊/ ⍝ Return the smallest
$endgroup$
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 89 85 bytes
k=>{double n=1,p;for(;Math.Abs(Math.Round(p=Math.Sqrt(n))-p)>k|p%1==0;n++);return n;}
Try it online!
-4 bytes thanks to Kevin Cruijssen!
$endgroup$
$begingroup$
You can save a byte by putting then++
in the loop, so the-1
can be removed from the return:k=>{double n=1,p;for(;Math.Abs(Math.Round(p=Math.Sqrt(0d+n))-p)>k|p%1==0;n++);return n;}
$endgroup$
– Kevin Cruijssen
yesterday
$begingroup$
Also, the0d+
can be removed, can it not?
$endgroup$
– Kevin Cruijssen
yesterday
$begingroup$
@KevinCruijssen Yes it can, I just forgot then
was already a double
$endgroup$
– Embodiment of Ignorance
23 hours ago
add a comment |
$begingroup$
Java (JDK), 73 bytes
k->{for(double i=1,j;;){j=Math.sqrt(++i)%1;if(j>0&&j<k||1-j<k)return i;}}
Try it online!
$endgroup$
add a comment |
$begingroup$
Java 8, 85 bytes
n->{double i=1,p;for(;Math.abs(Math.round(p=Math.sqrt(i))-p)>n|p%1==0;i++);return i;}
Port of EmbodimentOfIgnorance's C# .NET answer.
Try it online.
The Math.round
can alternatively be this, but unfortunately it's the same byte-count:
n->{double i=1,p;for(;Math.abs((int)((p=Math.sqrt(i))+.5)-p)>n|p%1==0;i++);return i;}
Try it online.
$endgroup$
add a comment |
$begingroup$
MathGolf, 16 bytes
²_b*α)½╠ü²1bαm,╓
Try it online!
Not a huge fan of this solution. It is a port of the 05AB1E solution, which is based on the same formula most answers are using.
Explanation
² pop a : push(a*a)
_ duplicate TOS
b push -1
* pop a, b : push(a*b)
α wrap last two elements in array
) increment
½ halve
╠ pop a, b, push b/a
ü ceiling with implicit map
² pop a : push(a*a)
1 push 1
b push -1
α wrap last two elements in array
m explicit map
, pop a, b, push b-a
╓ min of list
$endgroup$
$begingroup$
Is every symbol considered abyte
in code golfing? Because some of your characters require more than a single byte. I don't mean to nit-pick, I'm genuinely wondering :)
$endgroup$
– schroffl
3 hours ago
$begingroup$
Good question! A "byte" in golfing relates to the minimum file size required to store a program. The text used to visualize those bytes can be any bytes. I have chosen Code Page 437 to visualize my scripts, but the important part is the actual bytes that define the source code.
$endgroup$
– maxb
2 hours ago
$begingroup$
A good example of the number of characters and number of bytes being different is this answer. Here, the'ԓ'
character is actually 2 bytes, but the rest are 1 byte characters.
$endgroup$
– maxb
2 hours ago
add a comment |
$begingroup$
Forth (gforth), 76 bytes
: f 1 begin 1+ dup s>f fsqrt fdup fround f- fabs fdup f0> fover f< * until ;
Try it online!
Explanation
Starts a counter at 1 and Increments it in a loop. Each iteration it checks if the absolute value of the counter's square root - the closest integer is less than k
Code Explanation
: f start a new word definition
1 place a counter on the stack, start it at 1
begin start and indefinite loop
1+ add 1 to the counter
dup s>f convert a copy of the counter to a float
fsqrt get the square root of the counter
fdup fround f- get the difference between the square root and the next closes integer
fabs fdup get the absolute value of the result and duplicate
f0> check if the result is greater than 0 (not perfect square)
fover f< bring k to the top of the float stack and check if the sqrt is less than k
* multiply the two results (shorter "and" in this case)
until end loop if result ("and" of both conditions) is true
; end word definition
$endgroup$
add a comment |
$begingroup$
Jelly, 13 bytes
I have not managed to get anything terser than the same approach as alephalpha
- go upvote his Mathematica answer!
²;N$‘÷ḤĊ²_Ø+Ṃ
Try it online!
How?
²;N$‘÷ḤĊ²_Ø+Ṃ - Link: number, n (in (0,1))
² - square n -> n²
$ - last two links as a monad:
N - negate -> -(n²)
; - concatenate -> [n², -(n²)]
‘ - increment -> [1+n², 1-(n²)]
Ḥ - double n -> 2n
÷ - divide -> [(1+n²)/n/2, (1-(n²))/n/2]
Ċ - ceiling -> [⌈(1+n²)/n/2⌉, ⌈(1-(n²))/n/2⌉]
² - square -> [⌈(1+n²)/n/2⌉², ⌈(1-(n²))/n/2⌉²]
Ø+ - literal -> [1,-1]
_ - subtract -> [⌈(1+n²)/n/2⌉²-1, ⌈(1-(n²))/n/2⌉²+1]
Ṃ - minimum -> min(⌈(1+n²)/n/2⌉²-1, ⌈(1-(n²))/n/2⌉²+1)
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "200"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f180412%2fsquare-root-distance-from-integers%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
15 Answers
15
active
oldest
votes
15 Answers
15
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Wolfram Language (Mathematica), 34 bytes
Min[⌈.5/#+{-#,#}/2⌉^2+{1,-1}]&
Try it online!
Explanation
The result must be of the form $m^2 pm 1$ for some $m in mathbb{N}$. Solving the inequations $sqrt{m^2+1} - m le k$ and $m - sqrt{m^2-1} le k$, we get $m ge frac{1-k^2}{2k}$ and $m ge frac{1+k^2}{2k}$ respectively. So the result is $operatorname{min}left({leftlceil frac{1-k^2}{2k} rightrceil}^2+1, {leftlceil frac{1+k^2}{2k} rightrceil}^2-1right)$.
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 34 bytes
Min[⌈.5/#+{-#,#}/2⌉^2+{1,-1}]&
Try it online!
Explanation
The result must be of the form $m^2 pm 1$ for some $m in mathbb{N}$. Solving the inequations $sqrt{m^2+1} - m le k$ and $m - sqrt{m^2-1} le k$, we get $m ge frac{1-k^2}{2k}$ and $m ge frac{1+k^2}{2k}$ respectively. So the result is $operatorname{min}left({leftlceil frac{1-k^2}{2k} rightrceil}^2+1, {leftlceil frac{1+k^2}{2k} rightrceil}^2-1right)$.
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 34 bytes
Min[⌈.5/#+{-#,#}/2⌉^2+{1,-1}]&
Try it online!
Explanation
The result must be of the form $m^2 pm 1$ for some $m in mathbb{N}$. Solving the inequations $sqrt{m^2+1} - m le k$ and $m - sqrt{m^2-1} le k$, we get $m ge frac{1-k^2}{2k}$ and $m ge frac{1+k^2}{2k}$ respectively. So the result is $operatorname{min}left({leftlceil frac{1-k^2}{2k} rightrceil}^2+1, {leftlceil frac{1+k^2}{2k} rightrceil}^2-1right)$.
$endgroup$
Wolfram Language (Mathematica), 34 bytes
Min[⌈.5/#+{-#,#}/2⌉^2+{1,-1}]&
Try it online!
Explanation
The result must be of the form $m^2 pm 1$ for some $m in mathbb{N}$. Solving the inequations $sqrt{m^2+1} - m le k$ and $m - sqrt{m^2-1} le k$, we get $m ge frac{1-k^2}{2k}$ and $m ge frac{1+k^2}{2k}$ respectively. So the result is $operatorname{min}left({leftlceil frac{1-k^2}{2k} rightrceil}^2+1, {leftlceil frac{1+k^2}{2k} rightrceil}^2-1right)$.
edited yesterday
answered yesterday
alephalphaalephalpha
21.5k32993
21.5k32993
add a comment |
add a comment |
$begingroup$
Python, 42 bytes
lambda k:((k-1/k)//2)**2+1-2*(k<1/k%2<2-k)
Try it online!
Based on alephalpha's formula, explicitly checking if we're in the $m^2-1$ or $m^2+1$ case via the condition k<1/k%2<2-k
.
Python 3.8 can save a byte with an inline assignment.
Python 3.8, 41 bytes
lambda k:((a:=k-1/k)//2)**2-1+2*(a/2%1<k)
Try it online!
These beat my recursive solution:
50 bytes
f=lambda k,x=1:k>.5-abs(x**.5%1-.5)>0 or-~f(k,x+1)
Try it online!
$endgroup$
add a comment |
$begingroup$
Python, 42 bytes
lambda k:((k-1/k)//2)**2+1-2*(k<1/k%2<2-k)
Try it online!
Based on alephalpha's formula, explicitly checking if we're in the $m^2-1$ or $m^2+1$ case via the condition k<1/k%2<2-k
.
Python 3.8 can save a byte with an inline assignment.
Python 3.8, 41 bytes
lambda k:((a:=k-1/k)//2)**2-1+2*(a/2%1<k)
Try it online!
These beat my recursive solution:
50 bytes
f=lambda k,x=1:k>.5-abs(x**.5%1-.5)>0 or-~f(k,x+1)
Try it online!
$endgroup$
add a comment |
$begingroup$
Python, 42 bytes
lambda k:((k-1/k)//2)**2+1-2*(k<1/k%2<2-k)
Try it online!
Based on alephalpha's formula, explicitly checking if we're in the $m^2-1$ or $m^2+1$ case via the condition k<1/k%2<2-k
.
Python 3.8 can save a byte with an inline assignment.
Python 3.8, 41 bytes
lambda k:((a:=k-1/k)//2)**2-1+2*(a/2%1<k)
Try it online!
These beat my recursive solution:
50 bytes
f=lambda k,x=1:k>.5-abs(x**.5%1-.5)>0 or-~f(k,x+1)
Try it online!
$endgroup$
Python, 42 bytes
lambda k:((k-1/k)//2)**2+1-2*(k<1/k%2<2-k)
Try it online!
Based on alephalpha's formula, explicitly checking if we're in the $m^2-1$ or $m^2+1$ case via the condition k<1/k%2<2-k
.
Python 3.8 can save a byte with an inline assignment.
Python 3.8, 41 bytes
lambda k:((a:=k-1/k)//2)**2-1+2*(a/2%1<k)
Try it online!
These beat my recursive solution:
50 bytes
f=lambda k,x=1:k>.5-abs(x**.5%1-.5)>0 or-~f(k,x+1)
Try it online!
edited yesterday
answered yesterday
xnorxnor
91.3k18186443
91.3k18186443
add a comment |
add a comment |
$begingroup$
05AB1E, 16 bytes
nD(‚>I·/înTS·<-ß
Port of @alephalpha's Mathematica answer, with inspiration from @Sok's Pyth answer, so make sure to upvote both of them!
Try it online or verify all test cases.
Explanation:
n # Take the square of the (implicit) input
# i.e. 0.05 → 0.0025
D(‚ # Pair it with its negative
# i.e. 0.0025 → [0.0025,-0.0025]
> # Increment both by 1
# i.e. [0.0025,-0.0025] → [1.0025,0.9975]
I· # Push the input doubled
# i.e. 0.05 → 0.1
/ # Divide both numbers with this doubled input
# i.e. [1.0025,0.9975] / 0.1 → [10.025,9.975]
î # Round both up
# i.e. [10.025,9.975] → [11.0,10.0]
n # Take the square of those
# i.e. [11.0,10.0] → [121.0,100.0]
TS # Push [1,0]
· # Double both to [2,0]
< # Decrease both by 1 to [1,-1]
- # Decrease the earlier numbers by this
# i.e. [121.0,100.0] - [1,-1] → [120.0,101.0]
ß # Pop and push the minimum of the two
# i.e. [120.0,101.0] → 101.0
# (which is output implicitly)
$endgroup$
$begingroup$
Neat, thanks for linking the answer that has the formula used. I was doing mental gymnastics trying to figure out the formula from 05AB1E's ever-odd syntax.
$endgroup$
– Magic Octopus Urn
23 hours ago
add a comment |
$begingroup$
05AB1E, 16 bytes
nD(‚>I·/înTS·<-ß
Port of @alephalpha's Mathematica answer, with inspiration from @Sok's Pyth answer, so make sure to upvote both of them!
Try it online or verify all test cases.
Explanation:
n # Take the square of the (implicit) input
# i.e. 0.05 → 0.0025
D(‚ # Pair it with its negative
# i.e. 0.0025 → [0.0025,-0.0025]
> # Increment both by 1
# i.e. [0.0025,-0.0025] → [1.0025,0.9975]
I· # Push the input doubled
# i.e. 0.05 → 0.1
/ # Divide both numbers with this doubled input
# i.e. [1.0025,0.9975] / 0.1 → [10.025,9.975]
î # Round both up
# i.e. [10.025,9.975] → [11.0,10.0]
n # Take the square of those
# i.e. [11.0,10.0] → [121.0,100.0]
TS # Push [1,0]
· # Double both to [2,0]
< # Decrease both by 1 to [1,-1]
- # Decrease the earlier numbers by this
# i.e. [121.0,100.0] - [1,-1] → [120.0,101.0]
ß # Pop and push the minimum of the two
# i.e. [120.0,101.0] → 101.0
# (which is output implicitly)
$endgroup$
$begingroup$
Neat, thanks for linking the answer that has the formula used. I was doing mental gymnastics trying to figure out the formula from 05AB1E's ever-odd syntax.
$endgroup$
– Magic Octopus Urn
23 hours ago
add a comment |
$begingroup$
05AB1E, 16 bytes
nD(‚>I·/înTS·<-ß
Port of @alephalpha's Mathematica answer, with inspiration from @Sok's Pyth answer, so make sure to upvote both of them!
Try it online or verify all test cases.
Explanation:
n # Take the square of the (implicit) input
# i.e. 0.05 → 0.0025
D(‚ # Pair it with its negative
# i.e. 0.0025 → [0.0025,-0.0025]
> # Increment both by 1
# i.e. [0.0025,-0.0025] → [1.0025,0.9975]
I· # Push the input doubled
# i.e. 0.05 → 0.1
/ # Divide both numbers with this doubled input
# i.e. [1.0025,0.9975] / 0.1 → [10.025,9.975]
î # Round both up
# i.e. [10.025,9.975] → [11.0,10.0]
n # Take the square of those
# i.e. [11.0,10.0] → [121.0,100.0]
TS # Push [1,0]
· # Double both to [2,0]
< # Decrease both by 1 to [1,-1]
- # Decrease the earlier numbers by this
# i.e. [121.0,100.0] - [1,-1] → [120.0,101.0]
ß # Pop and push the minimum of the two
# i.e. [120.0,101.0] → 101.0
# (which is output implicitly)
$endgroup$
05AB1E, 16 bytes
nD(‚>I·/înTS·<-ß
Port of @alephalpha's Mathematica answer, with inspiration from @Sok's Pyth answer, so make sure to upvote both of them!
Try it online or verify all test cases.
Explanation:
n # Take the square of the (implicit) input
# i.e. 0.05 → 0.0025
D(‚ # Pair it with its negative
# i.e. 0.0025 → [0.0025,-0.0025]
> # Increment both by 1
# i.e. [0.0025,-0.0025] → [1.0025,0.9975]
I· # Push the input doubled
# i.e. 0.05 → 0.1
/ # Divide both numbers with this doubled input
# i.e. [1.0025,0.9975] / 0.1 → [10.025,9.975]
î # Round both up
# i.e. [10.025,9.975] → [11.0,10.0]
n # Take the square of those
# i.e. [11.0,10.0] → [121.0,100.0]
TS # Push [1,0]
· # Double both to [2,0]
< # Decrease both by 1 to [1,-1]
- # Decrease the earlier numbers by this
# i.e. [121.0,100.0] - [1,-1] → [120.0,101.0]
ß # Pop and push the minimum of the two
# i.e. [120.0,101.0] → 101.0
# (which is output implicitly)
answered yesterday
Kevin CruijssenKevin Cruijssen
38.8k557200
38.8k557200
$begingroup$
Neat, thanks for linking the answer that has the formula used. I was doing mental gymnastics trying to figure out the formula from 05AB1E's ever-odd syntax.
$endgroup$
– Magic Octopus Urn
23 hours ago
add a comment |
$begingroup$
Neat, thanks for linking the answer that has the formula used. I was doing mental gymnastics trying to figure out the formula from 05AB1E's ever-odd syntax.
$endgroup$
– Magic Octopus Urn
23 hours ago
$begingroup$
Neat, thanks for linking the answer that has the formula used. I was doing mental gymnastics trying to figure out the formula from 05AB1E's ever-odd syntax.
$endgroup$
– Magic Octopus Urn
23 hours ago
$begingroup$
Neat, thanks for linking the answer that has the formula used. I was doing mental gymnastics trying to figure out the formula from 05AB1E's ever-odd syntax.
$endgroup$
– Magic Octopus Urn
23 hours ago
add a comment |
$begingroup$
JavaScript (ES7), 51 50 bytes
f=(k,n)=>!(d=(s=n**.5)+~(s-.5))|d*d>k*k?f(k,-~n):n
Try it online!
(fails for the test cases that require too much recursion)
Non-recursive version, 57 56 bytes
k=>{for(n=1;!(d=(s=++n**.5)+~(s-.5))|d*d>k*k;);return n}
Try it online!
Or for 55 bytes:
k=>eval(`for(n=1;!(d=(s=++n**.5)+~(s-.5))|d*d>k*k;);n`)
Try it online!
(but this one is significantly slower)
$endgroup$
add a comment |
$begingroup$
JavaScript (ES7), 51 50 bytes
f=(k,n)=>!(d=(s=n**.5)+~(s-.5))|d*d>k*k?f(k,-~n):n
Try it online!
(fails for the test cases that require too much recursion)
Non-recursive version, 57 56 bytes
k=>{for(n=1;!(d=(s=++n**.5)+~(s-.5))|d*d>k*k;);return n}
Try it online!
Or for 55 bytes:
k=>eval(`for(n=1;!(d=(s=++n**.5)+~(s-.5))|d*d>k*k;);n`)
Try it online!
(but this one is significantly slower)
$endgroup$
add a comment |
$begingroup$
JavaScript (ES7), 51 50 bytes
f=(k,n)=>!(d=(s=n**.5)+~(s-.5))|d*d>k*k?f(k,-~n):n
Try it online!
(fails for the test cases that require too much recursion)
Non-recursive version, 57 56 bytes
k=>{for(n=1;!(d=(s=++n**.5)+~(s-.5))|d*d>k*k;);return n}
Try it online!
Or for 55 bytes:
k=>eval(`for(n=1;!(d=(s=++n**.5)+~(s-.5))|d*d>k*k;);n`)
Try it online!
(but this one is significantly slower)
$endgroup$
JavaScript (ES7), 51 50 bytes
f=(k,n)=>!(d=(s=n**.5)+~(s-.5))|d*d>k*k?f(k,-~n):n
Try it online!
(fails for the test cases that require too much recursion)
Non-recursive version, 57 56 bytes
k=>{for(n=1;!(d=(s=++n**.5)+~(s-.5))|d*d>k*k;);return n}
Try it online!
Or for 55 bytes:
k=>eval(`for(n=1;!(d=(s=++n**.5)+~(s-.5))|d*d>k*k;);n`)
Try it online!
(but this one is significantly slower)
edited yesterday
answered yesterday
ArnauldArnauld
77k693323
77k693323
add a comment |
add a comment |
$begingroup$
J, 39 29 bytes
[:<./_1 1++:*:@>.@%~1+(,-)@*:
NB. This shorter version simply uses @alephalpha's formula.
Try it online!
39 bytes, original, brute force
2(>:@])^:((<+.0=])(<.-.)@(-<.)@%:)^:_~]
Try it online!
Handles all test cases
$endgroup$
add a comment |
$begingroup$
J, 39 29 bytes
[:<./_1 1++:*:@>.@%~1+(,-)@*:
NB. This shorter version simply uses @alephalpha's formula.
Try it online!
39 bytes, original, brute force
2(>:@])^:((<+.0=])(<.-.)@(-<.)@%:)^:_~]
Try it online!
Handles all test cases
$endgroup$
add a comment |
$begingroup$
J, 39 29 bytes
[:<./_1 1++:*:@>.@%~1+(,-)@*:
NB. This shorter version simply uses @alephalpha's formula.
Try it online!
39 bytes, original, brute force
2(>:@])^:((<+.0=])(<.-.)@(-<.)@%:)^:_~]
Try it online!
Handles all test cases
$endgroup$
J, 39 29 bytes
[:<./_1 1++:*:@>.@%~1+(,-)@*:
NB. This shorter version simply uses @alephalpha's formula.
Try it online!
39 bytes, original, brute force
2(>:@])^:((<+.0=])(<.-.)@(-<.)@%:)^:_~]
Try it online!
Handles all test cases
edited yesterday
answered yesterday
JonahJonah
2,361916
2,361916
add a comment |
add a comment |
$begingroup$
Japt, 18 16 bytes
-2 bytes from Shaggy
_=¬u1)©U>½-½aZ}a
Try it online!
$endgroup$
$begingroup$
Might be shorter using Arnauld's solution
$endgroup$
– ASCII-only
yesterday
$begingroup$
A little shuffling saves a byte.
$endgroup$
– Shaggy
yesterday
$begingroup$
Oh... of course i could have reversed that :|. Also that%1 &&
is nasty, not sure if using Arnauld's solution would be shorter (maybe not)
$endgroup$
– ASCII-only
yesterday
$begingroup$
16 bytes by reassigningZ¬u1
toZ
at the beginning of the function.
$endgroup$
– Shaggy
yesterday
$begingroup$
The other method appears to be 26:[1,-1]®*U²Ä /U/2 c ²-Z} rm
$endgroup$
– ASCII-only
15 hours ago
add a comment |
$begingroup$
Japt, 18 16 bytes
-2 bytes from Shaggy
_=¬u1)©U>½-½aZ}a
Try it online!
$endgroup$
$begingroup$
Might be shorter using Arnauld's solution
$endgroup$
– ASCII-only
yesterday
$begingroup$
A little shuffling saves a byte.
$endgroup$
– Shaggy
yesterday
$begingroup$
Oh... of course i could have reversed that :|. Also that%1 &&
is nasty, not sure if using Arnauld's solution would be shorter (maybe not)
$endgroup$
– ASCII-only
yesterday
$begingroup$
16 bytes by reassigningZ¬u1
toZ
at the beginning of the function.
$endgroup$
– Shaggy
yesterday
$begingroup$
The other method appears to be 26:[1,-1]®*U²Ä /U/2 c ²-Z} rm
$endgroup$
– ASCII-only
15 hours ago
add a comment |
$begingroup$
Japt, 18 16 bytes
-2 bytes from Shaggy
_=¬u1)©U>½-½aZ}a
Try it online!
$endgroup$
Japt, 18 16 bytes
-2 bytes from Shaggy
_=¬u1)©U>½-½aZ}a
Try it online!
edited yesterday
answered yesterday
ASCII-onlyASCII-only
3,8201236
3,8201236
$begingroup$
Might be shorter using Arnauld's solution
$endgroup$
– ASCII-only
yesterday
$begingroup$
A little shuffling saves a byte.
$endgroup$
– Shaggy
yesterday
$begingroup$
Oh... of course i could have reversed that :|. Also that%1 &&
is nasty, not sure if using Arnauld's solution would be shorter (maybe not)
$endgroup$
– ASCII-only
yesterday
$begingroup$
16 bytes by reassigningZ¬u1
toZ
at the beginning of the function.
$endgroup$
– Shaggy
yesterday
$begingroup$
The other method appears to be 26:[1,-1]®*U²Ä /U/2 c ²-Z} rm
$endgroup$
– ASCII-only
15 hours ago
add a comment |
$begingroup$
Might be shorter using Arnauld's solution
$endgroup$
– ASCII-only
yesterday
$begingroup$
A little shuffling saves a byte.
$endgroup$
– Shaggy
yesterday
$begingroup$
Oh... of course i could have reversed that :|. Also that%1 &&
is nasty, not sure if using Arnauld's solution would be shorter (maybe not)
$endgroup$
– ASCII-only
yesterday
$begingroup$
16 bytes by reassigningZ¬u1
toZ
at the beginning of the function.
$endgroup$
– Shaggy
yesterday
$begingroup$
The other method appears to be 26:[1,-1]®*U²Ä /U/2 c ²-Z} rm
$endgroup$
– ASCII-only
15 hours ago
$begingroup$
Might be shorter using Arnauld's solution
$endgroup$
– ASCII-only
yesterday
$begingroup$
Might be shorter using Arnauld's solution
$endgroup$
– ASCII-only
yesterday
$begingroup$
A little shuffling saves a byte.
$endgroup$
– Shaggy
yesterday
$begingroup$
A little shuffling saves a byte.
$endgroup$
– Shaggy
yesterday
$begingroup$
Oh... of course i could have reversed that :|. Also that
%1 &&
is nasty, not sure if using Arnauld's solution would be shorter (maybe not)$endgroup$
– ASCII-only
yesterday
$begingroup$
Oh... of course i could have reversed that :|. Also that
%1 &&
is nasty, not sure if using Arnauld's solution would be shorter (maybe not)$endgroup$
– ASCII-only
yesterday
$begingroup$
16 bytes by reassigning
Z¬u1
to Z
at the beginning of the function.$endgroup$
– Shaggy
yesterday
$begingroup$
16 bytes by reassigning
Z¬u1
to Z
at the beginning of the function.$endgroup$
– Shaggy
yesterday
$begingroup$
The other method appears to be 26:
[1,-1]®*U²Ä /U/2 c ²-Z} rm
$endgroup$
– ASCII-only
15 hours ago
$begingroup$
The other method appears to be 26:
[1,-1]®*U²Ä /U/2 c ²-Z} rm
$endgroup$
– ASCII-only
15 hours ago
add a comment |
$begingroup$
Perl 6, 33 bytes
-1 byte thanks to Grimy
{first $_>*.sqrt*(1|-1)%1>0,1..*}
Try it online!
$endgroup$
$begingroup$
-1 byte by replacing>=
with>
. Square roots of integers are either integer or irrational, so the equality case provably cannot happen.
$endgroup$
– Grimy
yesterday
1
$begingroup$
@Grimy Thanks, this seems to be allowed according to the challenge rules. (Though floating-point numbers are always rational, of course.)
$endgroup$
– nwellnhof
yesterday
add a comment |
$begingroup$
Perl 6, 33 bytes
-1 byte thanks to Grimy
{first $_>*.sqrt*(1|-1)%1>0,1..*}
Try it online!
$endgroup$
$begingroup$
-1 byte by replacing>=
with>
. Square roots of integers are either integer or irrational, so the equality case provably cannot happen.
$endgroup$
– Grimy
yesterday
1
$begingroup$
@Grimy Thanks, this seems to be allowed according to the challenge rules. (Though floating-point numbers are always rational, of course.)
$endgroup$
– nwellnhof
yesterday
add a comment |
$begingroup$
Perl 6, 33 bytes
-1 byte thanks to Grimy
{first $_>*.sqrt*(1|-1)%1>0,1..*}
Try it online!
$endgroup$
Perl 6, 33 bytes
-1 byte thanks to Grimy
{first $_>*.sqrt*(1|-1)%1>0,1..*}
Try it online!
edited yesterday
answered yesterday
nwellnhofnwellnhof
7,12511128
7,12511128
$begingroup$
-1 byte by replacing>=
with>
. Square roots of integers are either integer or irrational, so the equality case provably cannot happen.
$endgroup$
– Grimy
yesterday
1
$begingroup$
@Grimy Thanks, this seems to be allowed according to the challenge rules. (Though floating-point numbers are always rational, of course.)
$endgroup$
– nwellnhof
yesterday
add a comment |
$begingroup$
-1 byte by replacing>=
with>
. Square roots of integers are either integer or irrational, so the equality case provably cannot happen.
$endgroup$
– Grimy
yesterday
1
$begingroup$
@Grimy Thanks, this seems to be allowed according to the challenge rules. (Though floating-point numbers are always rational, of course.)
$endgroup$
– nwellnhof
yesterday
$begingroup$
-1 byte by replacing
>=
with >
. Square roots of integers are either integer or irrational, so the equality case provably cannot happen.$endgroup$
– Grimy
yesterday
$begingroup$
-1 byte by replacing
>=
with >
. Square roots of integers are either integer or irrational, so the equality case provably cannot happen.$endgroup$
– Grimy
yesterday
1
1
$begingroup$
@Grimy Thanks, this seems to be allowed according to the challenge rules. (Though floating-point numbers are always rational, of course.)
$endgroup$
– nwellnhof
yesterday
$begingroup$
@Grimy Thanks, this seems to be allowed according to the challenge rules. (Though floating-point numbers are always rational, of course.)
$endgroup$
– nwellnhof
yesterday
add a comment |
$begingroup$
Pyth, 22 21 bytes
hSm-^.Ech*d^Q2yQ2d_B1
Try it online here, or verify all the test cases at once here.
Another port of alephalpha's excellent answer, make sure to give them an upvote!
hSm-^.Ech*d^Q2yQ2d_B1 Implicit: Q=eval(input())
_B1 [1,-1]
m Map each element of the above, as d, using:
^Q2 Q^2
*d Multiply by d
h Increment
c yQ Divide by (2 * Q)
.E Round up
^ 2 Square
- d Subtract d
S Sort
h Take first element, implicit print
Edit: Saved a byte, thanks to Kevin Cruijssen
$endgroup$
1
$begingroup$
I don't know Pyth, but is it possible to create[-1,1]
in 3 bytes as well, or do you need an additional reverse so it becomes 4 bytes? If it's possible in 3 bytes, you could do that, and then change the*_d
to*d
and the+d
to-d
. Also, does Pyth not have a Minimum builtin, instead of sort & take first?
$endgroup$
– Kevin Cruijssen
yesterday
1
$begingroup$
@KevinCruijssen The order of the two elements isn't important as we're taking the minimum, though I can't think of a way of creating the pair in 3 bytes. A good catch on changing it to- ... d
though, that saves me a byte! Thanks
$endgroup$
– Sok
yesterday
$begingroup$
@KevinCruijssen Also there isn't a single byte minimum or maximum function unfortunately :o(
$endgroup$
– Sok
yesterday
1
$begingroup$
Ah, of course. You map over the values, so it doesn't matter if it's[1,-1]
or[-1,1]
. I was comparing the*d
and-d
with my 05AB1E answer, where I don't use a map, but can subtract/multiply a 2D array from/with another 2D array, so I don't need a map. Glad I could help to save a byte in that case. :) And thanks for the inspiration for my 05AB1E answer.
$endgroup$
– Kevin Cruijssen
yesterday
add a comment |
$begingroup$
Pyth, 22 21 bytes
hSm-^.Ech*d^Q2yQ2d_B1
Try it online here, or verify all the test cases at once here.
Another port of alephalpha's excellent answer, make sure to give them an upvote!
hSm-^.Ech*d^Q2yQ2d_B1 Implicit: Q=eval(input())
_B1 [1,-1]
m Map each element of the above, as d, using:
^Q2 Q^2
*d Multiply by d
h Increment
c yQ Divide by (2 * Q)
.E Round up
^ 2 Square
- d Subtract d
S Sort
h Take first element, implicit print
Edit: Saved a byte, thanks to Kevin Cruijssen
$endgroup$
1
$begingroup$
I don't know Pyth, but is it possible to create[-1,1]
in 3 bytes as well, or do you need an additional reverse so it becomes 4 bytes? If it's possible in 3 bytes, you could do that, and then change the*_d
to*d
and the+d
to-d
. Also, does Pyth not have a Minimum builtin, instead of sort & take first?
$endgroup$
– Kevin Cruijssen
yesterday
1
$begingroup$
@KevinCruijssen The order of the two elements isn't important as we're taking the minimum, though I can't think of a way of creating the pair in 3 bytes. A good catch on changing it to- ... d
though, that saves me a byte! Thanks
$endgroup$
– Sok
yesterday
$begingroup$
@KevinCruijssen Also there isn't a single byte minimum or maximum function unfortunately :o(
$endgroup$
– Sok
yesterday
1
$begingroup$
Ah, of course. You map over the values, so it doesn't matter if it's[1,-1]
or[-1,1]
. I was comparing the*d
and-d
with my 05AB1E answer, where I don't use a map, but can subtract/multiply a 2D array from/with another 2D array, so I don't need a map. Glad I could help to save a byte in that case. :) And thanks for the inspiration for my 05AB1E answer.
$endgroup$
– Kevin Cruijssen
yesterday
add a comment |
$begingroup$
Pyth, 22 21 bytes
hSm-^.Ech*d^Q2yQ2d_B1
Try it online here, or verify all the test cases at once here.
Another port of alephalpha's excellent answer, make sure to give them an upvote!
hSm-^.Ech*d^Q2yQ2d_B1 Implicit: Q=eval(input())
_B1 [1,-1]
m Map each element of the above, as d, using:
^Q2 Q^2
*d Multiply by d
h Increment
c yQ Divide by (2 * Q)
.E Round up
^ 2 Square
- d Subtract d
S Sort
h Take first element, implicit print
Edit: Saved a byte, thanks to Kevin Cruijssen
$endgroup$
Pyth, 22 21 bytes
hSm-^.Ech*d^Q2yQ2d_B1
Try it online here, or verify all the test cases at once here.
Another port of alephalpha's excellent answer, make sure to give them an upvote!
hSm-^.Ech*d^Q2yQ2d_B1 Implicit: Q=eval(input())
_B1 [1,-1]
m Map each element of the above, as d, using:
^Q2 Q^2
*d Multiply by d
h Increment
c yQ Divide by (2 * Q)
.E Round up
^ 2 Square
- d Subtract d
S Sort
h Take first element, implicit print
Edit: Saved a byte, thanks to Kevin Cruijssen
edited yesterday
answered yesterday
SokSok
4,047925
4,047925
1
$begingroup$
I don't know Pyth, but is it possible to create[-1,1]
in 3 bytes as well, or do you need an additional reverse so it becomes 4 bytes? If it's possible in 3 bytes, you could do that, and then change the*_d
to*d
and the+d
to-d
. Also, does Pyth not have a Minimum builtin, instead of sort & take first?
$endgroup$
– Kevin Cruijssen
yesterday
1
$begingroup$
@KevinCruijssen The order of the two elements isn't important as we're taking the minimum, though I can't think of a way of creating the pair in 3 bytes. A good catch on changing it to- ... d
though, that saves me a byte! Thanks
$endgroup$
– Sok
yesterday
$begingroup$
@KevinCruijssen Also there isn't a single byte minimum or maximum function unfortunately :o(
$endgroup$
– Sok
yesterday
1
$begingroup$
Ah, of course. You map over the values, so it doesn't matter if it's[1,-1]
or[-1,1]
. I was comparing the*d
and-d
with my 05AB1E answer, where I don't use a map, but can subtract/multiply a 2D array from/with another 2D array, so I don't need a map. Glad I could help to save a byte in that case. :) And thanks for the inspiration for my 05AB1E answer.
$endgroup$
– Kevin Cruijssen
yesterday
add a comment |
1
$begingroup$
I don't know Pyth, but is it possible to create[-1,1]
in 3 bytes as well, or do you need an additional reverse so it becomes 4 bytes? If it's possible in 3 bytes, you could do that, and then change the*_d
to*d
and the+d
to-d
. Also, does Pyth not have a Minimum builtin, instead of sort & take first?
$endgroup$
– Kevin Cruijssen
yesterday
1
$begingroup$
@KevinCruijssen The order of the two elements isn't important as we're taking the minimum, though I can't think of a way of creating the pair in 3 bytes. A good catch on changing it to- ... d
though, that saves me a byte! Thanks
$endgroup$
– Sok
yesterday
$begingroup$
@KevinCruijssen Also there isn't a single byte minimum or maximum function unfortunately :o(
$endgroup$
– Sok
yesterday
1
$begingroup$
Ah, of course. You map over the values, so it doesn't matter if it's[1,-1]
or[-1,1]
. I was comparing the*d
and-d
with my 05AB1E answer, where I don't use a map, but can subtract/multiply a 2D array from/with another 2D array, so I don't need a map. Glad I could help to save a byte in that case. :) And thanks for the inspiration for my 05AB1E answer.
$endgroup$
– Kevin Cruijssen
yesterday
1
1
$begingroup$
I don't know Pyth, but is it possible to create
[-1,1]
in 3 bytes as well, or do you need an additional reverse so it becomes 4 bytes? If it's possible in 3 bytes, you could do that, and then change the *_d
to *d
and the +d
to -d
. Also, does Pyth not have a Minimum builtin, instead of sort & take first?$endgroup$
– Kevin Cruijssen
yesterday
$begingroup$
I don't know Pyth, but is it possible to create
[-1,1]
in 3 bytes as well, or do you need an additional reverse so it becomes 4 bytes? If it's possible in 3 bytes, you could do that, and then change the *_d
to *d
and the +d
to -d
. Also, does Pyth not have a Minimum builtin, instead of sort & take first?$endgroup$
– Kevin Cruijssen
yesterday
1
1
$begingroup$
@KevinCruijssen The order of the two elements isn't important as we're taking the minimum, though I can't think of a way of creating the pair in 3 bytes. A good catch on changing it to
- ... d
though, that saves me a byte! Thanks$endgroup$
– Sok
yesterday
$begingroup$
@KevinCruijssen The order of the two elements isn't important as we're taking the minimum, though I can't think of a way of creating the pair in 3 bytes. A good catch on changing it to
- ... d
though, that saves me a byte! Thanks$endgroup$
– Sok
yesterday
$begingroup$
@KevinCruijssen Also there isn't a single byte minimum or maximum function unfortunately :o(
$endgroup$
– Sok
yesterday
$begingroup$
@KevinCruijssen Also there isn't a single byte minimum or maximum function unfortunately :o(
$endgroup$
– Sok
yesterday
1
1
$begingroup$
Ah, of course. You map over the values, so it doesn't matter if it's
[1,-1]
or [-1,1]
. I was comparing the *d
and -d
with my 05AB1E answer, where I don't use a map, but can subtract/multiply a 2D array from/with another 2D array, so I don't need a map. Glad I could help to save a byte in that case. :) And thanks for the inspiration for my 05AB1E answer.$endgroup$
– Kevin Cruijssen
yesterday
$begingroup$
Ah, of course. You map over the values, so it doesn't matter if it's
[1,-1]
or [-1,1]
. I was comparing the *d
and -d
with my 05AB1E answer, where I don't use a map, but can subtract/multiply a 2D array from/with another 2D array, so I don't need a map. Glad I could help to save a byte in that case. :) And thanks for the inspiration for my 05AB1E answer.$endgroup$
– Kevin Cruijssen
yesterday
add a comment |
$begingroup$
APL (Dyalog Unicode), 27 bytesSBCS
⌊/0~⍨¯1 1+2*⍨∘⌈+⍨÷⍨1(+,-)×⍨
Try it online!
Monadic train taking one argument. This is a port of alephalpha's answer.
How:
⌊/0~⍨¯1 1+2*⍨∘⌈+⍨÷⍨1(+,-)×⍨ ⍝ Monadic train
×⍨ ⍝ Square of the argument
1(+,-) ⍝ 1 ± that (returns 1+k^2, 1-k^2)
÷⍨ ⍝ divided by
+⍨ ⍝ twice the argument
∘⌈ ⍝ Ceiling
2*⍨ ⍝ Squared
¯1 1+ ⍝ -1 to the first, +1 to the second
0~⍨ ⍝ Removing the zeroes
⌊/ ⍝ Return the smallest
$endgroup$
add a comment |
$begingroup$
APL (Dyalog Unicode), 27 bytesSBCS
⌊/0~⍨¯1 1+2*⍨∘⌈+⍨÷⍨1(+,-)×⍨
Try it online!
Monadic train taking one argument. This is a port of alephalpha's answer.
How:
⌊/0~⍨¯1 1+2*⍨∘⌈+⍨÷⍨1(+,-)×⍨ ⍝ Monadic train
×⍨ ⍝ Square of the argument
1(+,-) ⍝ 1 ± that (returns 1+k^2, 1-k^2)
÷⍨ ⍝ divided by
+⍨ ⍝ twice the argument
∘⌈ ⍝ Ceiling
2*⍨ ⍝ Squared
¯1 1+ ⍝ -1 to the first, +1 to the second
0~⍨ ⍝ Removing the zeroes
⌊/ ⍝ Return the smallest
$endgroup$
add a comment |
$begingroup$
APL (Dyalog Unicode), 27 bytesSBCS
⌊/0~⍨¯1 1+2*⍨∘⌈+⍨÷⍨1(+,-)×⍨
Try it online!
Monadic train taking one argument. This is a port of alephalpha's answer.
How:
⌊/0~⍨¯1 1+2*⍨∘⌈+⍨÷⍨1(+,-)×⍨ ⍝ Monadic train
×⍨ ⍝ Square of the argument
1(+,-) ⍝ 1 ± that (returns 1+k^2, 1-k^2)
÷⍨ ⍝ divided by
+⍨ ⍝ twice the argument
∘⌈ ⍝ Ceiling
2*⍨ ⍝ Squared
¯1 1+ ⍝ -1 to the first, +1 to the second
0~⍨ ⍝ Removing the zeroes
⌊/ ⍝ Return the smallest
$endgroup$
APL (Dyalog Unicode), 27 bytesSBCS
⌊/0~⍨¯1 1+2*⍨∘⌈+⍨÷⍨1(+,-)×⍨
Try it online!
Monadic train taking one argument. This is a port of alephalpha's answer.
How:
⌊/0~⍨¯1 1+2*⍨∘⌈+⍨÷⍨1(+,-)×⍨ ⍝ Monadic train
×⍨ ⍝ Square of the argument
1(+,-) ⍝ 1 ± that (returns 1+k^2, 1-k^2)
÷⍨ ⍝ divided by
+⍨ ⍝ twice the argument
∘⌈ ⍝ Ceiling
2*⍨ ⍝ Squared
¯1 1+ ⍝ -1 to the first, +1 to the second
0~⍨ ⍝ Removing the zeroes
⌊/ ⍝ Return the smallest
answered yesterday
J. SalléJ. Sallé
1,948322
1,948322
add a comment |
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 89 85 bytes
k=>{double n=1,p;for(;Math.Abs(Math.Round(p=Math.Sqrt(n))-p)>k|p%1==0;n++);return n;}
Try it online!
-4 bytes thanks to Kevin Cruijssen!
$endgroup$
$begingroup$
You can save a byte by putting then++
in the loop, so the-1
can be removed from the return:k=>{double n=1,p;for(;Math.Abs(Math.Round(p=Math.Sqrt(0d+n))-p)>k|p%1==0;n++);return n;}
$endgroup$
– Kevin Cruijssen
yesterday
$begingroup$
Also, the0d+
can be removed, can it not?
$endgroup$
– Kevin Cruijssen
yesterday
$begingroup$
@KevinCruijssen Yes it can, I just forgot then
was already a double
$endgroup$
– Embodiment of Ignorance
23 hours ago
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 89 85 bytes
k=>{double n=1,p;for(;Math.Abs(Math.Round(p=Math.Sqrt(n))-p)>k|p%1==0;n++);return n;}
Try it online!
-4 bytes thanks to Kevin Cruijssen!
$endgroup$
$begingroup$
You can save a byte by putting then++
in the loop, so the-1
can be removed from the return:k=>{double n=1,p;for(;Math.Abs(Math.Round(p=Math.Sqrt(0d+n))-p)>k|p%1==0;n++);return n;}
$endgroup$
– Kevin Cruijssen
yesterday
$begingroup$
Also, the0d+
can be removed, can it not?
$endgroup$
– Kevin Cruijssen
yesterday
$begingroup$
@KevinCruijssen Yes it can, I just forgot then
was already a double
$endgroup$
– Embodiment of Ignorance
23 hours ago
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 89 85 bytes
k=>{double n=1,p;for(;Math.Abs(Math.Round(p=Math.Sqrt(n))-p)>k|p%1==0;n++);return n;}
Try it online!
-4 bytes thanks to Kevin Cruijssen!
$endgroup$
C# (Visual C# Interactive Compiler), 89 85 bytes
k=>{double n=1,p;for(;Math.Abs(Math.Round(p=Math.Sqrt(n))-p)>k|p%1==0;n++);return n;}
Try it online!
-4 bytes thanks to Kevin Cruijssen!
edited 23 hours ago
answered yesterday
Embodiment of IgnoranceEmbodiment of Ignorance
1,288119
1,288119
$begingroup$
You can save a byte by putting then++
in the loop, so the-1
can be removed from the return:k=>{double n=1,p;for(;Math.Abs(Math.Round(p=Math.Sqrt(0d+n))-p)>k|p%1==0;n++);return n;}
$endgroup$
– Kevin Cruijssen
yesterday
$begingroup$
Also, the0d+
can be removed, can it not?
$endgroup$
– Kevin Cruijssen
yesterday
$begingroup$
@KevinCruijssen Yes it can, I just forgot then
was already a double
$endgroup$
– Embodiment of Ignorance
23 hours ago
add a comment |
$begingroup$
You can save a byte by putting then++
in the loop, so the-1
can be removed from the return:k=>{double n=1,p;for(;Math.Abs(Math.Round(p=Math.Sqrt(0d+n))-p)>k|p%1==0;n++);return n;}
$endgroup$
– Kevin Cruijssen
yesterday
$begingroup$
Also, the0d+
can be removed, can it not?
$endgroup$
– Kevin Cruijssen
yesterday
$begingroup$
@KevinCruijssen Yes it can, I just forgot then
was already a double
$endgroup$
– Embodiment of Ignorance
23 hours ago
$begingroup$
You can save a byte by putting the
n++
in the loop, so the -1
can be removed from the return: k=>{double n=1,p;for(;Math.Abs(Math.Round(p=Math.Sqrt(0d+n))-p)>k|p%1==0;n++);return n;}
$endgroup$
– Kevin Cruijssen
yesterday
$begingroup$
You can save a byte by putting the
n++
in the loop, so the -1
can be removed from the return: k=>{double n=1,p;for(;Math.Abs(Math.Round(p=Math.Sqrt(0d+n))-p)>k|p%1==0;n++);return n;}
$endgroup$
– Kevin Cruijssen
yesterday
$begingroup$
Also, the
0d+
can be removed, can it not?$endgroup$
– Kevin Cruijssen
yesterday
$begingroup$
Also, the
0d+
can be removed, can it not?$endgroup$
– Kevin Cruijssen
yesterday
$begingroup$
@KevinCruijssen Yes it can, I just forgot the
n
was already a double$endgroup$
– Embodiment of Ignorance
23 hours ago
$begingroup$
@KevinCruijssen Yes it can, I just forgot the
n
was already a double$endgroup$
– Embodiment of Ignorance
23 hours ago
add a comment |
$begingroup$
Java (JDK), 73 bytes
k->{for(double i=1,j;;){j=Math.sqrt(++i)%1;if(j>0&&j<k||1-j<k)return i;}}
Try it online!
$endgroup$
add a comment |
$begingroup$
Java (JDK), 73 bytes
k->{for(double i=1,j;;){j=Math.sqrt(++i)%1;if(j>0&&j<k||1-j<k)return i;}}
Try it online!
$endgroup$
add a comment |
$begingroup$
Java (JDK), 73 bytes
k->{for(double i=1,j;;){j=Math.sqrt(++i)%1;if(j>0&&j<k||1-j<k)return i;}}
Try it online!
$endgroup$
Java (JDK), 73 bytes
k->{for(double i=1,j;;){j=Math.sqrt(++i)%1;if(j>0&&j<k||1-j<k)return i;}}
Try it online!
answered 18 hours ago
Sara JSara J
813
813
add a comment |
add a comment |
$begingroup$
Java 8, 85 bytes
n->{double i=1,p;for(;Math.abs(Math.round(p=Math.sqrt(i))-p)>n|p%1==0;i++);return i;}
Port of EmbodimentOfIgnorance's C# .NET answer.
Try it online.
The Math.round
can alternatively be this, but unfortunately it's the same byte-count:
n->{double i=1,p;for(;Math.abs((int)((p=Math.sqrt(i))+.5)-p)>n|p%1==0;i++);return i;}
Try it online.
$endgroup$
add a comment |
$begingroup$
Java 8, 85 bytes
n->{double i=1,p;for(;Math.abs(Math.round(p=Math.sqrt(i))-p)>n|p%1==0;i++);return i;}
Port of EmbodimentOfIgnorance's C# .NET answer.
Try it online.
The Math.round
can alternatively be this, but unfortunately it's the same byte-count:
n->{double i=1,p;for(;Math.abs((int)((p=Math.sqrt(i))+.5)-p)>n|p%1==0;i++);return i;}
Try it online.
$endgroup$
add a comment |
$begingroup$
Java 8, 85 bytes
n->{double i=1,p;for(;Math.abs(Math.round(p=Math.sqrt(i))-p)>n|p%1==0;i++);return i;}
Port of EmbodimentOfIgnorance's C# .NET answer.
Try it online.
The Math.round
can alternatively be this, but unfortunately it's the same byte-count:
n->{double i=1,p;for(;Math.abs((int)((p=Math.sqrt(i))+.5)-p)>n|p%1==0;i++);return i;}
Try it online.
$endgroup$
Java 8, 85 bytes
n->{double i=1,p;for(;Math.abs(Math.round(p=Math.sqrt(i))-p)>n|p%1==0;i++);return i;}
Port of EmbodimentOfIgnorance's C# .NET answer.
Try it online.
The Math.round
can alternatively be this, but unfortunately it's the same byte-count:
n->{double i=1,p;for(;Math.abs((int)((p=Math.sqrt(i))+.5)-p)>n|p%1==0;i++);return i;}
Try it online.
answered yesterday
Kevin CruijssenKevin Cruijssen
38.8k557200
38.8k557200
add a comment |
add a comment |
$begingroup$
MathGolf, 16 bytes
²_b*α)½╠ü²1bαm,╓
Try it online!
Not a huge fan of this solution. It is a port of the 05AB1E solution, which is based on the same formula most answers are using.
Explanation
² pop a : push(a*a)
_ duplicate TOS
b push -1
* pop a, b : push(a*b)
α wrap last two elements in array
) increment
½ halve
╠ pop a, b, push b/a
ü ceiling with implicit map
² pop a : push(a*a)
1 push 1
b push -1
α wrap last two elements in array
m explicit map
, pop a, b, push b-a
╓ min of list
$endgroup$
$begingroup$
Is every symbol considered abyte
in code golfing? Because some of your characters require more than a single byte. I don't mean to nit-pick, I'm genuinely wondering :)
$endgroup$
– schroffl
3 hours ago
$begingroup$
Good question! A "byte" in golfing relates to the minimum file size required to store a program. The text used to visualize those bytes can be any bytes. I have chosen Code Page 437 to visualize my scripts, but the important part is the actual bytes that define the source code.
$endgroup$
– maxb
2 hours ago
$begingroup$
A good example of the number of characters and number of bytes being different is this answer. Here, the'ԓ'
character is actually 2 bytes, but the rest are 1 byte characters.
$endgroup$
– maxb
2 hours ago
add a comment |
$begingroup$
MathGolf, 16 bytes
²_b*α)½╠ü²1bαm,╓
Try it online!
Not a huge fan of this solution. It is a port of the 05AB1E solution, which is based on the same formula most answers are using.
Explanation
² pop a : push(a*a)
_ duplicate TOS
b push -1
* pop a, b : push(a*b)
α wrap last two elements in array
) increment
½ halve
╠ pop a, b, push b/a
ü ceiling with implicit map
² pop a : push(a*a)
1 push 1
b push -1
α wrap last two elements in array
m explicit map
, pop a, b, push b-a
╓ min of list
$endgroup$
$begingroup$
Is every symbol considered abyte
in code golfing? Because some of your characters require more than a single byte. I don't mean to nit-pick, I'm genuinely wondering :)
$endgroup$
– schroffl
3 hours ago
$begingroup$
Good question! A "byte" in golfing relates to the minimum file size required to store a program. The text used to visualize those bytes can be any bytes. I have chosen Code Page 437 to visualize my scripts, but the important part is the actual bytes that define the source code.
$endgroup$
– maxb
2 hours ago
$begingroup$
A good example of the number of characters and number of bytes being different is this answer. Here, the'ԓ'
character is actually 2 bytes, but the rest are 1 byte characters.
$endgroup$
– maxb
2 hours ago
add a comment |
$begingroup$
MathGolf, 16 bytes
²_b*α)½╠ü²1bαm,╓
Try it online!
Not a huge fan of this solution. It is a port of the 05AB1E solution, which is based on the same formula most answers are using.
Explanation
² pop a : push(a*a)
_ duplicate TOS
b push -1
* pop a, b : push(a*b)
α wrap last two elements in array
) increment
½ halve
╠ pop a, b, push b/a
ü ceiling with implicit map
² pop a : push(a*a)
1 push 1
b push -1
α wrap last two elements in array
m explicit map
, pop a, b, push b-a
╓ min of list
$endgroup$
MathGolf, 16 bytes
²_b*α)½╠ü²1bαm,╓
Try it online!
Not a huge fan of this solution. It is a port of the 05AB1E solution, which is based on the same formula most answers are using.
Explanation
² pop a : push(a*a)
_ duplicate TOS
b push -1
* pop a, b : push(a*b)
α wrap last two elements in array
) increment
½ halve
╠ pop a, b, push b/a
ü ceiling with implicit map
² pop a : push(a*a)
1 push 1
b push -1
α wrap last two elements in array
m explicit map
, pop a, b, push b-a
╓ min of list
answered 22 hours ago
maxbmaxb
3,02311132
3,02311132
$begingroup$
Is every symbol considered abyte
in code golfing? Because some of your characters require more than a single byte. I don't mean to nit-pick, I'm genuinely wondering :)
$endgroup$
– schroffl
3 hours ago
$begingroup$
Good question! A "byte" in golfing relates to the minimum file size required to store a program. The text used to visualize those bytes can be any bytes. I have chosen Code Page 437 to visualize my scripts, but the important part is the actual bytes that define the source code.
$endgroup$
– maxb
2 hours ago
$begingroup$
A good example of the number of characters and number of bytes being different is this answer. Here, the'ԓ'
character is actually 2 bytes, but the rest are 1 byte characters.
$endgroup$
– maxb
2 hours ago
add a comment |
$begingroup$
Is every symbol considered abyte
in code golfing? Because some of your characters require more than a single byte. I don't mean to nit-pick, I'm genuinely wondering :)
$endgroup$
– schroffl
3 hours ago
$begingroup$
Good question! A "byte" in golfing relates to the minimum file size required to store a program. The text used to visualize those bytes can be any bytes. I have chosen Code Page 437 to visualize my scripts, but the important part is the actual bytes that define the source code.
$endgroup$
– maxb
2 hours ago
$begingroup$
A good example of the number of characters and number of bytes being different is this answer. Here, the'ԓ'
character is actually 2 bytes, but the rest are 1 byte characters.
$endgroup$
– maxb
2 hours ago
$begingroup$
Is every symbol considered a
byte
in code golfing? Because some of your characters require more than a single byte. I don't mean to nit-pick, I'm genuinely wondering :)$endgroup$
– schroffl
3 hours ago
$begingroup$
Is every symbol considered a
byte
in code golfing? Because some of your characters require more than a single byte. I don't mean to nit-pick, I'm genuinely wondering :)$endgroup$
– schroffl
3 hours ago
$begingroup$
Good question! A "byte" in golfing relates to the minimum file size required to store a program. The text used to visualize those bytes can be any bytes. I have chosen Code Page 437 to visualize my scripts, but the important part is the actual bytes that define the source code.
$endgroup$
– maxb
2 hours ago
$begingroup$
Good question! A "byte" in golfing relates to the minimum file size required to store a program. The text used to visualize those bytes can be any bytes. I have chosen Code Page 437 to visualize my scripts, but the important part is the actual bytes that define the source code.
$endgroup$
– maxb
2 hours ago
$begingroup$
A good example of the number of characters and number of bytes being different is this answer. Here, the
'ԓ'
character is actually 2 bytes, but the rest are 1 byte characters.$endgroup$
– maxb
2 hours ago
$begingroup$
A good example of the number of characters and number of bytes being different is this answer. Here, the
'ԓ'
character is actually 2 bytes, but the rest are 1 byte characters.$endgroup$
– maxb
2 hours ago
add a comment |
$begingroup$
Forth (gforth), 76 bytes
: f 1 begin 1+ dup s>f fsqrt fdup fround f- fabs fdup f0> fover f< * until ;
Try it online!
Explanation
Starts a counter at 1 and Increments it in a loop. Each iteration it checks if the absolute value of the counter's square root - the closest integer is less than k
Code Explanation
: f start a new word definition
1 place a counter on the stack, start it at 1
begin start and indefinite loop
1+ add 1 to the counter
dup s>f convert a copy of the counter to a float
fsqrt get the square root of the counter
fdup fround f- get the difference between the square root and the next closes integer
fabs fdup get the absolute value of the result and duplicate
f0> check if the result is greater than 0 (not perfect square)
fover f< bring k to the top of the float stack and check if the sqrt is less than k
* multiply the two results (shorter "and" in this case)
until end loop if result ("and" of both conditions) is true
; end word definition
$endgroup$
add a comment |
$begingroup$
Forth (gforth), 76 bytes
: f 1 begin 1+ dup s>f fsqrt fdup fround f- fabs fdup f0> fover f< * until ;
Try it online!
Explanation
Starts a counter at 1 and Increments it in a loop. Each iteration it checks if the absolute value of the counter's square root - the closest integer is less than k
Code Explanation
: f start a new word definition
1 place a counter on the stack, start it at 1
begin start and indefinite loop
1+ add 1 to the counter
dup s>f convert a copy of the counter to a float
fsqrt get the square root of the counter
fdup fround f- get the difference between the square root and the next closes integer
fabs fdup get the absolute value of the result and duplicate
f0> check if the result is greater than 0 (not perfect square)
fover f< bring k to the top of the float stack and check if the sqrt is less than k
* multiply the two results (shorter "and" in this case)
until end loop if result ("and" of both conditions) is true
; end word definition
$endgroup$
add a comment |
$begingroup$
Forth (gforth), 76 bytes
: f 1 begin 1+ dup s>f fsqrt fdup fround f- fabs fdup f0> fover f< * until ;
Try it online!
Explanation
Starts a counter at 1 and Increments it in a loop. Each iteration it checks if the absolute value of the counter's square root - the closest integer is less than k
Code Explanation
: f start a new word definition
1 place a counter on the stack, start it at 1
begin start and indefinite loop
1+ add 1 to the counter
dup s>f convert a copy of the counter to a float
fsqrt get the square root of the counter
fdup fround f- get the difference between the square root and the next closes integer
fabs fdup get the absolute value of the result and duplicate
f0> check if the result is greater than 0 (not perfect square)
fover f< bring k to the top of the float stack and check if the sqrt is less than k
* multiply the two results (shorter "and" in this case)
until end loop if result ("and" of both conditions) is true
; end word definition
$endgroup$
Forth (gforth), 76 bytes
: f 1 begin 1+ dup s>f fsqrt fdup fround f- fabs fdup f0> fover f< * until ;
Try it online!
Explanation
Starts a counter at 1 and Increments it in a loop. Each iteration it checks if the absolute value of the counter's square root - the closest integer is less than k
Code Explanation
: f start a new word definition
1 place a counter on the stack, start it at 1
begin start and indefinite loop
1+ add 1 to the counter
dup s>f convert a copy of the counter to a float
fsqrt get the square root of the counter
fdup fround f- get the difference between the square root and the next closes integer
fabs fdup get the absolute value of the result and duplicate
f0> check if the result is greater than 0 (not perfect square)
fover f< bring k to the top of the float stack and check if the sqrt is less than k
* multiply the two results (shorter "and" in this case)
until end loop if result ("and" of both conditions) is true
; end word definition
answered 20 hours ago
reffureffu
62126
62126
add a comment |
add a comment |
$begingroup$
Jelly, 13 bytes
I have not managed to get anything terser than the same approach as alephalpha
- go upvote his Mathematica answer!
²;N$‘÷ḤĊ²_Ø+Ṃ
Try it online!
How?
²;N$‘÷ḤĊ²_Ø+Ṃ - Link: number, n (in (0,1))
² - square n -> n²
$ - last two links as a monad:
N - negate -> -(n²)
; - concatenate -> [n², -(n²)]
‘ - increment -> [1+n², 1-(n²)]
Ḥ - double n -> 2n
÷ - divide -> [(1+n²)/n/2, (1-(n²))/n/2]
Ċ - ceiling -> [⌈(1+n²)/n/2⌉, ⌈(1-(n²))/n/2⌉]
² - square -> [⌈(1+n²)/n/2⌉², ⌈(1-(n²))/n/2⌉²]
Ø+ - literal -> [1,-1]
_ - subtract -> [⌈(1+n²)/n/2⌉²-1, ⌈(1-(n²))/n/2⌉²+1]
Ṃ - minimum -> min(⌈(1+n²)/n/2⌉²-1, ⌈(1-(n²))/n/2⌉²+1)
$endgroup$
add a comment |
$begingroup$
Jelly, 13 bytes
I have not managed to get anything terser than the same approach as alephalpha
- go upvote his Mathematica answer!
²;N$‘÷ḤĊ²_Ø+Ṃ
Try it online!
How?
²;N$‘÷ḤĊ²_Ø+Ṃ - Link: number, n (in (0,1))
² - square n -> n²
$ - last two links as a monad:
N - negate -> -(n²)
; - concatenate -> [n², -(n²)]
‘ - increment -> [1+n², 1-(n²)]
Ḥ - double n -> 2n
÷ - divide -> [(1+n²)/n/2, (1-(n²))/n/2]
Ċ - ceiling -> [⌈(1+n²)/n/2⌉, ⌈(1-(n²))/n/2⌉]
² - square -> [⌈(1+n²)/n/2⌉², ⌈(1-(n²))/n/2⌉²]
Ø+ - literal -> [1,-1]
_ - subtract -> [⌈(1+n²)/n/2⌉²-1, ⌈(1-(n²))/n/2⌉²+1]
Ṃ - minimum -> min(⌈(1+n²)/n/2⌉²-1, ⌈(1-(n²))/n/2⌉²+1)
$endgroup$
add a comment |
$begingroup$
Jelly, 13 bytes
I have not managed to get anything terser than the same approach as alephalpha
- go upvote his Mathematica answer!
²;N$‘÷ḤĊ²_Ø+Ṃ
Try it online!
How?
²;N$‘÷ḤĊ²_Ø+Ṃ - Link: number, n (in (0,1))
² - square n -> n²
$ - last two links as a monad:
N - negate -> -(n²)
; - concatenate -> [n², -(n²)]
‘ - increment -> [1+n², 1-(n²)]
Ḥ - double n -> 2n
÷ - divide -> [(1+n²)/n/2, (1-(n²))/n/2]
Ċ - ceiling -> [⌈(1+n²)/n/2⌉, ⌈(1-(n²))/n/2⌉]
² - square -> [⌈(1+n²)/n/2⌉², ⌈(1-(n²))/n/2⌉²]
Ø+ - literal -> [1,-1]
_ - subtract -> [⌈(1+n²)/n/2⌉²-1, ⌈(1-(n²))/n/2⌉²+1]
Ṃ - minimum -> min(⌈(1+n²)/n/2⌉²-1, ⌈(1-(n²))/n/2⌉²+1)
$endgroup$
Jelly, 13 bytes
I have not managed to get anything terser than the same approach as alephalpha
- go upvote his Mathematica answer!
²;N$‘÷ḤĊ²_Ø+Ṃ
Try it online!
How?
²;N$‘÷ḤĊ²_Ø+Ṃ - Link: number, n (in (0,1))
² - square n -> n²
$ - last two links as a monad:
N - negate -> -(n²)
; - concatenate -> [n², -(n²)]
‘ - increment -> [1+n², 1-(n²)]
Ḥ - double n -> 2n
÷ - divide -> [(1+n²)/n/2, (1-(n²))/n/2]
Ċ - ceiling -> [⌈(1+n²)/n/2⌉, ⌈(1-(n²))/n/2⌉]
² - square -> [⌈(1+n²)/n/2⌉², ⌈(1-(n²))/n/2⌉²]
Ø+ - literal -> [1,-1]
_ - subtract -> [⌈(1+n²)/n/2⌉²-1, ⌈(1-(n²))/n/2⌉²+1]
Ṃ - minimum -> min(⌈(1+n²)/n/2⌉²-1, ⌈(1-(n²))/n/2⌉²+1)
answered 19 hours ago
Jonathan AllanJonathan Allan
52.3k535170
52.3k535170
add a comment |
add a comment |
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f180412%2fsquare-root-distance-from-integers%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown