Approximate the proportion of integers with neighboring factors
up vote
4
down vote
favorite
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 |
up vote
4
down vote
favorite
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
5 hours ago
@xnor I've now addressed that in the post.
– Doorknob♦
5 hours ago
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
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 5 hours ago
asked 5 hours ago
Doorknob♦
54k17112342
54k17112342
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
5 hours ago
@xnor I've now addressed that in the post.
– Doorknob♦
5 hours ago
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
5 hours ago
@xnor I've now addressed that in the post.
– Doorknob♦
5 hours ago
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
5 hours ago
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
5 hours ago
@xnor I've now addressed that in the post.
– Doorknob♦
5 hours ago
@xnor I've now addressed that in the post.
– Doorknob♦
5 hours ago
add a comment |
4 Answers
4
active
oldest
votes
up vote
1
down vote
Jelly, 15 bytes
‘€×‘$ḍẸ¥Ɱæl/$Æm
Try it online!
add a comment |
up vote
1
down vote
JavaScript (ES6), 94 92 90 bytes
Saved 2 bytes thanks to @Shaggy + 2 more bytes from there
Returns a float.
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
3 hours ago
add a comment |
up vote
0
down vote
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.
add a comment |
up vote
0
down vote
Jelly, 14 13 bytes
-1 using Erik the Outgolfer's idea to take the mean of a list of zeros and ones.
+2µḊPƝḍⱮ!Ẹ€Æm
A monadic Link accepting an integer which yields a float.
Try it online! (very inefficient since it tests divisibility over the range of $(n+2)!$)
(Previous was +2µḊPƝḍⱮ!§T,$Ẉ
yielding [numerator, denominator]
, unreduced)
How?
+2µḊPƝḍⱮ!Ẹ€Æm - Link: integer, n
+2 - add 2
µ - new monadic chain, call that x
Ḋ - dequeue (implicit range of) x
Ɲ - apply to neighbours:
P - product
! - factorial of x = x!
Ɱ - map across (implicit range of) x! with:
ḍ - divides?
Ẹ€ - any? for each (1 if divisible by any of the neighbour products else 0)
Æm - mean
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',
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
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Jelly, 15 bytes
‘€×‘$ḍẸ¥Ɱæl/$Æm
Try it online!
add a comment |
up vote
1
down vote
Jelly, 15 bytes
‘€×‘$ḍẸ¥Ɱæl/$Æm
Try it online!
add a comment |
up vote
1
down vote
up vote
1
down vote
Jelly, 15 bytes
‘€×‘$ḍẸ¥Ɱæl/$Æm
Try it online!
Jelly, 15 bytes
‘€×‘$ḍẸ¥Ɱæl/$Æm
Try it online!
answered 5 hours ago
Erik the Outgolfer
31.2k429102
31.2k429102
add a comment |
add a comment |
up vote
1
down vote
JavaScript (ES6), 94 92 90 bytes
Saved 2 bytes thanks to @Shaggy + 2 more bytes from there
Returns a float.
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
3 hours ago
add a comment |
up vote
1
down vote
JavaScript (ES6), 94 92 90 bytes
Saved 2 bytes thanks to @Shaggy + 2 more bytes from there
Returns a float.
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
3 hours ago
add a comment |
up vote
1
down vote
up vote
1
down vote
JavaScript (ES6), 94 92 90 bytes
Saved 2 bytes thanks to @Shaggy + 2 more bytes from there
Returns a float.
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 float.
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 1 hour ago
answered 3 hours ago
Arnauld
71.6k688299
71.6k688299
1
-2 bytes
– Shaggy
3 hours ago
add a comment |
1
-2 bytes
– Shaggy
3 hours ago
1
1
-2 bytes
– Shaggy
3 hours ago
-2 bytes
– Shaggy
3 hours ago
add a comment |
up vote
0
down vote
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.
add a comment |
up vote
0
down vote
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.
add a comment |
up vote
0
down vote
up vote
0
down vote
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.
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.
edited 52 mins ago
answered 1 hour ago
Neil
78.9k744175
78.9k744175
add a comment |
add a comment |
up vote
0
down vote
Jelly, 14 13 bytes
-1 using Erik the Outgolfer's idea to take the mean of a list of zeros and ones.
+2µḊPƝḍⱮ!Ẹ€Æm
A monadic Link accepting an integer which yields a float.
Try it online! (very inefficient since it tests divisibility over the range of $(n+2)!$)
(Previous was +2µḊPƝḍⱮ!§T,$Ẉ
yielding [numerator, denominator]
, unreduced)
How?
+2µḊPƝḍⱮ!Ẹ€Æm - Link: integer, n
+2 - add 2
µ - new monadic chain, call that x
Ḋ - dequeue (implicit range of) x
Ɲ - apply to neighbours:
P - product
! - factorial of x = x!
Ɱ - map across (implicit range of) x! with:
ḍ - divides?
Ẹ€ - any? for each (1 if divisible by any of the neighbour products else 0)
Æm - mean
add a comment |
up vote
0
down vote
Jelly, 14 13 bytes
-1 using Erik the Outgolfer's idea to take the mean of a list of zeros and ones.
+2µḊPƝḍⱮ!Ẹ€Æm
A monadic Link accepting an integer which yields a float.
Try it online! (very inefficient since it tests divisibility over the range of $(n+2)!$)
(Previous was +2µḊPƝḍⱮ!§T,$Ẉ
yielding [numerator, denominator]
, unreduced)
How?
+2µḊPƝḍⱮ!Ẹ€Æm - Link: integer, n
+2 - add 2
µ - new monadic chain, call that x
Ḋ - dequeue (implicit range of) x
Ɲ - apply to neighbours:
P - product
! - factorial of x = x!
Ɱ - map across (implicit range of) x! with:
ḍ - divides?
Ẹ€ - any? for each (1 if divisible by any of the neighbour products else 0)
Æm - mean
add a comment |
up vote
0
down vote
up vote
0
down vote
Jelly, 14 13 bytes
-1 using Erik the Outgolfer's idea to take the mean of a list of zeros and ones.
+2µḊPƝḍⱮ!Ẹ€Æm
A monadic Link accepting an integer which yields a float.
Try it online! (very inefficient since it tests divisibility over the range of $(n+2)!$)
(Previous was +2µḊPƝḍⱮ!§T,$Ẉ
yielding [numerator, denominator]
, unreduced)
How?
+2µḊPƝḍⱮ!Ẹ€Æm - Link: integer, n
+2 - add 2
µ - new monadic chain, call that x
Ḋ - dequeue (implicit range of) x
Ɲ - apply to neighbours:
P - product
! - factorial of x = x!
Ɱ - map across (implicit range of) x! with:
ḍ - divides?
Ẹ€ - any? for each (1 if divisible by any of the neighbour products else 0)
Æm - mean
Jelly, 14 13 bytes
-1 using Erik the Outgolfer's idea to take the mean of a list of zeros and ones.
+2µḊPƝḍⱮ!Ẹ€Æm
A monadic Link accepting an integer which yields a float.
Try it online! (very inefficient since it tests divisibility over the range of $(n+2)!$)
(Previous was +2µḊPƝḍⱮ!§T,$Ẉ
yielding [numerator, denominator]
, unreduced)
How?
+2µḊPƝḍⱮ!Ẹ€Æm - Link: integer, n
+2 - add 2
µ - new monadic chain, call that x
Ḋ - dequeue (implicit range of) x
Ɲ - apply to neighbours:
P - product
! - factorial of x = x!
Ɱ - map across (implicit range of) x! with:
ḍ - divides?
Ẹ€ - any? for each (1 if divisible by any of the neighbour products else 0)
Æm - mean
edited 43 mins ago
answered 1 hour ago
Jonathan Allan
50.6k534165
50.6k534165
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
5 hours ago
@xnor I've now addressed that in the post.
– Doorknob♦
5 hours ago