| Bytes | Lang | Time | Link |
|---|---|---|---|
| 044 | Dyalog APL | 250729T031304Z | Aaron |
| 015 | Japt x | 170830T094449Z | Shaggy |
| 052 | Wolfram Language Mathematica | 201226T232905Z | att |
| 098 | Java 8 | 170830T080657Z | Kevin Cr |
| 104 | C# .NET Core | 170831T070339Z | Dennis.V |
| 115 | VB.NET .NET 4.5 | 170830T160540Z | Brian J |
| 046 | TIBASIC | 170830T155239Z | Josiah W |
| 062 | Ruby | 170830T125016Z | Cristian |
| 012 | 05AB1E | 170830T083116Z | Emigna |
| 941 | PHP | 170830T105717Z | Titus |
| 019 | Husk | 170830T083300Z | Zgarb |
| 060 | JavaScript ES7 | 170829T234718Z | Neil |
| 083 | Python 2 | 170830T054322Z | anon |
| 064 | Mathematica | 170830T035553Z | JungHwan |
| 078 | Mathematica | 170830T001118Z | ZaMoC |
| 013 | Jelly | 170829T235607Z | Leaky Nu |
| 115 | Mathematica | 170830T004459Z | totallyh |
| 134 | Python 2 | 170830T000258Z | totallyh |
| 091 | R | 170830T000518Z | Giuseppe |
| 073 | R | 170829T235204Z | MickyT |
| 020 | MATL | 170829T235641Z | Luis Men |
Dyalog APL, 44
{+/⍵>{-/⍵⌷⍨⊂(⌈¨.1∘+,⊢)(2÷⍨≢)⍵}(∪⊢∨⍳)¨⍳10000}
Explanation
{+/⍵> ¨⍳10000} Sum those that are less than the arg from 1 to 10,000
( )
⊢∨⍳ Find the greatest common divisor between the argument and [1..argument]
∪ Get unique list
{ } The DMDP part:
(2÷⍨≢)⍵ Find the mid point of the vector
( ,⊢) Concat the answer with
.1∘+ just a teeny bit more
⌈¨ and get the max for each. This causes even-length vectors to pick elements left and right of the midpoint, and odd-length vectors to give the exact middle twice
⍵⌷⍨⊂ Enclose and select (backwards) from the original
-/ and get the difference
Japt -x, 25 19 17 15 bytes
L²õÈâ ËaX/DÃd<U
L²õÈâ ËaX/DÃd<U :Implicit input of integer U
L :100
² :Squared
õ :Range [1,L²]
È :Map each X
â : Divisors
Ë : Map each D
aX/D : Absolute difference with X/D
à : End inner map
d<U : Any less than U?
:Implicit output of sum of resulting array
Java 8, 151 111 110 101 98 bytes
n->{int r=0,x=10000,i;for(;x-->0;r-=i-n>>-1)for(i=x;i-->1&&(x<i*i|x%i>0||(i=x/i-i)>i););return r;}
-10 bytes thanks to @Nevay.
-3 bytes thanks to @ceilingcat.
Explanation:
n->{ // Method with integer as parameter and return-type
int r=0, // Result-integer, starting at 0
x=10000, // Index-integer `x` for the outer loop, starting at 10,000
i; // Index-integer `i` for the inner loop, uninitialized
for(;x-->0; // Loop `x` in the range (10000, 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;i-->1 // Inner loop `i` in the range (`x`, 1]:
&&(x<i*i // If the current square of `i` is smaller than or equals to `x`,
|x%i>0 // and the current `x` is divisible by `i`:
||(i=x/i-i)// Calculate the MaxMin-Division difference
>i);); // And stop the inner loop
return r;} // After the loops, return the result
C# (.NET Core), 104 bytes
x=>{int o=0,i=1;for(;i<=10000;i++)for(int j=1;j<=i;j++)if(i%j<1&Math.Abs(j-i/j)<x){o++;break;}return o;}
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:
Dimis 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
sas an integral type, it casts to the floor for me. - VB uses
^as exponent. So while10000is 5 characters,10^4is 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.
iis assumedIntegerbecause I assigned a integer literal.Ais assumedObjectbut as soon as I add an integer, it behaves like anInteger. - Rather than
ifchecking the that the difference is satisfactory, add it directly to the result by casting the boolean to an integer. However, VB uses-1forTrue, so subtract to get the correct sign. - Technically, we want
Modto not be0. 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
Byteto store that, saving bytes in the declaration by using a shorter named type.
TI-BASIC, 46 bytes
Note that TI-BASIC is a tokenized language. Also, the E in line 2 is a small capital E, found by pressing 2ND+, .
Input A
DelVar DFor(B,1,E4
For(C,1,√(B
If not(fPart(B/C
B/C-C<A
End
D+Ans→D
End
Result will be in D, and Ans immediately after program execution. If it is to be displayed, adding two more bytes (newline and Ans) would suffice.
05AB1E, 19 18 17 16 15 12 bytes
4°ƒNÑÂα{нI‹O
Explanation
4°ƒ # for N in [0 ... 10**4] do:
NÑ # push divisors of N
 # bifurcate
α # element-wise absolute difference
{ # sort
н # pop the head (smallest difference)
I‹ # is it smaller than the input?
O # sum the stack
PHP, 94+1 bytes
for(;$n++<1e4;$c+=$d<$argn)if(($i=$n**.5)>~~$i){while($n%++$i);for($d=1;$n%--$i;)$d++;}echo$c;
Run as pipe with -nR or try it online.
Husk, 19 bytes
#ȯV<⁰Sz≠↔§f`¦ḣḣ□100
No TIO link, since it times out. This version uses 100 in place of 10000 and finishes in a couple of seconds.
Explanation
Husk has no divisors built-in or support for scientific notation yet.
#ȯV<⁰Sz≠↔§f`¦ḣḣ□100 Input is n (accessed with ⁰).
□100 Square of 100: 10000
ḣ Inclusive range from 1.
# Count number of elements for which
ȯ this composition of 3 functions gives truthy result:
Argument k, say k = 12.
§f`¦ḣ Divisors of k:
ḣ Range: [1,2,3,..,12]
§f Filter by
`¦ divides k: [1,2,3,4,6,12]
Sz≠↔ Absolute differences of divisor pairs:
↔ Reverse: [12,6,4,3,2,1]
Sz Zip with divisor list
≠ using absolute difference: [11,4,1,1,4,11]
V<⁰ Is any of these less than n?
JavaScript (ES7), 60 bytes
f=(n,i=1e4,j=i**.5|0)=>i?i%j?f(n,i,j-1):(i/j-j<n)+f(n,i-1):0
Probably exceeds your recursion limit, so you might prefer the iterative version for 70 bytes:
n=>[...Array(1e4)].map(g=(j=++i**.5|0)=>i%j?g(j-1):k+=i/j-j<n,i=k=0)|k
Python 2, 83 bytes
A bit on the slow side, uses 5 seconds per test case
lambda x:sum(x>min(abs(y/t-t)for t in range(1,y+1)if y%t<1)for y in range(1,10001))
Mathematica, 64 bytes
Count[Divisors~Array~1*^4,a_/;#+a[[i=⌈Tr[1^a]/2⌉]]>a[[-i]]]&
Usage
f = Count[Divisors~Array~1*^4,a_/;#+a[[i=⌈Tr[1^a]/2⌉]]>a[[-i]]]&
f[1]
100
f /@ {1, 5, 13, 369, 777, 2000, 5000, 9000, 10000, 20000}
{100, 492, 1201, 6175, 7264, 8478, 9440, 9888, 10000, 10000}
Explanation
Divisors~Array~1*^4
Generate the lists of divisors, from 1 to 10000. (the lists of divisors are sorted automatically)
Count[ ..., a_/; ... ]
Count the occurrences of elements a, such that...
#+a[[i=⌈Tr[1^a]/2⌉]]>a[[-i]]]
(input) + (left one of the middle element(s)) > (right one of the middle element(s)) If there is only one middle element, left = right.
Mathematica, 78 bytes
(s=#;Tr[1^Select[Table[#2-#&@@Quantile[Divisors@i,{.5,.51}],{i,10^4}],#<s&]])&
Mathematica, 119 115 bytes
(n=#;Tr[1^Select[Last@#-First@#&/@(Take[Divisors@#,Round[{-.1,.1}+(1+Length@Divisors@#)/2]]&/@Range@10000),#<n&]])&
I finally got this thing working and I've been trying for the past half an hour. ._.
Example run
Python 2, 134 bytes
lambda i:len(filter(lambda n:n<i,[reduce(lambda x,y:y-x,[[x,n/x]for x in range(1,int(n**.5+1))if n%x<1][-1])for n in range(1,10001)]))
Eugh... need to do much better.
R, 91 bytes
function(n)sum(sapply(1:1e4,function(x,d=(1:x)[x%%1:x<1])diff(d[median(seq(d))+.5*0:1]))<n)
Takes a different (worse) approach to computing the DMDP than MickyT's solution by using array indexing and diff to compute it. Alas.
R, 73 77 bytes
Thanks to @Guiseppe for the 4 bytes
sum(sapply(1:1e4,function(x)min(abs((w=which(x%%1:x<1))-rev(w))))<scan())
Have lost the vectorize function to calculate the DMDP and is now using a sapply over the function. The truthies for items which are less than the input are summed for the result.
MATL, 20 bytes
1e4:"@Z\2Y"dJ2/)G<vs
The code times out in TIO. Here's an example run with the offline compiler:
>> matl 1e4:"@Z\2Y"dJ2/)G<vs
> 13
1201
