g | x | w | all
Bytes Lang Time Link
048Wolfram Language Mathematica181217T061805Zatt
103Clojure240903T144735ZNikoNyrh
026Charcoal181216T234838ZNeil
078Python 2181218T034609ZChas Bro
010Jelly181216T234943ZJonathan
012Jelly181216T193333ZErik the
090JavaScript ES6181216T213612ZArnauld
01505AB1E181217T101910ZKevin Cr

Wolfram Language (Mathematica), 69 ... 52 51 48 bytes

(-##&@@@-1^#).GCD@@@(#!/(#+2)!)&@*Subsets@*Range

Try it online!

1-indexed. Much faster than the previous version.

Fairly straightforward implementation of inclusion-exclusion.

                                @*Subsets@*Range    subsets of 1..n
 -##&@@@-1^#                                        include/exclude?
                     #!/(#+2)!                      index -> inverse neighbor-product
              GCD@@@(         )                     subset density
             .                                      collect

GCD evaluates to 0 with no arguments, in contrast to LCM which does not evaluate.

Clojure, 103 bytes

#(/(count(for[i(range 1000000):when(first(for[j(range 2 %):when(=(mod i(*(inc j)j))0)]j))]i))1000000.0)

The input is "3-indexed", so you need to call it like this to test the case n = 12:

(def f #(...))
((comp f (partial + 3)) 12)
; 0.215085

TIO.

Charcoal, 26 bytes

FN⊞υ×⁺²ι⁺³ιI∕LΦΠυ¬⌊Eυ﹪ιλΠυ

Try it online! Link is to verbose version of code. Hopelessly inefficient (O(n!²)) so only works up to n=4 on TIO. Explanation:

FN⊞υ×⁺²ι⁺³ι

Input n and calculate the first n products of neighbouring factors.

I∕LΦΠυ¬⌊Eυ﹪ιλΠυ

Take the product of all of those factors and use that to calculate the proportion of numbers having at least one of those factors.

30-byte less slow version is only O(n!) so can do up to n=6 on TIO:

F⊕N⊞υ⁺²ιI∕LΦΠυΣEυ∧μ¬﹪ι×λ§υ⊖μΠυ

Try it online! Link is to verbose version of code.

46-byte faster version is only O(lcm(1..n+2)) so can do up to n=10 on TIO:

FN⊞υ×⁺²ι⁺³ι≔⁰η≔⁰ζW∨¬η⌈Eυ﹪ηκ«≦⊕η≧⁺⌈Eυ¬﹪ηκζ»I∕ζη

Try it online! Link is to verbose version of code.

45-byte faster version is only O(2ⁿ) so can do up to n=13 on TIO:

⊞υ±¹FEN×⁺²ι⁺³ιF⮌υ⊞υ±÷×ικ⌈Φ⊕ι∧λ¬∨﹪ιλ﹪κλIΣ∕¹✂υ¹

Try it online! Link is to verbose version of code.

54-byte fastest version uses more efficient LCM so can do up to n=18 on TIO:

⊞υ±¹FEN×⁺²ι⁺³ιFEυ⟦κι⟧«W⊟κ⊞⊞Oκλ﹪§κ±²λ⊞υ±÷Π…κ²⊟κ»IΣ∕¹✂υ¹

Try it online! Link is to verbose version of code.

Python 2, 78 bytes

lambda n:sum(any(-~i%(j*-~j)<1for j in range(2,n+2))for i in range(10**7))/1e7

Try it online!

Returns the approximate decimal to +5 digits; uses the naive brute force approach xnor suggests in comments on the question.

Jelly,  14 13  10 bytes

-1 using Erik the Outgolfer's idea to take the mean of a list of zeros and ones.
-3 by using 3-indexing (as allowed in the question) - thanks to Dennis for pointing this out.

ḊPƝḍⱮ!Ẹ€Æm

A monadic Link accepting an integer, n+2, which yields a float.

Try it online! (very inefficient since it tests divisibility over the range \$[2, (n+2)!]\$)

(Started out as +2µḊPƝḍⱮ!§T,$Ẉ, taking n and yielding [numerator, denominator], unreduced)

How?

ḊPƝḍⱮ!Ẹ€Æm - Link: integer, x=n+2
Ḋ          - dequeue (implicit range of) x  - i.e. [2,3,4,...,n+2]
  Ɲ        - apply to neighbours:
 P         -   product                             [2×3,3×4,...,(n+1)×(n+2)]
     !     - factorial of x                        x!
    Ɱ      - map across (implicit range of) x! with:
   ḍ       -   divides?                            [[2×3ḍ1,3×4ḍ1,...,(n+1)×(n+2)ḍ1],[2×3ḍ2,3×4ḍ2,...,(n+1)×(n+2)ḍ2],...,[2×3ḍ(x!),3×4ḍ(x!),...,(n+1)×(n+2)ḍ(x!)]]
       €   - for each:
      Ẹ    -   any?  (1 if divisible by any of the neighbour products else 0)
        Æm - mean

Jelly, 12 bytes

Ḋב$ḍẸ¥ⱮP$Æm

Try it online!

-2 thanks to Jonathan Allan's suggestion to replace the LCM with the product (i.e. the LCM multiplied by an integer).

Dennis noticed I can 2-index as well.

JavaScript (ES6),  94 92  90 bytes

Saved 2 bytes thanks to @Shaggy + 2 more bytes from there

Returns a decimal approximation.

n=>(x=2,g=a=>n--?g([...a,x*++x]):[...Array(1e6)].map((_,k)=>n+=a.some(d=>k%d<1))&&n/1e6)``

Try it online!


JavaScript (ES6), 131 bytes

A much longer solution that returns an exact result as a pair \$[numerator, denominator]\$.

f=(n,a=[],p=x=1)=>n?f(n-1,[...a,q=++x*-~x],p*q/(g=(a,b)=>a?g(b%a,a):b)(p,q)):[...Array(p)].map((_,k)=>n+=a.some(d=>-~k%d<1))&&[n,p]

Try it online!

05AB1E, 15 bytes

Ì©!Lε®LüP¦Öà}ÅA

Port of @JonathanAllan's Jelly answer, so also extremely slow.

Try it online or verify the first three test cases.

Explanation:

Ì                 # Add 2 to the (implicit) input
                  #  i.e. 3 → 5
 ©                # Store this in the register (without popping)
  !               # Take the factorial of it
                  #  i.e. 5 → 120
   L              # Create a list in the range [1, (input+2)!]
                  #   i.e. 120 → [1,2,3,...,118,119,120]
    ε       }     #  Map over each value in this list
     ®            #  Push the input+2 from the register
      L           #  Create a list in the range [1, input+2]
                  #   i.e. 5 → [1,2,3,4,5]
       ü          #  Take each pair
                  #    i.e. [1,2,3,4,5] → [[1,2],[2,3],[3,4],[4,5]]
        P         #   And take the product of that pair
                  #    i.e. [[1,2],[2,3],[3,4],[4,5]] → [2,6,12,20]
         ¦        #  Then remove the first value from this product-pair list
                  #   i.e. [2,6,12,20] → [6,12,20]
          Ö       #  Check for each product-pair if it divides the current map-value
                  #  (1 if truthy; 0 if falsey)
                  #   i.e. [1,2,3,...,118,119,120] and [6,12,20]
                  #    → [[0,0,0],[0,0,0],[0,0,0],...,[0,0,0],[0,0,0],[1,1,1]]
           à      #  And check if it's truthy for any by taking the maximum
                  #   i.e. [[0,0,0],[0,0,0],[0,0,0],...,[0,0,0],[0,0,0],[1,1,1]]
                  #    → [0,0,0,...,0,0,1]
             ÅA   # After the map, take the mean (and output implicitly)
                  #  i.e. [0,0,0,...,0,0,1] → 0.2