Let's talk about divisors...

Leaving out perfect squares (for a moment), all positive integers can be expressed as the product of 2 of their divisors. Quick example for 126: Here are all the divisors of 126

As you can see all the divisors can be paired. Here are what we will call the Divisor Pairs:[1, 126], [2, 63], [3, 42], [6, 21], [7, 18], [9, 14]

For this challenge we will need only the last pair of this list (which is the center pair of the picture):[9,14].We will call this pair the MaxMin Divisor Pair.
The Difference of MaxMin Divisor Pair(DMDP) is the difference of the two elements of the pair which is [9,14]=5
One more example for 544. The divisors are:

[1, 2, 4, 8, 16, 17, 32, 34, 68, 136, 272, 544]

and DMDP(544)=15 because 32-17=15

What about the perfect squares? All perfect squares have DMDP=0
Let's take for example 64 with divisors

{1, 2, 4, 8, 16, 32, 64}

As you can see in this case the MaxMin Divisor Pair is [8,8] which has DMDP=0
we are almost done..

The Challenge

Given an integer n>0, output how many integers less than or equal to10000, have DMDP less thann

\$\begingroup\$Wouldn't it make more sense to have the 10000 as a second, variable, input?\$\endgroup\$
– Jonathan AllanAug 29 '17 at 23:35

1

\$\begingroup\$Yes, I thought about that but it would not add anything to the challenge. In this way I think it is easier for everybody to understand the challenge.\$\endgroup\$
– user73398Aug 29 '17 at 23:39

\$\begingroup\$ÆDạ"Ṛ$Ṃ saves you a byte over ÆDạ:@¥⁸Ṃ (I had ạ"ṚṂ...ȷ4RÆDÇ€<⁸S for 15 - too similar - EDIT: hmm or was it, no : involved... what do you think?)\$\endgroup\$
– Jonathan AllanAug 30 '17 at 0:22

\$\begingroup\$@JonathanAllan I think you should post this 13-byter\$\endgroup\$
– Leaky NunAug 30 '17 at 0:25

\$\begingroup\$Oh wow. nah you go for it, I saved you one byte that saves another 2!\$\endgroup\$
– Jonathan AllanAug 30 '17 at 0:27

Java 8, 151111110 101 bytes

n->{ // Method with integer as parameter and return-type
int r=0, // Result-integer
x=10000, // Index-integer starting at 10,000
i; // Another index-integer for the inner loop
for(;x-->0; // Loop (1) from 10,000 down to 0
r-=i-n>>-1) // If the MaxMin-Divisor Pair's difference is lower than the input,
// add 1 to the result (after every iteration)
for(i=x, // Set `i` to `x`
i-->1;) // Inner loop (2) from `i` downwards to 1
if(x>=i*i // If the current square-root of `x` is smaller than or equal to `i`,
&x%i<1){ // and if the current `x` is divisible by `i`:
i=x/i-i; // Calculate the MaxMin-Division difference
break;} // And leave the inner loop (2)
// End of inner loop (2) (implicit / single-line body)
// End of loop (1) (implicit / single-line body)
return r; // Return the result
} // End of method

\$\begingroup\$Don't have time to try it myself, but would it be shorter to have the inner loop start from x and have an extra variable for the current minimum?\$\endgroup\$
– JollyJokerAug 30 '17 at 13:00

\$\begingroup\$@Nevay Thanks again, really need to remember x>=i*i as alternative for using Math.sqrt, since this is the second time you've golfed that in my code.\$\endgroup\$
– Kevin CruijssenAug 31 '17 at 12:04

\$\begingroup\$Ah, I didn't notice the DMDP is the min diff of that factor list! Very nice. I think sum(sapply(1:1e4,function(x)min(abs((w=which(x%%1:x<1))-rev(w))))<scan()) is a bit shorter\$\endgroup\$
– GiuseppeAug 29 '17 at 23:54

VB.NET (.NET 4.5) 116 115 bytes

Function A(n)
For i=1To 10^4
Dim s As Byte=Math.Sqrt(i)
While i Mod s>0
s-=1
End While
A-=i/s-s<n
Next
End Function

Explanation:

A function that takes n as a parameter, and returns the result.

Starts at the square root, and looks for the nearest integer that evenly divides (will be the smaller of the MaxMin Divisor Pair). Then gets the larger of the pair (i/s), finds the difference, and compares against the input.

Golfing strategies used:

Dim is expensive, so the fewer variables I declare the better.

I start searching at the square root, but only want to look at integers. By declaring s as an integral type, it casts to the floor for me.

VB uses ^ as exponent. So while 10000 is 5 characters, 10^4 is only 4.

VB creates an automatic variable with the same name and type as the function definition (in my case A). At the end of the function, if there is no return, the value of the function variable will be returned instead. So I save characters by not declaring a separate variable and not using a return statement.

VB has very forgiving typing/casting. i is assumed Integer because I assigned a integer literal. A is assumed Object but as soon as I add an integer, it behaves like an Integer.

Rather than if checking the that the difference is satisfactory, add it directly to the result by casting the boolean to an integer. However, VB uses -1 for True, so subtract to get the correct sign.

Technically, we want Mod to not be 0. Taking the modulus of a negative number in VB.NET will give a negative result. But, everything is positive so I can save a byte by turning <> into >.

The largest number to check is 10000. The square root of that is 100. So I only need a Byte to store that, saving bytes in the declaration by using a shorter named type.

Your Answer

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

Post as a guest

Post as a guest

Code Golf Stack Exchange is a site for recreational programming competitions, not general programming questions. Challenges must have an objective scoring criterion, and it is highly recommended to first post proposed challenges in the Sandbox.