Approximate the proportion of integers with neighboring factors
If 1 is not counted as a factor, then
- 40 has two neighboring factors (4 and 5)
- 1092 has two neighboring factors (13 and 14)
- 350 does not have two neighboring factors
(out of its factors 2, 5, 7, 10, 14, 25, 35, 50, 70, and 175,
no two are consecutive)
The proportion of positive integers that have this property
is the proportion divisible by any of
6 (2 × 3), 12 (3 × 4), 20 (4 × 5), 30, 56, ….
If we only calculate the proportion divisible by the first n of these,
we get an approximation that gets more accurate as n increases.
For example,
for n=1,
we find the proportion of integers divisible by 2 × 3 = 6,
which is 1/6.
For n=2,
all integers divisible by 3 × 4 = 12 are also divisible by 6,
so the approximation is still 1/6.
For n=3,
the proportion of integers divisible by 6 or 20 is 1/5,
and so on.
Here are the first few values:
1 1/6 0.16666666666666666
3 1/5 0.20000000000000000
6 22/105 0.20952380952380953
9 491/2310 0.21255411255411255
12 2153/10010 0.21508491508491510
15 36887/170170 0.21676558735382265
21 65563/301070 0.21776663234463747
24 853883/3913910 0.21816623274423785
27 24796879/113503390 0.21846817967287144
For values of n between the provided values,
the output should be the same as the output for the value above
(e.g. n=5 → 1/5).
Your program should take n and output either a fraction or decimal answer.
You may take n at any offset
(e.g. 0-indexing or 2-indexing into this sequence, instead of 1-indexing).
For decimal output,
your program must be accurate to at least 5 digits
for all the test cases given.
Scoring is code-golf, with the shortest code winning.
Inspired by What proportion of positive integers have two factors that differ by 1? by marty cohen -- specifically, by Dan's answer.
code-golf number
add a comment |
If 1 is not counted as a factor, then
- 40 has two neighboring factors (4 and 5)
- 1092 has two neighboring factors (13 and 14)
- 350 does not have two neighboring factors
(out of its factors 2, 5, 7, 10, 14, 25, 35, 50, 70, and 175,
no two are consecutive)
The proportion of positive integers that have this property
is the proportion divisible by any of
6 (2 × 3), 12 (3 × 4), 20 (4 × 5), 30, 56, ….
If we only calculate the proportion divisible by the first n of these,
we get an approximation that gets more accurate as n increases.
For example,
for n=1,
we find the proportion of integers divisible by 2 × 3 = 6,
which is 1/6.
For n=2,
all integers divisible by 3 × 4 = 12 are also divisible by 6,
so the approximation is still 1/6.
For n=3,
the proportion of integers divisible by 6 or 20 is 1/5,
and so on.
Here are the first few values:
1 1/6 0.16666666666666666
3 1/5 0.20000000000000000
6 22/105 0.20952380952380953
9 491/2310 0.21255411255411255
12 2153/10010 0.21508491508491510
15 36887/170170 0.21676558735382265
21 65563/301070 0.21776663234463747
24 853883/3913910 0.21816623274423785
27 24796879/113503390 0.21846817967287144
For values of n between the provided values,
the output should be the same as the output for the value above
(e.g. n=5 → 1/5).
Your program should take n and output either a fraction or decimal answer.
You may take n at any offset
(e.g. 0-indexing or 2-indexing into this sequence, instead of 1-indexing).
For decimal output,
your program must be accurate to at least 5 digits
for all the test cases given.
Scoring is code-golf, with the shortest code winning.
Inspired by What proportion of positive integers have two factors that differ by 1? by marty cohen -- specifically, by Dan's answer.
code-golf number
1
How accurate does a decimal answer have to be? A natural strategy seems to be to count the integers with a valid divisor in some enormous range and divide by the length of the range, which gets better as an approximation as the range gets bigger.
– xnor
Dec 16 at 19:07
@xnor I've now addressed that in the post.
– Doorknob♦
Dec 16 at 19:45
add a comment |
If 1 is not counted as a factor, then
- 40 has two neighboring factors (4 and 5)
- 1092 has two neighboring factors (13 and 14)
- 350 does not have two neighboring factors
(out of its factors 2, 5, 7, 10, 14, 25, 35, 50, 70, and 175,
no two are consecutive)
The proportion of positive integers that have this property
is the proportion divisible by any of
6 (2 × 3), 12 (3 × 4), 20 (4 × 5), 30, 56, ….
If we only calculate the proportion divisible by the first n of these,
we get an approximation that gets more accurate as n increases.
For example,
for n=1,
we find the proportion of integers divisible by 2 × 3 = 6,
which is 1/6.
For n=2,
all integers divisible by 3 × 4 = 12 are also divisible by 6,
so the approximation is still 1/6.
For n=3,
the proportion of integers divisible by 6 or 20 is 1/5,
and so on.
Here are the first few values:
1 1/6 0.16666666666666666
3 1/5 0.20000000000000000
6 22/105 0.20952380952380953
9 491/2310 0.21255411255411255
12 2153/10010 0.21508491508491510
15 36887/170170 0.21676558735382265
21 65563/301070 0.21776663234463747
24 853883/3913910 0.21816623274423785
27 24796879/113503390 0.21846817967287144
For values of n between the provided values,
the output should be the same as the output for the value above
(e.g. n=5 → 1/5).
Your program should take n and output either a fraction or decimal answer.
You may take n at any offset
(e.g. 0-indexing or 2-indexing into this sequence, instead of 1-indexing).
For decimal output,
your program must be accurate to at least 5 digits
for all the test cases given.
Scoring is code-golf, with the shortest code winning.
Inspired by What proportion of positive integers have two factors that differ by 1? by marty cohen -- specifically, by Dan's answer.
code-golf number
If 1 is not counted as a factor, then
- 40 has two neighboring factors (4 and 5)
- 1092 has two neighboring factors (13 and 14)
- 350 does not have two neighboring factors
(out of its factors 2, 5, 7, 10, 14, 25, 35, 50, 70, and 175,
no two are consecutive)
The proportion of positive integers that have this property
is the proportion divisible by any of
6 (2 × 3), 12 (3 × 4), 20 (4 × 5), 30, 56, ….
If we only calculate the proportion divisible by the first n of these,
we get an approximation that gets more accurate as n increases.
For example,
for n=1,
we find the proportion of integers divisible by 2 × 3 = 6,
which is 1/6.
For n=2,
all integers divisible by 3 × 4 = 12 are also divisible by 6,
so the approximation is still 1/6.
For n=3,
the proportion of integers divisible by 6 or 20 is 1/5,
and so on.
Here are the first few values:
1 1/6 0.16666666666666666
3 1/5 0.20000000000000000
6 22/105 0.20952380952380953
9 491/2310 0.21255411255411255
12 2153/10010 0.21508491508491510
15 36887/170170 0.21676558735382265
21 65563/301070 0.21776663234463747
24 853883/3913910 0.21816623274423785
27 24796879/113503390 0.21846817967287144
For values of n between the provided values,
the output should be the same as the output for the value above
(e.g. n=5 → 1/5).
Your program should take n and output either a fraction or decimal answer.
You may take n at any offset
(e.g. 0-indexing or 2-indexing into this sequence, instead of 1-indexing).
For decimal output,
your program must be accurate to at least 5 digits
for all the test cases given.
Scoring is code-golf, with the shortest code winning.
Inspired by What proportion of positive integers have two factors that differ by 1? by marty cohen -- specifically, by Dan's answer.
code-golf number
code-golf number
edited Dec 16 at 19:45
asked Dec 16 at 18:54
Doorknob♦
54.3k17113345
54.3k17113345
1
How accurate does a decimal answer have to be? A natural strategy seems to be to count the integers with a valid divisor in some enormous range and divide by the length of the range, which gets better as an approximation as the range gets bigger.
– xnor
Dec 16 at 19:07
@xnor I've now addressed that in the post.
– Doorknob♦
Dec 16 at 19:45
add a comment |
1
How accurate does a decimal answer have to be? A natural strategy seems to be to count the integers with a valid divisor in some enormous range and divide by the length of the range, which gets better as an approximation as the range gets bigger.
– xnor
Dec 16 at 19:07
@xnor I've now addressed that in the post.
– Doorknob♦
Dec 16 at 19:45
1
1
How accurate does a decimal answer have to be? A natural strategy seems to be to count the integers with a valid divisor in some enormous range and divide by the length of the range, which gets better as an approximation as the range gets bigger.
– xnor
Dec 16 at 19:07
How accurate does a decimal answer have to be? A natural strategy seems to be to count the integers with a valid divisor in some enormous range and divide by the length of the range, which gets better as an approximation as the range gets bigger.
– xnor
Dec 16 at 19:07
@xnor I've now addressed that in the post.
– Doorknob♦
Dec 16 at 19:45
@xnor I've now addressed that in the post.
– Doorknob♦
Dec 16 at 19:45
add a comment |
7 Answers
7
active
oldest
votes
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
Hm... I suspect what makes this shorter than mine is the use of!
instead ofæl/
... Ah, the joys of rules changing while sleeping.
– Erik the Outgolfer
Dec 17 at 12:41
@EriktheOutgolfer yeah, very similar methods when I look closer! can you useP
to get down to 13?
– Jonathan Allan
Dec 17 at 13:08
Instead ofẸ€
? I'm afraidP
is the same as׃1$
, so it won't work. (And that would be 14 anyway...) If instead ofæl/
, maybe (P
is LCM*k after all).
– Erik the Outgolfer
Dec 17 at 13:09
@EriktheOutgolfer instead ofæl/
– Jonathan Allan
Dec 17 at 13:10
Yeah, I think I can do that, and the result would theoretically be as precise as withæl/
I guess. (Night-owl golfing does have issues...) EDIT: Yeah, although I'll have to reduce the argument over TIO to4
... :P
– Erik the Outgolfer
Dec 17 at 13:12
|
show 2 more comments
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
add a comment |
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!
1
-2 bytes
– Shaggy
Dec 16 at 21:49
This should work, in theory for 82 bytes.
– Shaggy
Dec 17 at 10:25
@Shaggy I don't really know what the consensus is for answers like that. While it does work in theory, it doesn't work in practice for any input. (I personally dislike this kind of answers. This is why I usually include a rule such as "your code should at least work up to a given limit" in my own challenges when I suspect that I'll get answers such as "works only for n=1 on TIO" ... or doesn't work at all in the present case.)
– Arnauld
Dec 17 at 10:34
Personally, I'm a big fan of the infinite time & memory consensus ;)
– Shaggy
Dec 17 at 11:47
Oh I like it too. :) My only reservation is that I think it should be possible to test any answer for at least a couple of distinct inputs.
– Arnauld
Dec 17 at 12:14
|
show 1 more comment
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.
add a comment |
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.
add a comment |
Wolfram Language (Mathematica), 69 68 61 52 bytes
Count[Range[#!],b_/;Or@@(# #-#&@Range[3,#]∣b)]/#!&
Try it online!
3-indexed. At first I was going to use LCM@@
but realized that #!
would be shorter... but now it a lot of memory for Range[#!]
...
Managed to golf down the condition by 2 bytes, which was nice.
Older numerical solution (56 bytes):
N@Count[Range[5^8],b_/;Or@@Array[(# #-#)∣b&,#,3]]/5^8&
Try it online!
2-indexed. More efficient when #!>5^8
(#>9
, assuming #
is an integer).
add a comment |
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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "200"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f177668%2fapproximate-the-proportion-of-integers-with-neighboring-factors%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
Hm... I suspect what makes this shorter than mine is the use of!
instead ofæl/
... Ah, the joys of rules changing while sleeping.
– Erik the Outgolfer
Dec 17 at 12:41
@EriktheOutgolfer yeah, very similar methods when I look closer! can you useP
to get down to 13?
– Jonathan Allan
Dec 17 at 13:08
Instead ofẸ€
? I'm afraidP
is the same as׃1$
, so it won't work. (And that would be 14 anyway...) If instead ofæl/
, maybe (P
is LCM*k after all).
– Erik the Outgolfer
Dec 17 at 13:09
@EriktheOutgolfer instead ofæl/
– Jonathan Allan
Dec 17 at 13:10
Yeah, I think I can do that, and the result would theoretically be as precise as withæl/
I guess. (Night-owl golfing does have issues...) EDIT: Yeah, although I'll have to reduce the argument over TIO to4
... :P
– Erik the Outgolfer
Dec 17 at 13:12
|
show 2 more comments
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
Hm... I suspect what makes this shorter than mine is the use of!
instead ofæl/
... Ah, the joys of rules changing while sleeping.
– Erik the Outgolfer
Dec 17 at 12:41
@EriktheOutgolfer yeah, very similar methods when I look closer! can you useP
to get down to 13?
– Jonathan Allan
Dec 17 at 13:08
Instead ofẸ€
? I'm afraidP
is the same as׃1$
, so it won't work. (And that would be 14 anyway...) If instead ofæl/
, maybe (P
is LCM*k after all).
– Erik the Outgolfer
Dec 17 at 13:09
@EriktheOutgolfer instead ofæl/
– Jonathan Allan
Dec 17 at 13:10
Yeah, I think I can do that, and the result would theoretically be as precise as withæl/
I guess. (Night-owl golfing does have issues...) EDIT: Yeah, although I'll have to reduce the argument over TIO to4
... :P
– Erik the Outgolfer
Dec 17 at 13:12
|
show 2 more comments
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, 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
edited Dec 17 at 18:06
answered Dec 16 at 23:49
Jonathan Allan
50.7k534165
50.7k534165
Hm... I suspect what makes this shorter than mine is the use of!
instead ofæl/
... Ah, the joys of rules changing while sleeping.
– Erik the Outgolfer
Dec 17 at 12:41
@EriktheOutgolfer yeah, very similar methods when I look closer! can you useP
to get down to 13?
– Jonathan Allan
Dec 17 at 13:08
Instead ofẸ€
? I'm afraidP
is the same as׃1$
, so it won't work. (And that would be 14 anyway...) If instead ofæl/
, maybe (P
is LCM*k after all).
– Erik the Outgolfer
Dec 17 at 13:09
@EriktheOutgolfer instead ofæl/
– Jonathan Allan
Dec 17 at 13:10
Yeah, I think I can do that, and the result would theoretically be as precise as withæl/
I guess. (Night-owl golfing does have issues...) EDIT: Yeah, although I'll have to reduce the argument over TIO to4
... :P
– Erik the Outgolfer
Dec 17 at 13:12
|
show 2 more comments
Hm... I suspect what makes this shorter than mine is the use of!
instead ofæl/
... Ah, the joys of rules changing while sleeping.
– Erik the Outgolfer
Dec 17 at 12:41
@EriktheOutgolfer yeah, very similar methods when I look closer! can you useP
to get down to 13?
– Jonathan Allan
Dec 17 at 13:08
Instead ofẸ€
? I'm afraidP
is the same as׃1$
, so it won't work. (And that would be 14 anyway...) If instead ofæl/
, maybe (P
is LCM*k after all).
– Erik the Outgolfer
Dec 17 at 13:09
@EriktheOutgolfer instead ofæl/
– Jonathan Allan
Dec 17 at 13:10
Yeah, I think I can do that, and the result would theoretically be as precise as withæl/
I guess. (Night-owl golfing does have issues...) EDIT: Yeah, although I'll have to reduce the argument over TIO to4
... :P
– Erik the Outgolfer
Dec 17 at 13:12
Hm... I suspect what makes this shorter than mine is the use of
!
instead of æl/
... Ah, the joys of rules changing while sleeping.– Erik the Outgolfer
Dec 17 at 12:41
Hm... I suspect what makes this shorter than mine is the use of
!
instead of æl/
... Ah, the joys of rules changing while sleeping.– Erik the Outgolfer
Dec 17 at 12:41
@EriktheOutgolfer yeah, very similar methods when I look closer! can you use
P
to get down to 13?– Jonathan Allan
Dec 17 at 13:08
@EriktheOutgolfer yeah, very similar methods when I look closer! can you use
P
to get down to 13?– Jonathan Allan
Dec 17 at 13:08
Instead of
Ẹ€
? I'm afraid P
is the same as ׃1$
, so it won't work. (And that would be 14 anyway...) If instead of æl/
, maybe (P
is LCM*k after all).– Erik the Outgolfer
Dec 17 at 13:09
Instead of
Ẹ€
? I'm afraid P
is the same as ׃1$
, so it won't work. (And that would be 14 anyway...) If instead of æl/
, maybe (P
is LCM*k after all).– Erik the Outgolfer
Dec 17 at 13:09
@EriktheOutgolfer instead of
æl/
– Jonathan Allan
Dec 17 at 13:10
@EriktheOutgolfer instead of
æl/
– Jonathan Allan
Dec 17 at 13:10
Yeah, I think I can do that, and the result would theoretically be as precise as with
æl/
I guess. (Night-owl golfing does have issues...) EDIT: Yeah, although I'll have to reduce the argument over TIO to 4
... :P– Erik the Outgolfer
Dec 17 at 13:12
Yeah, I think I can do that, and the result would theoretically be as precise as with
æl/
I guess. (Night-owl golfing does have issues...) EDIT: Yeah, although I'll have to reduce the argument over TIO to 4
... :P– Erik the Outgolfer
Dec 17 at 13:12
|
show 2 more comments
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
add a comment |
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
add a comment |
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
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
answered Dec 17 at 10:19
Kevin Cruijssen
35.6k554186
35.6k554186
add a comment |
add a comment |
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!
1
-2 bytes
– Shaggy
Dec 16 at 21:49
This should work, in theory for 82 bytes.
– Shaggy
Dec 17 at 10:25
@Shaggy I don't really know what the consensus is for answers like that. While it does work in theory, it doesn't work in practice for any input. (I personally dislike this kind of answers. This is why I usually include a rule such as "your code should at least work up to a given limit" in my own challenges when I suspect that I'll get answers such as "works only for n=1 on TIO" ... or doesn't work at all in the present case.)
– Arnauld
Dec 17 at 10:34
Personally, I'm a big fan of the infinite time & memory consensus ;)
– Shaggy
Dec 17 at 11:47
Oh I like it too. :) My only reservation is that I think it should be possible to test any answer for at least a couple of distinct inputs.
– Arnauld
Dec 17 at 12:14
|
show 1 more comment
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!
1
-2 bytes
– Shaggy
Dec 16 at 21:49
This should work, in theory for 82 bytes.
– Shaggy
Dec 17 at 10:25
@Shaggy I don't really know what the consensus is for answers like that. While it does work in theory, it doesn't work in practice for any input. (I personally dislike this kind of answers. This is why I usually include a rule such as "your code should at least work up to a given limit" in my own challenges when I suspect that I'll get answers such as "works only for n=1 on TIO" ... or doesn't work at all in the present case.)
– Arnauld
Dec 17 at 10:34
Personally, I'm a big fan of the infinite time & memory consensus ;)
– Shaggy
Dec 17 at 11:47
Oh I like it too. :) My only reservation is that I think it should be possible to test any answer for at least a couple of distinct inputs.
– Arnauld
Dec 17 at 12:14
|
show 1 more comment
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!
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!
edited Dec 17 at 10:22
answered Dec 16 at 21:36
Arnauld
72.4k689304
72.4k689304
1
-2 bytes
– Shaggy
Dec 16 at 21:49
This should work, in theory for 82 bytes.
– Shaggy
Dec 17 at 10:25
@Shaggy I don't really know what the consensus is for answers like that. While it does work in theory, it doesn't work in practice for any input. (I personally dislike this kind of answers. This is why I usually include a rule such as "your code should at least work up to a given limit" in my own challenges when I suspect that I'll get answers such as "works only for n=1 on TIO" ... or doesn't work at all in the present case.)
– Arnauld
Dec 17 at 10:34
Personally, I'm a big fan of the infinite time & memory consensus ;)
– Shaggy
Dec 17 at 11:47
Oh I like it too. :) My only reservation is that I think it should be possible to test any answer for at least a couple of distinct inputs.
– Arnauld
Dec 17 at 12:14
|
show 1 more comment
1
-2 bytes
– Shaggy
Dec 16 at 21:49
This should work, in theory for 82 bytes.
– Shaggy
Dec 17 at 10:25
@Shaggy I don't really know what the consensus is for answers like that. While it does work in theory, it doesn't work in practice for any input. (I personally dislike this kind of answers. This is why I usually include a rule such as "your code should at least work up to a given limit" in my own challenges when I suspect that I'll get answers such as "works only for n=1 on TIO" ... or doesn't work at all in the present case.)
– Arnauld
Dec 17 at 10:34
Personally, I'm a big fan of the infinite time & memory consensus ;)
– Shaggy
Dec 17 at 11:47
Oh I like it too. :) My only reservation is that I think it should be possible to test any answer for at least a couple of distinct inputs.
– Arnauld
Dec 17 at 12:14
1
1
-2 bytes
– Shaggy
Dec 16 at 21:49
-2 bytes
– Shaggy
Dec 16 at 21:49
This should work, in theory for 82 bytes.
– Shaggy
Dec 17 at 10:25
This should work, in theory for 82 bytes.
– Shaggy
Dec 17 at 10:25
@Shaggy I don't really know what the consensus is for answers like that. While it does work in theory, it doesn't work in practice for any input. (I personally dislike this kind of answers. This is why I usually include a rule such as "your code should at least work up to a given limit" in my own challenges when I suspect that I'll get answers such as "works only for n=1 on TIO" ... or doesn't work at all in the present case.)
– Arnauld
Dec 17 at 10:34
@Shaggy I don't really know what the consensus is for answers like that. While it does work in theory, it doesn't work in practice for any input. (I personally dislike this kind of answers. This is why I usually include a rule such as "your code should at least work up to a given limit" in my own challenges when I suspect that I'll get answers such as "works only for n=1 on TIO" ... or doesn't work at all in the present case.)
– Arnauld
Dec 17 at 10:34
Personally, I'm a big fan of the infinite time & memory consensus ;)
– Shaggy
Dec 17 at 11:47
Personally, I'm a big fan of the infinite time & memory consensus ;)
– Shaggy
Dec 17 at 11:47
Oh I like it too. :) My only reservation is that I think it should be possible to test any answer for at least a couple of distinct inputs.
– Arnauld
Dec 17 at 12:14
Oh I like it too. :) My only reservation is that I think it should be possible to test any answer for at least a couple of distinct inputs.
– Arnauld
Dec 17 at 12:14
|
show 1 more comment
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.
add a comment |
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.
add a comment |
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.
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.
edited Dec 17 at 14:09
answered Dec 16 at 19:33
Erik the Outgolfer
31.3k429103
31.3k429103
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Dec 23 at 0:21
answered Dec 16 at 23:48
Neil
79.3k744177
79.3k744177
add a comment |
add a comment |
Wolfram Language (Mathematica), 69 68 61 52 bytes
Count[Range[#!],b_/;Or@@(# #-#&@Range[3,#]∣b)]/#!&
Try it online!
3-indexed. At first I was going to use LCM@@
but realized that #!
would be shorter... but now it a lot of memory for Range[#!]
...
Managed to golf down the condition by 2 bytes, which was nice.
Older numerical solution (56 bytes):
N@Count[Range[5^8],b_/;Or@@Array[(# #-#)∣b&,#,3]]/5^8&
Try it online!
2-indexed. More efficient when #!>5^8
(#>9
, assuming #
is an integer).
add a comment |
Wolfram Language (Mathematica), 69 68 61 52 bytes
Count[Range[#!],b_/;Or@@(# #-#&@Range[3,#]∣b)]/#!&
Try it online!
3-indexed. At first I was going to use LCM@@
but realized that #!
would be shorter... but now it a lot of memory for Range[#!]
...
Managed to golf down the condition by 2 bytes, which was nice.
Older numerical solution (56 bytes):
N@Count[Range[5^8],b_/;Or@@Array[(# #-#)∣b&,#,3]]/5^8&
Try it online!
2-indexed. More efficient when #!>5^8
(#>9
, assuming #
is an integer).
add a comment |
Wolfram Language (Mathematica), 69 68 61 52 bytes
Count[Range[#!],b_/;Or@@(# #-#&@Range[3,#]∣b)]/#!&
Try it online!
3-indexed. At first I was going to use LCM@@
but realized that #!
would be shorter... but now it a lot of memory for Range[#!]
...
Managed to golf down the condition by 2 bytes, which was nice.
Older numerical solution (56 bytes):
N@Count[Range[5^8],b_/;Or@@Array[(# #-#)∣b&,#,3]]/5^8&
Try it online!
2-indexed. More efficient when #!>5^8
(#>9
, assuming #
is an integer).
Wolfram Language (Mathematica), 69 68 61 52 bytes
Count[Range[#!],b_/;Or@@(# #-#&@Range[3,#]∣b)]/#!&
Try it online!
3-indexed. At first I was going to use LCM@@
but realized that #!
would be shorter... but now it a lot of memory for Range[#!]
...
Managed to golf down the condition by 2 bytes, which was nice.
Older numerical solution (56 bytes):
N@Count[Range[5^8],b_/;Or@@Array[(# #-#)∣b&,#,3]]/5^8&
Try it online!
2-indexed. More efficient when #!>5^8
(#>9
, assuming #
is an integer).
edited Dec 23 at 8:16
answered Dec 17 at 6:18
attinat
1014
1014
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Dec 18 at 3:46
Chas Brown
4,8081522
4,8081522
add a comment |
add a comment |
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).
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f177668%2fapproximate-the-proportion-of-integers-with-neighboring-factors%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
How accurate does a decimal answer have to be? A natural strategy seems to be to count the integers with a valid divisor in some enormous range and divide by the length of the range, which gets better as an approximation as the range gets bigger.
– xnor
Dec 16 at 19:07
@xnor I've now addressed that in the post.
– Doorknob♦
Dec 16 at 19:45