19
2
Write code that when given a positive number \$x\$ as input, outputs the largest positive divisor of \$x\$ less than or equal to the square root of \$x\$.
In other words find the largest \$n > 0\$ such that
\$\exists m\geq n:m\cdot n=x\$
(Exists \$m\$ greater than or equal to \$n\$ such that \$m\$ times \$n\$ is \$x\$)
For example if the input were \$12\$ the divisors are \$1\$, \$2\$, \$3\$, \$4\$, \$6\$, and \$12\$. \$1\$, \$2\$ and \$3\$ all multiply by larger numbers to get \$12\$, but \$3\$ is the largest so we return \$3\$.
This is code-golf so answers will be scored in bytes with fewer bytes being considered a better score.
Test Cases
(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)
11I don't see how closing popular questions as dupes of older inactive ones help the site...? If you notice it early, sure, go ahead and hammer it. If it has twice the number of answers and more upvotes than the old one. Keep it, and if anything, close the other one... – Stewie Griffin – 2018-06-21T07:16:30.287
@StewieGriffin A problem with "popular questions" is that they're on HNQ. Which is probably not a very good thing. / I don't see how it harms the site either, you can just move the answers to the old one. – user202729 – 2018-06-21T08:02:34.870
5The HNQ might attract new users, and that's a good thing (IMO). – Stewie Griffin – 2018-06-21T09:56:18.587
Come on, this isn't a dupe. The linked question requires both numbers, this one requires one – qwr – 2018-06-21T22:59:07.293
1@qwr But the core idea is the same. The difference is very small. Method in each challenge can be used for another. – user202729 – 2018-06-22T00:47:34.377
... and is still competitive. For example the Python2/3 solutions here. – user202729 – 2018-06-22T01:12:42.950
@user202729 it is not the same. For example Peter Cordes's solution below would have to be modified – qwr – 2018-06-22T02:47:01.083
This gotta be an OEIS somehow, right? – htmlcoderexe – 2018-06-22T08:05:51.087
@htmlcoderexe There is! It is at the bottom of the question. – Post Rock Garf Hunter – 2018-06-22T13:04:28.687
@CatWizard absolutely missed that! – htmlcoderexe – 2018-06-22T13:44:50.453
@CatWizard, I don't quite understand why my question is being closed as the duplicate when you're claiming this one is different and mine predates yours by over 5 years. I honestly don't give a crap about the condition of my forgotten question, but I'm concerned about a community that endorses that kind of sabotage. I'll happily take this to meta, but I'm not sure how to get there.
– Hand-E-Food – 2018-06-25T23:10:05.8631
@Hand-E-Food I don't claim this one is different. In fact I do believe that the two have the same content. My reasons for the closure of your question are the same as those in the comment at the top of the thread, this question has more answers. The meta is here if you would like to ask there. You may also have interest in this.
– Post Rock Garf Hunter – 2018-06-25T23:18:10.227Thanks for those links! I appreciate getting a better understanding of the policy. – Hand-E-Food – 2018-06-26T00:27:40.803