Écrivez du code qui, lorsqu'il reçoit un nombre positif en entrée, génère le plus grand diviseur positif de x inférieur ou égal à la racine carrée de x .
En d'autres termes, trouvez le plus grand tel que
(Existe supérieur ou égal à n tel que m fois n soit x )
Par exemple, si l'entrée était les diviseurs sont 1 , 2 , 3 , 4 , 6 et 12 . 1 , 2 et 3 multiplient tous par de plus grands nombres pour obtenir 12 , mais étant le plus grand, nous retournons donc 3 .
Il s'agit de code-golf, donc les réponses seront notées en octets, moins d'octets étant considérés comme un meilleur score.
Cas de test
(1,1)
(2,1)
(3,1)
(4,2)
(5,1)
(6,2)
(7,1)
(8,2)
(9,3)
(10,2)
(11,1)
(12,3)
(13,1)
(14,2)
(15,3)
(16,4)
(17,1)
(18,3)
(19,1)
(20,4)
(21,3)
(22,2)
(23,1)
(24,4)
(25,5)
(26,2)
(27,3)
(28,4)
(29,1)
(30,5)
(31,1)
(32,4)
(33,3)
(34,2)
(35,5)
(36,6)
(37,1)
(38,2)
(39,3)
(40,5)
(41,1)
(42,6)
(43,1)
(44,4)
(45,5)
(46,2)
(47,1)
(48,6)
(49,7)
(50,5)
Réponses:
Python3 ,
4947 octetsExplication
l=x**.5//1
→ Attribuerl
le plus grand entier inférieur à égal à la racine carrée dex
while x%l:l-=1
→ Tandis quel
ne se divise pas uniformémentx
, décrémenterl
.Modifications
...//1
pour enregistrer deux octets. (Les décimales vont bien! Merci @Rod)la source
input
/ à laprint
placedef
/return
, vous pouvez également remplacerint(...)
par...//1
pour enregistrer plus d'octets comme vous pouvez le voir iciMATL , 7 octets
Essayez-le en ligne!
Pour cette explication, nous utiliserons «12» comme exemple d'entrée. Explication:
Cela fonctionne en raison de nombreuses coïncidences heureuses.
<n>)
indexerala source
Z\J2/)
(J2/
ou équivaut.5j
àend/2
lorsqu'il est utilisé comme index)C (gcc)
-lm
, 35 octetsEssayez-le en ligne!
la source
sqrt
une fonction intégrée. Avec-fno-builtin-sqrt
, gcc supposeint sqrt(int)
et ne passe pas adouble
. Sur x86-64,double
est passé dans un registre différent d'un entier. Sur 32 bits, adouble
prendrait 2 emplacements sur la pile, vous passeriez donc également des ordures (ou une sous-normale avec l'entier comme bas de la mantisse, si les 32 bits supérieurs étaient zéro). Cela casse également, sauf si vous effectuez une génération de débogage, car il repose sur le code-gen non optimisé par défaut de gcc pour évaluer les expressions dans le registre de valeur de retour.sqrt()
issue is different: I was curious how that managed to work, because the caller has to somehow know to convertint
todouble
. I posted the answer to that as a comment in case anyone else was curious. Effectively gcc hassqrt
(including prototype) as a built-in, otherwise this would fail for reasons we sometimes see in SO asm Qsi;f(n){for(i=0;++i<n/i||n%i;);}
is 31B, and works withgcc -O
on x86-64 (costing 2 or 3 more bytes for the command line option.) Using||
instead of|
causes gcc to leave then/i
result fromidiv
in EAX, the return-value register (godbolt.org/g/RJYeui). The undefined-behaviour from++i
without a sequence point happens to work. (The asm produced is basically the same as my x86 machine code answer.) With-O0
, gcc always seems to leavei
in EAX, but maybe we can use that...05AB1E, 5 bytes
Try it online! or as a Test suite
Explanation
la source
APL (Dyalog Unicode),
161412 bytesI'm glad I was able to write some answer in APL since I only just learned it. Many, many thanks to Adám for help with golfing. Golfing suggestions very much welcome. Try it online!
To learn more about APL, take a look at The APL Orchard.
EDIT: -2 bytes to fixing a problem with my code. Thanks to H.PWiz for pointing out that problem. -2 bytes from shortening everything again.
Ungolfing
la source
Husk, 4 bytes
Try it online!
Explanation
la source
R,
4533 bytesTry it online!
Original:
Try it online!
la source
Code machine x86 32 bits (IA32):
1816 octetschangelog: gérer
n=1
correctement le cas de test, enregistrer 2 octets et retourner dans EAX.Comptez jusqu'à
n/i <= i
(c'est-à-dire lorsque nous atteignons le sqrt), et utilisez le premier diviseur exact après cela.Une version 64 bits de ceci est appelable depuis C avec la convention d'appel System V x86-64, comme
int squarish_root_countup(int edi)
.nasm -felf32 -l/dev/stdout squarish-root.asm
:Essayez-le en ligne! avec un appelant asm qui utilise directement le premier octet d'argv [1] comme entier et utilise le résultat comme état de sortie du processus.
la source
Japt
-h
,86 octetsEssayez-le
2 octets économisés grâce à Oliver
Explication
la source
Jelly, 5 bytes
Try it online!
la source
JavaScript ES7,
3331 bytesTry it online
la source
Snowman, 38 bytes
Try it online!
la source
dc, 24
Try it online!
Explanation:
la source
J,
2419 bytes-5 bytes thanks to Sherlock's GCD idea
Try it online!
original answer
Try it online!
parsed
explanation
1 + i.@<.@%:
gives the range1 .. floor(sqrt)
.(A) B
forms a hook, with the above range passed as the right arg]
to A and the original number passed as its left arg[
. Thus...] | [
gives the remainer of each item in the range divided into the original arg.0 = ] | [
gives the divisors with no remainder.] #~ ...
then filters the range, leaving only those.{:
gives the last item in the list, ie, the largest one.la source
Jelly, 5 bytes
Try it online!
la source
Haskell, 36 bytes
Try it online!
Well this is my answer to this challenge. This uses a particular list comprehension to find the answer. In our list comprehension we picky from the infinite list z from the list (y,z) is all the ordered pairs such that y≥z .
[1..]
that is the positive integers, and we pick[1..y]
. This means thatWe then select only those pairs such thaty⋅z=x , meaning that we make the list of all pairs of numbers that multiply to x . Now since our comprehension is based first on y and then z this means that our pairs are in ascending order of y , or more usefully in descending order of z .
So to get the largestz we take the z belonging to the first element. This is our result.
la source
QBasic (4.5), 52 bytes
la source
Forth (gforth), 53 bytes
The shortest way seems to be using the floating point stack and
fsqrt
, the shortest I could get without it was 62 bytes using/mod
and checking if the quotient was greater than the divisor.Try it online!
Explanation
Code Explanation
la source
F#,
5549 bytesTry it online!
Seq.findBack
: Returns the last element for which the given function returnsTrue
. The function in this case checks to see if a number is a factor of the value.la source
Brain-Flak, 144 bytes
Try it online!
I'm not really sure this answer is very good. I feel like there may be a nice way to solve this task however I'm just not clever enough.
Explanation
I tried to do an exploded view of the answer but there are so many moving parts it was not very enlightening, so here is an explanation of what the code does.
The first important bit is this
This takes the two numbers on top of the stack and if they are unequal increments the second one, if they are equal it increments the first one and replaces the second one with zero. If we repeat this code a bunch we will get all the pairs(x,y) such that x≥y .
The next part is multiplication, taken with modification from the wiki. This multiplication is special because it preserves the existing values without destroying them. It goes like:
So we are multiplying all these ordered pairs. For each result we check if it is equal to the input. If so we terminate and return the smaller item in the pair.
la source
Python 2, 41 bytes
Try it online!
la source
Jelly, 6 bytes
Try it online!
la source
Perl 5
-p
, 26 bytesTry it online!
la source
Rust,
7170 bytesPre-uglified version
Edits
> 0
over!= 0
. (Thanks to @CatWizard)la source
!=
be replaced with>
?Japt, 8 bytes
Try it online!
la source
Triangularity, 49 bytes
Try it online!
la source
Pyret, 93 bytes
You can try this online by copying it into the online Pyret editor!
The above evaluates to an anonymous function. When it is applied to an integer, it returns a result according to the specification.
la source
Actually, 7 bytes
Based on my APL answer here. Golfing suggestions welcome! Try it online!
Ungolfing
la source
A port of this Mathematica answer.
Jelly, 11 bytes
Try it online!
This (11 bytes) also works, and don't depend on
³
:Unfortunately
½Ḟ÷@Ċ÷@ʋÐL
(10 bytes) doesn't work. And apparentlyƬ
andÐĿ
is not exactly the same (when the link is dyadic)Method: (letn be the input)
la source
Java 8,
6554 bytesPort of @hunteke's Python 3 answer.
Try it online.
Old 65 bytes answer:
Try it online.
Explanation:
la source