Terms of the EKG sequence
$begingroup$
Introduction
The EKG sequence begins with 1 and 2, then the rule is that the next term is the smallest positive integer not already in the sequence and whose common factor with the last term is greater than 1 (they are not coprimes).
The first terms are:
1, 2, 4, 6, 3, 9, 12, 8, 10, 5, 15, ...
It's called EKG because the graph of its terms is quite similar to an EKG.
It's sequence A064413 in the OEIS.
Challenge
You have to write a function which takes an integer n as input and outputs how many of the n first terms of the sequence are greater than n.
As the sequence's rule begins with the third term, the input integer has to be greater or equal to 3. For example, given input 10
the output is 1
because the 7th term is 12
and none of the other first ten terms exceed 10.
Test cases
3 -> 1
10 -> 1
100 -> 9
1000 -> 70
Rules
- For integers lower than 3, the function may output 0 or an error code.
- No other particular rules except: it's code golf, the shorter the better!
code-golf sequence
$endgroup$
add a comment |
$begingroup$
Introduction
The EKG sequence begins with 1 and 2, then the rule is that the next term is the smallest positive integer not already in the sequence and whose common factor with the last term is greater than 1 (they are not coprimes).
The first terms are:
1, 2, 4, 6, 3, 9, 12, 8, 10, 5, 15, ...
It's called EKG because the graph of its terms is quite similar to an EKG.
It's sequence A064413 in the OEIS.
Challenge
You have to write a function which takes an integer n as input and outputs how many of the n first terms of the sequence are greater than n.
As the sequence's rule begins with the third term, the input integer has to be greater or equal to 3. For example, given input 10
the output is 1
because the 7th term is 12
and none of the other first ten terms exceed 10.
Test cases
3 -> 1
10 -> 1
100 -> 9
1000 -> 70
Rules
- For integers lower than 3, the function may output 0 or an error code.
- No other particular rules except: it's code golf, the shorter the better!
code-golf sequence
$endgroup$
$begingroup$
Can we use 0-indexing, with1
being the 0th term of the sequence and therefor making, for example,15
the 10th term, rather than5
?
$endgroup$
– Shaggy
Nov 13 '18 at 16:48
$begingroup$
@Shaggy I think it's fair to use this as a mathematical way, but actually it will change the result of test cases and indeed the asked function in itself. Thus I think you should'nt be allowed to do so. Sorry.
$endgroup$
– david
Nov 13 '18 at 16:59
add a comment |
$begingroup$
Introduction
The EKG sequence begins with 1 and 2, then the rule is that the next term is the smallest positive integer not already in the sequence and whose common factor with the last term is greater than 1 (they are not coprimes).
The first terms are:
1, 2, 4, 6, 3, 9, 12, 8, 10, 5, 15, ...
It's called EKG because the graph of its terms is quite similar to an EKG.
It's sequence A064413 in the OEIS.
Challenge
You have to write a function which takes an integer n as input and outputs how many of the n first terms of the sequence are greater than n.
As the sequence's rule begins with the third term, the input integer has to be greater or equal to 3. For example, given input 10
the output is 1
because the 7th term is 12
and none of the other first ten terms exceed 10.
Test cases
3 -> 1
10 -> 1
100 -> 9
1000 -> 70
Rules
- For integers lower than 3, the function may output 0 or an error code.
- No other particular rules except: it's code golf, the shorter the better!
code-golf sequence
$endgroup$
Introduction
The EKG sequence begins with 1 and 2, then the rule is that the next term is the smallest positive integer not already in the sequence and whose common factor with the last term is greater than 1 (they are not coprimes).
The first terms are:
1, 2, 4, 6, 3, 9, 12, 8, 10, 5, 15, ...
It's called EKG because the graph of its terms is quite similar to an EKG.
It's sequence A064413 in the OEIS.
Challenge
You have to write a function which takes an integer n as input and outputs how many of the n first terms of the sequence are greater than n.
As the sequence's rule begins with the third term, the input integer has to be greater or equal to 3. For example, given input 10
the output is 1
because the 7th term is 12
and none of the other first ten terms exceed 10.
Test cases
3 -> 1
10 -> 1
100 -> 9
1000 -> 70
Rules
- For integers lower than 3, the function may output 0 or an error code.
- No other particular rules except: it's code golf, the shorter the better!
code-golf sequence
code-golf sequence
edited Nov 13 '18 at 19:41
Solomon Ucko
268110
268110
asked Nov 13 '18 at 13:46
daviddavid
199111
199111
$begingroup$
Can we use 0-indexing, with1
being the 0th term of the sequence and therefor making, for example,15
the 10th term, rather than5
?
$endgroup$
– Shaggy
Nov 13 '18 at 16:48
$begingroup$
@Shaggy I think it's fair to use this as a mathematical way, but actually it will change the result of test cases and indeed the asked function in itself. Thus I think you should'nt be allowed to do so. Sorry.
$endgroup$
– david
Nov 13 '18 at 16:59
add a comment |
$begingroup$
Can we use 0-indexing, with1
being the 0th term of the sequence and therefor making, for example,15
the 10th term, rather than5
?
$endgroup$
– Shaggy
Nov 13 '18 at 16:48
$begingroup$
@Shaggy I think it's fair to use this as a mathematical way, but actually it will change the result of test cases and indeed the asked function in itself. Thus I think you should'nt be allowed to do so. Sorry.
$endgroup$
– david
Nov 13 '18 at 16:59
$begingroup$
Can we use 0-indexing, with
1
being the 0th term of the sequence and therefor making, for example, 15
the 10th term, rather than 5
?$endgroup$
– Shaggy
Nov 13 '18 at 16:48
$begingroup$
Can we use 0-indexing, with
1
being the 0th term of the sequence and therefor making, for example, 15
the 10th term, rather than 5
?$endgroup$
– Shaggy
Nov 13 '18 at 16:48
$begingroup$
@Shaggy I think it's fair to use this as a mathematical way, but actually it will change the result of test cases and indeed the asked function in itself. Thus I think you should'nt be allowed to do so. Sorry.
$endgroup$
– david
Nov 13 '18 at 16:59
$begingroup$
@Shaggy I think it's fair to use this as a mathematical way, but actually it will change the result of test cases and indeed the asked function in itself. Thus I think you should'nt be allowed to do so. Sorry.
$endgroup$
– david
Nov 13 '18 at 16:59
add a comment |
9 Answers
9
active
oldest
votes
$begingroup$
Jelly, 20 19 18 bytes
S‘gṪ’ɗƇḟ¹Ṃṭ
1Ç¡>¹S
This is a full program.
Try it online!
How it works
1Ç¡>¹S Main link. Argument: n (integer)
1 Set the return value to 1.
Ç¡ Call the helper link n times.
>¹ Compare the elements of the result with n.
S Take the sum, counting elements larger than n.
S‘gṪ’ɗƇḟ¹Ṃṭ Helper link. Argument: A (array or 1)
S Take the sum of A.
‘ Increment; add 1.
ɗƇ Drei comb; keep only elements k of [1, ..., sum(A)+1] for which the
three links to the left return a truthy value.
g Take the GCD of k and all elements of A.
Ṫ Tail; extract the last GCD.
’ Decrement the result, mapping 1 to 0.
ḟ¹ Filterfalse; remove the elements that occur in A.
Ṃ Take the minimum.
ṭ Tack; append the minimum to A.
Note that the generated sequence is $[1, mathbf0, 2, 4, 6, 3, 9, 12, 8, 10, 5, 15, dots]$. Since calling the helper link $n$ times generates a sequence of length $n + 1$, the $0$ is practically ignored.
$endgroup$
add a comment |
$begingroup$
Perl 6, 66 bytes
+grep *>$_,(1,2,first *gcd@_[*-1]>1,grep *∉@_,1..*...*)[^$_]
Try it online!
Too slow on TIO for n = 1000.
$endgroup$
add a comment |
$begingroup$
JavaScript (ES6), 107 106 105 bytes
f=(n,a=[2,1],k=3)=>a[n-1]?0:a.indexOf(k)+(C=(a,b)=>b?C(b,a%b):a>1)(k,a[0])?f(n,a,k+1):(k>n)+f(n,[k,...a])
Try it online!
How?
The helper function $C$ returns true if two given integers are not coprime:
C = (a, b) => b ? C(b, a % b) : a > 1
The array $a$ is initialized to $[2,1]$ and holds all values that were encountered so far in reverse order. Therefore, the last value is always $a[0]$.
To know if $k$ qualifies as the next term of the sequence, we test whether the following expression is equal to $0$:
a.indexOf(k) + C(k, a[0])
a.indexOf(k)
is equal to either:
$-1$ if $k$ is not found in $a$
$0$ if $k$ is equal to the last value (in which case it's necessarily not coprime with it)- some $ige 1$ otherwise
Therefore, a.indexOf(k) + C(k, a[0])
is equal to $0$ if and only if $k$ is not found in $a$ and $k$ is not coprime with the last value ($-1+true=0$).
$endgroup$
add a comment |
$begingroup$
Haskell, 89 82 bytes
Edit: -7 bytes thanks to @H.PWiz
f l=[n|n<-[3..],all(/=n)l,gcd(l!!0)n>1]!!0:l
g n=sum[1|x<-iterate f[2]!!(n-2),x>n]
Try it online!
$endgroup$
$begingroup$
82 bytes
$endgroup$
– H.PWiz
Nov 13 '18 at 17:41
$begingroup$
@H.PWiz: ah, that's clever. Thanks!
$endgroup$
– nimi
Nov 13 '18 at 17:50
add a comment |
$begingroup$
Husk, 16 bytes
#>¹↑¡§ḟȯ←⌋→`-Nḣ2
Try it online!
Explanation
#>¹↑¡§ḟȯ←⌋→`-Nḣ2 Implicit input, say n=10
ḣ2 Range to 2: [1,2]
¡ Construct an infinite list, adding new elements using this function:
Argument is list of numbers found so far, say L=[1,2,4]
N Natural numbers: [1,2,3,4,5,6,7...
`- Remove elements of L: K=[3,5,6,7...
ḟ Find first element of K that satisfies this:
Argument is a number in K, say 6
§ → Last element of L: 4
⌋ GCD: 2
ȯ← Decrement: 1
Implicitly: is it nonzero? Yes, so 6 is good.
Result is the EKG sequence: [1,2,4,6,3,9,12...
↑ Take the first n elements: [1,2,4,6,3,9,12,8,10,5]
# Count the number of those
>¹ that are larger than n: 1
$endgroup$
add a comment |
$begingroup$
MATL, 29 bytes
qq:2:w"GE:yX-y0)yZdqg)1)h]G>z
Try it online!
Explanation:
#implicit input, n, say 10
qq: #push 1:8
2: #push [1 2]. Stack: [1 .. 8], [1 2]
w #swap top two elements on stack
" #begin for loop (do the following n-2 times):
GE: #push 1...20. Stack: [1 2], [1..20]
y #copy from below. Stack:[1 2], [1..20], [1 2]
X- #set difference. Stack: [1 2], [3..20]
y0) #copy last element from below. Stack:[1 2], [3..20], 2
yZd #copy from below and elementwise GCD. Stack:[1 2], [3..20],[1,2,etc.]
qg) #select those with gcd greater than 1. Stack:[1 2], [4,6,etc.]
1) #take first. Stack:[1 2], 4
h #horizontally concatenate. Stack:[1 2 4]
] #end of for loop
G>z #count those greater than input
#implicit output of result
$endgroup$
$begingroup$
please can you explain why do you double the input (withGE:
)?
$endgroup$
– david
Nov 13 '18 at 20:56
1
$begingroup$
@david I think empirically I noticed that $a(n)leq 2n$ but I don't have a proof, so I originally used $a(n)leq n^2$, which timed out on the $n=1000$ test case, so I switched it back to test that, and left it in. I'm still not sure about either bound, but it seems to work, so I may try to come up with a proof. Awhile
loop would be much messier in MATL so I was trying to avoid it.
$endgroup$
– Giuseppe
Nov 13 '18 at 21:08
add a comment |
$begingroup$
JavaScript, 93 91 87 bytes
Throws an overflow error for 0
or 1
, outputs 0
for 2
.
n=>(g=x=>n-i?g[++x]|(h=(y,z)=>z?h(z,y%z):y)(x,c)<2?g(x):(g[c=x]=++i,x>n)+g(2):0)(i=c=2)
Try it online
$endgroup$
add a comment |
$begingroup$
Japt, 23 21 bytes
@_jX ªAøZ}f}gA=ì)Aè>U
Try it
@_jX ªAøZ}f}gA=ì)Aè>U
:Implicit input of integer U
A :10
ì :Digit array
= :Reassign to A
@ }g :While the length of A < U+1, take the last element as X,
:pass it through the following function & push the result to A
_ }f : Find the first integer Z >= 0 that returns falsey
jX : Is Z co-prime with X?
ª : OR
AøZ : Does A contain Z?
) :End loop
Aè>U :Count the elements in A that are greater than U
$endgroup$
add a comment |
$begingroup$
Python 3, 153 bytes
import math
def f(n):
l=[1];c=0
for _ in range(n-1):l+=[min(*range(2,n*4),key=lambda x:n*8if x in l or math.gcd(x,l[-1])<2else x)];c+=l[-1]>n
return c
Try it online! (Warning: Takes ~30 seconds to evaluate)
$endgroup$
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%2f175858%2fterms-of-the-ekg-sequence%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Jelly, 20 19 18 bytes
S‘gṪ’ɗƇḟ¹Ṃṭ
1Ç¡>¹S
This is a full program.
Try it online!
How it works
1Ç¡>¹S Main link. Argument: n (integer)
1 Set the return value to 1.
Ç¡ Call the helper link n times.
>¹ Compare the elements of the result with n.
S Take the sum, counting elements larger than n.
S‘gṪ’ɗƇḟ¹Ṃṭ Helper link. Argument: A (array or 1)
S Take the sum of A.
‘ Increment; add 1.
ɗƇ Drei comb; keep only elements k of [1, ..., sum(A)+1] for which the
three links to the left return a truthy value.
g Take the GCD of k and all elements of A.
Ṫ Tail; extract the last GCD.
’ Decrement the result, mapping 1 to 0.
ḟ¹ Filterfalse; remove the elements that occur in A.
Ṃ Take the minimum.
ṭ Tack; append the minimum to A.
Note that the generated sequence is $[1, mathbf0, 2, 4, 6, 3, 9, 12, 8, 10, 5, 15, dots]$. Since calling the helper link $n$ times generates a sequence of length $n + 1$, the $0$ is practically ignored.
$endgroup$
add a comment |
$begingroup$
Jelly, 20 19 18 bytes
S‘gṪ’ɗƇḟ¹Ṃṭ
1Ç¡>¹S
This is a full program.
Try it online!
How it works
1Ç¡>¹S Main link. Argument: n (integer)
1 Set the return value to 1.
Ç¡ Call the helper link n times.
>¹ Compare the elements of the result with n.
S Take the sum, counting elements larger than n.
S‘gṪ’ɗƇḟ¹Ṃṭ Helper link. Argument: A (array or 1)
S Take the sum of A.
‘ Increment; add 1.
ɗƇ Drei comb; keep only elements k of [1, ..., sum(A)+1] for which the
three links to the left return a truthy value.
g Take the GCD of k and all elements of A.
Ṫ Tail; extract the last GCD.
’ Decrement the result, mapping 1 to 0.
ḟ¹ Filterfalse; remove the elements that occur in A.
Ṃ Take the minimum.
ṭ Tack; append the minimum to A.
Note that the generated sequence is $[1, mathbf0, 2, 4, 6, 3, 9, 12, 8, 10, 5, 15, dots]$. Since calling the helper link $n$ times generates a sequence of length $n + 1$, the $0$ is practically ignored.
$endgroup$
add a comment |
$begingroup$
Jelly, 20 19 18 bytes
S‘gṪ’ɗƇḟ¹Ṃṭ
1Ç¡>¹S
This is a full program.
Try it online!
How it works
1Ç¡>¹S Main link. Argument: n (integer)
1 Set the return value to 1.
Ç¡ Call the helper link n times.
>¹ Compare the elements of the result with n.
S Take the sum, counting elements larger than n.
S‘gṪ’ɗƇḟ¹Ṃṭ Helper link. Argument: A (array or 1)
S Take the sum of A.
‘ Increment; add 1.
ɗƇ Drei comb; keep only elements k of [1, ..., sum(A)+1] for which the
three links to the left return a truthy value.
g Take the GCD of k and all elements of A.
Ṫ Tail; extract the last GCD.
’ Decrement the result, mapping 1 to 0.
ḟ¹ Filterfalse; remove the elements that occur in A.
Ṃ Take the minimum.
ṭ Tack; append the minimum to A.
Note that the generated sequence is $[1, mathbf0, 2, 4, 6, 3, 9, 12, 8, 10, 5, 15, dots]$. Since calling the helper link $n$ times generates a sequence of length $n + 1$, the $0$ is practically ignored.
$endgroup$
Jelly, 20 19 18 bytes
S‘gṪ’ɗƇḟ¹Ṃṭ
1Ç¡>¹S
This is a full program.
Try it online!
How it works
1Ç¡>¹S Main link. Argument: n (integer)
1 Set the return value to 1.
Ç¡ Call the helper link n times.
>¹ Compare the elements of the result with n.
S Take the sum, counting elements larger than n.
S‘gṪ’ɗƇḟ¹Ṃṭ Helper link. Argument: A (array or 1)
S Take the sum of A.
‘ Increment; add 1.
ɗƇ Drei comb; keep only elements k of [1, ..., sum(A)+1] for which the
three links to the left return a truthy value.
g Take the GCD of k and all elements of A.
Ṫ Tail; extract the last GCD.
’ Decrement the result, mapping 1 to 0.
ḟ¹ Filterfalse; remove the elements that occur in A.
Ṃ Take the minimum.
ṭ Tack; append the minimum to A.
Note that the generated sequence is $[1, mathbf0, 2, 4, 6, 3, 9, 12, 8, 10, 5, 15, dots]$. Since calling the helper link $n$ times generates a sequence of length $n + 1$, the $0$ is practically ignored.
edited Nov 13 '18 at 14:47
answered Nov 13 '18 at 14:03
Dennis♦Dennis
187k32297736
187k32297736
add a comment |
add a comment |
$begingroup$
Perl 6, 66 bytes
+grep *>$_,(1,2,first *gcd@_[*-1]>1,grep *∉@_,1..*...*)[^$_]
Try it online!
Too slow on TIO for n = 1000.
$endgroup$
add a comment |
$begingroup$
Perl 6, 66 bytes
+grep *>$_,(1,2,first *gcd@_[*-1]>1,grep *∉@_,1..*...*)[^$_]
Try it online!
Too slow on TIO for n = 1000.
$endgroup$
add a comment |
$begingroup$
Perl 6, 66 bytes
+grep *>$_,(1,2,first *gcd@_[*-1]>1,grep *∉@_,1..*...*)[^$_]
Try it online!
Too slow on TIO for n = 1000.
$endgroup$
Perl 6, 66 bytes
+grep *>$_,(1,2,first *gcd@_[*-1]>1,grep *∉@_,1..*...*)[^$_]
Try it online!
Too slow on TIO for n = 1000.
edited Nov 13 '18 at 15:39
answered Nov 13 '18 at 15:21
nwellnhofnwellnhof
6,53511125
6,53511125
add a comment |
add a comment |
$begingroup$
JavaScript (ES6), 107 106 105 bytes
f=(n,a=[2,1],k=3)=>a[n-1]?0:a.indexOf(k)+(C=(a,b)=>b?C(b,a%b):a>1)(k,a[0])?f(n,a,k+1):(k>n)+f(n,[k,...a])
Try it online!
How?
The helper function $C$ returns true if two given integers are not coprime:
C = (a, b) => b ? C(b, a % b) : a > 1
The array $a$ is initialized to $[2,1]$ and holds all values that were encountered so far in reverse order. Therefore, the last value is always $a[0]$.
To know if $k$ qualifies as the next term of the sequence, we test whether the following expression is equal to $0$:
a.indexOf(k) + C(k, a[0])
a.indexOf(k)
is equal to either:
$-1$ if $k$ is not found in $a$
$0$ if $k$ is equal to the last value (in which case it's necessarily not coprime with it)- some $ige 1$ otherwise
Therefore, a.indexOf(k) + C(k, a[0])
is equal to $0$ if and only if $k$ is not found in $a$ and $k$ is not coprime with the last value ($-1+true=0$).
$endgroup$
add a comment |
$begingroup$
JavaScript (ES6), 107 106 105 bytes
f=(n,a=[2,1],k=3)=>a[n-1]?0:a.indexOf(k)+(C=(a,b)=>b?C(b,a%b):a>1)(k,a[0])?f(n,a,k+1):(k>n)+f(n,[k,...a])
Try it online!
How?
The helper function $C$ returns true if two given integers are not coprime:
C = (a, b) => b ? C(b, a % b) : a > 1
The array $a$ is initialized to $[2,1]$ and holds all values that were encountered so far in reverse order. Therefore, the last value is always $a[0]$.
To know if $k$ qualifies as the next term of the sequence, we test whether the following expression is equal to $0$:
a.indexOf(k) + C(k, a[0])
a.indexOf(k)
is equal to either:
$-1$ if $k$ is not found in $a$
$0$ if $k$ is equal to the last value (in which case it's necessarily not coprime with it)- some $ige 1$ otherwise
Therefore, a.indexOf(k) + C(k, a[0])
is equal to $0$ if and only if $k$ is not found in $a$ and $k$ is not coprime with the last value ($-1+true=0$).
$endgroup$
add a comment |
$begingroup$
JavaScript (ES6), 107 106 105 bytes
f=(n,a=[2,1],k=3)=>a[n-1]?0:a.indexOf(k)+(C=(a,b)=>b?C(b,a%b):a>1)(k,a[0])?f(n,a,k+1):(k>n)+f(n,[k,...a])
Try it online!
How?
The helper function $C$ returns true if two given integers are not coprime:
C = (a, b) => b ? C(b, a % b) : a > 1
The array $a$ is initialized to $[2,1]$ and holds all values that were encountered so far in reverse order. Therefore, the last value is always $a[0]$.
To know if $k$ qualifies as the next term of the sequence, we test whether the following expression is equal to $0$:
a.indexOf(k) + C(k, a[0])
a.indexOf(k)
is equal to either:
$-1$ if $k$ is not found in $a$
$0$ if $k$ is equal to the last value (in which case it's necessarily not coprime with it)- some $ige 1$ otherwise
Therefore, a.indexOf(k) + C(k, a[0])
is equal to $0$ if and only if $k$ is not found in $a$ and $k$ is not coprime with the last value ($-1+true=0$).
$endgroup$
JavaScript (ES6), 107 106 105 bytes
f=(n,a=[2,1],k=3)=>a[n-1]?0:a.indexOf(k)+(C=(a,b)=>b?C(b,a%b):a>1)(k,a[0])?f(n,a,k+1):(k>n)+f(n,[k,...a])
Try it online!
How?
The helper function $C$ returns true if two given integers are not coprime:
C = (a, b) => b ? C(b, a % b) : a > 1
The array $a$ is initialized to $[2,1]$ and holds all values that were encountered so far in reverse order. Therefore, the last value is always $a[0]$.
To know if $k$ qualifies as the next term of the sequence, we test whether the following expression is equal to $0$:
a.indexOf(k) + C(k, a[0])
a.indexOf(k)
is equal to either:
$-1$ if $k$ is not found in $a$
$0$ if $k$ is equal to the last value (in which case it's necessarily not coprime with it)- some $ige 1$ otherwise
Therefore, a.indexOf(k) + C(k, a[0])
is equal to $0$ if and only if $k$ is not found in $a$ and $k$ is not coprime with the last value ($-1+true=0$).
edited Nov 13 '18 at 17:45
answered Nov 13 '18 at 14:21
ArnauldArnauld
73.3k689307
73.3k689307
add a comment |
add a comment |
$begingroup$
Haskell, 89 82 bytes
Edit: -7 bytes thanks to @H.PWiz
f l=[n|n<-[3..],all(/=n)l,gcd(l!!0)n>1]!!0:l
g n=sum[1|x<-iterate f[2]!!(n-2),x>n]
Try it online!
$endgroup$
$begingroup$
82 bytes
$endgroup$
– H.PWiz
Nov 13 '18 at 17:41
$begingroup$
@H.PWiz: ah, that's clever. Thanks!
$endgroup$
– nimi
Nov 13 '18 at 17:50
add a comment |
$begingroup$
Haskell, 89 82 bytes
Edit: -7 bytes thanks to @H.PWiz
f l=[n|n<-[3..],all(/=n)l,gcd(l!!0)n>1]!!0:l
g n=sum[1|x<-iterate f[2]!!(n-2),x>n]
Try it online!
$endgroup$
$begingroup$
82 bytes
$endgroup$
– H.PWiz
Nov 13 '18 at 17:41
$begingroup$
@H.PWiz: ah, that's clever. Thanks!
$endgroup$
– nimi
Nov 13 '18 at 17:50
add a comment |
$begingroup$
Haskell, 89 82 bytes
Edit: -7 bytes thanks to @H.PWiz
f l=[n|n<-[3..],all(/=n)l,gcd(l!!0)n>1]!!0:l
g n=sum[1|x<-iterate f[2]!!(n-2),x>n]
Try it online!
$endgroup$
Haskell, 89 82 bytes
Edit: -7 bytes thanks to @H.PWiz
f l=[n|n<-[3..],all(/=n)l,gcd(l!!0)n>1]!!0:l
g n=sum[1|x<-iterate f[2]!!(n-2),x>n]
Try it online!
edited Nov 13 '18 at 17:50
answered Nov 13 '18 at 15:08
niminimi
31.5k32185
31.5k32185
$begingroup$
82 bytes
$endgroup$
– H.PWiz
Nov 13 '18 at 17:41
$begingroup$
@H.PWiz: ah, that's clever. Thanks!
$endgroup$
– nimi
Nov 13 '18 at 17:50
add a comment |
$begingroup$
82 bytes
$endgroup$
– H.PWiz
Nov 13 '18 at 17:41
$begingroup$
@H.PWiz: ah, that's clever. Thanks!
$endgroup$
– nimi
Nov 13 '18 at 17:50
$begingroup$
82 bytes
$endgroup$
– H.PWiz
Nov 13 '18 at 17:41
$begingroup$
82 bytes
$endgroup$
– H.PWiz
Nov 13 '18 at 17:41
$begingroup$
@H.PWiz: ah, that's clever. Thanks!
$endgroup$
– nimi
Nov 13 '18 at 17:50
$begingroup$
@H.PWiz: ah, that's clever. Thanks!
$endgroup$
– nimi
Nov 13 '18 at 17:50
add a comment |
$begingroup$
Husk, 16 bytes
#>¹↑¡§ḟȯ←⌋→`-Nḣ2
Try it online!
Explanation
#>¹↑¡§ḟȯ←⌋→`-Nḣ2 Implicit input, say n=10
ḣ2 Range to 2: [1,2]
¡ Construct an infinite list, adding new elements using this function:
Argument is list of numbers found so far, say L=[1,2,4]
N Natural numbers: [1,2,3,4,5,6,7...
`- Remove elements of L: K=[3,5,6,7...
ḟ Find first element of K that satisfies this:
Argument is a number in K, say 6
§ → Last element of L: 4
⌋ GCD: 2
ȯ← Decrement: 1
Implicitly: is it nonzero? Yes, so 6 is good.
Result is the EKG sequence: [1,2,4,6,3,9,12...
↑ Take the first n elements: [1,2,4,6,3,9,12,8,10,5]
# Count the number of those
>¹ that are larger than n: 1
$endgroup$
add a comment |
$begingroup$
Husk, 16 bytes
#>¹↑¡§ḟȯ←⌋→`-Nḣ2
Try it online!
Explanation
#>¹↑¡§ḟȯ←⌋→`-Nḣ2 Implicit input, say n=10
ḣ2 Range to 2: [1,2]
¡ Construct an infinite list, adding new elements using this function:
Argument is list of numbers found so far, say L=[1,2,4]
N Natural numbers: [1,2,3,4,5,6,7...
`- Remove elements of L: K=[3,5,6,7...
ḟ Find first element of K that satisfies this:
Argument is a number in K, say 6
§ → Last element of L: 4
⌋ GCD: 2
ȯ← Decrement: 1
Implicitly: is it nonzero? Yes, so 6 is good.
Result is the EKG sequence: [1,2,4,6,3,9,12...
↑ Take the first n elements: [1,2,4,6,3,9,12,8,10,5]
# Count the number of those
>¹ that are larger than n: 1
$endgroup$
add a comment |
$begingroup$
Husk, 16 bytes
#>¹↑¡§ḟȯ←⌋→`-Nḣ2
Try it online!
Explanation
#>¹↑¡§ḟȯ←⌋→`-Nḣ2 Implicit input, say n=10
ḣ2 Range to 2: [1,2]
¡ Construct an infinite list, adding new elements using this function:
Argument is list of numbers found so far, say L=[1,2,4]
N Natural numbers: [1,2,3,4,5,6,7...
`- Remove elements of L: K=[3,5,6,7...
ḟ Find first element of K that satisfies this:
Argument is a number in K, say 6
§ → Last element of L: 4
⌋ GCD: 2
ȯ← Decrement: 1
Implicitly: is it nonzero? Yes, so 6 is good.
Result is the EKG sequence: [1,2,4,6,3,9,12...
↑ Take the first n elements: [1,2,4,6,3,9,12,8,10,5]
# Count the number of those
>¹ that are larger than n: 1
$endgroup$
Husk, 16 bytes
#>¹↑¡§ḟȯ←⌋→`-Nḣ2
Try it online!
Explanation
#>¹↑¡§ḟȯ←⌋→`-Nḣ2 Implicit input, say n=10
ḣ2 Range to 2: [1,2]
¡ Construct an infinite list, adding new elements using this function:
Argument is list of numbers found so far, say L=[1,2,4]
N Natural numbers: [1,2,3,4,5,6,7...
`- Remove elements of L: K=[3,5,6,7...
ḟ Find first element of K that satisfies this:
Argument is a number in K, say 6
§ → Last element of L: 4
⌋ GCD: 2
ȯ← Decrement: 1
Implicitly: is it nonzero? Yes, so 6 is good.
Result is the EKG sequence: [1,2,4,6,3,9,12...
↑ Take the first n elements: [1,2,4,6,3,9,12,8,10,5]
# Count the number of those
>¹ that are larger than n: 1
answered Nov 14 '18 at 8:19
ZgarbZgarb
26.4k462229
26.4k462229
add a comment |
add a comment |
$begingroup$
MATL, 29 bytes
qq:2:w"GE:yX-y0)yZdqg)1)h]G>z
Try it online!
Explanation:
#implicit input, n, say 10
qq: #push 1:8
2: #push [1 2]. Stack: [1 .. 8], [1 2]
w #swap top two elements on stack
" #begin for loop (do the following n-2 times):
GE: #push 1...20. Stack: [1 2], [1..20]
y #copy from below. Stack:[1 2], [1..20], [1 2]
X- #set difference. Stack: [1 2], [3..20]
y0) #copy last element from below. Stack:[1 2], [3..20], 2
yZd #copy from below and elementwise GCD. Stack:[1 2], [3..20],[1,2,etc.]
qg) #select those with gcd greater than 1. Stack:[1 2], [4,6,etc.]
1) #take first. Stack:[1 2], 4
h #horizontally concatenate. Stack:[1 2 4]
] #end of for loop
G>z #count those greater than input
#implicit output of result
$endgroup$
$begingroup$
please can you explain why do you double the input (withGE:
)?
$endgroup$
– david
Nov 13 '18 at 20:56
1
$begingroup$
@david I think empirically I noticed that $a(n)leq 2n$ but I don't have a proof, so I originally used $a(n)leq n^2$, which timed out on the $n=1000$ test case, so I switched it back to test that, and left it in. I'm still not sure about either bound, but it seems to work, so I may try to come up with a proof. Awhile
loop would be much messier in MATL so I was trying to avoid it.
$endgroup$
– Giuseppe
Nov 13 '18 at 21:08
add a comment |
$begingroup$
MATL, 29 bytes
qq:2:w"GE:yX-y0)yZdqg)1)h]G>z
Try it online!
Explanation:
#implicit input, n, say 10
qq: #push 1:8
2: #push [1 2]. Stack: [1 .. 8], [1 2]
w #swap top two elements on stack
" #begin for loop (do the following n-2 times):
GE: #push 1...20. Stack: [1 2], [1..20]
y #copy from below. Stack:[1 2], [1..20], [1 2]
X- #set difference. Stack: [1 2], [3..20]
y0) #copy last element from below. Stack:[1 2], [3..20], 2
yZd #copy from below and elementwise GCD. Stack:[1 2], [3..20],[1,2,etc.]
qg) #select those with gcd greater than 1. Stack:[1 2], [4,6,etc.]
1) #take first. Stack:[1 2], 4
h #horizontally concatenate. Stack:[1 2 4]
] #end of for loop
G>z #count those greater than input
#implicit output of result
$endgroup$
$begingroup$
please can you explain why do you double the input (withGE:
)?
$endgroup$
– david
Nov 13 '18 at 20:56
1
$begingroup$
@david I think empirically I noticed that $a(n)leq 2n$ but I don't have a proof, so I originally used $a(n)leq n^2$, which timed out on the $n=1000$ test case, so I switched it back to test that, and left it in. I'm still not sure about either bound, but it seems to work, so I may try to come up with a proof. Awhile
loop would be much messier in MATL so I was trying to avoid it.
$endgroup$
– Giuseppe
Nov 13 '18 at 21:08
add a comment |
$begingroup$
MATL, 29 bytes
qq:2:w"GE:yX-y0)yZdqg)1)h]G>z
Try it online!
Explanation:
#implicit input, n, say 10
qq: #push 1:8
2: #push [1 2]. Stack: [1 .. 8], [1 2]
w #swap top two elements on stack
" #begin for loop (do the following n-2 times):
GE: #push 1...20. Stack: [1 2], [1..20]
y #copy from below. Stack:[1 2], [1..20], [1 2]
X- #set difference. Stack: [1 2], [3..20]
y0) #copy last element from below. Stack:[1 2], [3..20], 2
yZd #copy from below and elementwise GCD. Stack:[1 2], [3..20],[1,2,etc.]
qg) #select those with gcd greater than 1. Stack:[1 2], [4,6,etc.]
1) #take first. Stack:[1 2], 4
h #horizontally concatenate. Stack:[1 2 4]
] #end of for loop
G>z #count those greater than input
#implicit output of result
$endgroup$
MATL, 29 bytes
qq:2:w"GE:yX-y0)yZdqg)1)h]G>z
Try it online!
Explanation:
#implicit input, n, say 10
qq: #push 1:8
2: #push [1 2]. Stack: [1 .. 8], [1 2]
w #swap top two elements on stack
" #begin for loop (do the following n-2 times):
GE: #push 1...20. Stack: [1 2], [1..20]
y #copy from below. Stack:[1 2], [1..20], [1 2]
X- #set difference. Stack: [1 2], [3..20]
y0) #copy last element from below. Stack:[1 2], [3..20], 2
yZd #copy from below and elementwise GCD. Stack:[1 2], [3..20],[1,2,etc.]
qg) #select those with gcd greater than 1. Stack:[1 2], [4,6,etc.]
1) #take first. Stack:[1 2], 4
h #horizontally concatenate. Stack:[1 2 4]
] #end of for loop
G>z #count those greater than input
#implicit output of result
answered Nov 13 '18 at 18:23
GiuseppeGiuseppe
16.5k31052
16.5k31052
$begingroup$
please can you explain why do you double the input (withGE:
)?
$endgroup$
– david
Nov 13 '18 at 20:56
1
$begingroup$
@david I think empirically I noticed that $a(n)leq 2n$ but I don't have a proof, so I originally used $a(n)leq n^2$, which timed out on the $n=1000$ test case, so I switched it back to test that, and left it in. I'm still not sure about either bound, but it seems to work, so I may try to come up with a proof. Awhile
loop would be much messier in MATL so I was trying to avoid it.
$endgroup$
– Giuseppe
Nov 13 '18 at 21:08
add a comment |
$begingroup$
please can you explain why do you double the input (withGE:
)?
$endgroup$
– david
Nov 13 '18 at 20:56
1
$begingroup$
@david I think empirically I noticed that $a(n)leq 2n$ but I don't have a proof, so I originally used $a(n)leq n^2$, which timed out on the $n=1000$ test case, so I switched it back to test that, and left it in. I'm still not sure about either bound, but it seems to work, so I may try to come up with a proof. Awhile
loop would be much messier in MATL so I was trying to avoid it.
$endgroup$
– Giuseppe
Nov 13 '18 at 21:08
$begingroup$
please can you explain why do you double the input (with
GE:
)?$endgroup$
– david
Nov 13 '18 at 20:56
$begingroup$
please can you explain why do you double the input (with
GE:
)?$endgroup$
– david
Nov 13 '18 at 20:56
1
1
$begingroup$
@david I think empirically I noticed that $a(n)leq 2n$ but I don't have a proof, so I originally used $a(n)leq n^2$, which timed out on the $n=1000$ test case, so I switched it back to test that, and left it in. I'm still not sure about either bound, but it seems to work, so I may try to come up with a proof. A
while
loop would be much messier in MATL so I was trying to avoid it.$endgroup$
– Giuseppe
Nov 13 '18 at 21:08
$begingroup$
@david I think empirically I noticed that $a(n)leq 2n$ but I don't have a proof, so I originally used $a(n)leq n^2$, which timed out on the $n=1000$ test case, so I switched it back to test that, and left it in. I'm still not sure about either bound, but it seems to work, so I may try to come up with a proof. A
while
loop would be much messier in MATL so I was trying to avoid it.$endgroup$
– Giuseppe
Nov 13 '18 at 21:08
add a comment |
$begingroup$
JavaScript, 93 91 87 bytes
Throws an overflow error for 0
or 1
, outputs 0
for 2
.
n=>(g=x=>n-i?g[++x]|(h=(y,z)=>z?h(z,y%z):y)(x,c)<2?g(x):(g[c=x]=++i,x>n)+g(2):0)(i=c=2)
Try it online
$endgroup$
add a comment |
$begingroup$
JavaScript, 93 91 87 bytes
Throws an overflow error for 0
or 1
, outputs 0
for 2
.
n=>(g=x=>n-i?g[++x]|(h=(y,z)=>z?h(z,y%z):y)(x,c)<2?g(x):(g[c=x]=++i,x>n)+g(2):0)(i=c=2)
Try it online
$endgroup$
add a comment |
$begingroup$
JavaScript, 93 91 87 bytes
Throws an overflow error for 0
or 1
, outputs 0
for 2
.
n=>(g=x=>n-i?g[++x]|(h=(y,z)=>z?h(z,y%z):y)(x,c)<2?g(x):(g[c=x]=++i,x>n)+g(2):0)(i=c=2)
Try it online
$endgroup$
JavaScript, 93 91 87 bytes
Throws an overflow error for 0
or 1
, outputs 0
for 2
.
n=>(g=x=>n-i?g[++x]|(h=(y,z)=>z?h(z,y%z):y)(x,c)<2?g(x):(g[c=x]=++i,x>n)+g(2):0)(i=c=2)
Try it online
edited Nov 19 '18 at 11:49
answered Nov 13 '18 at 21:59
ShaggyShaggy
19.2k21666
19.2k21666
add a comment |
add a comment |
$begingroup$
Japt, 23 21 bytes
@_jX ªAøZ}f}gA=ì)Aè>U
Try it
@_jX ªAøZ}f}gA=ì)Aè>U
:Implicit input of integer U
A :10
ì :Digit array
= :Reassign to A
@ }g :While the length of A < U+1, take the last element as X,
:pass it through the following function & push the result to A
_ }f : Find the first integer Z >= 0 that returns falsey
jX : Is Z co-prime with X?
ª : OR
AøZ : Does A contain Z?
) :End loop
Aè>U :Count the elements in A that are greater than U
$endgroup$
add a comment |
$begingroup$
Japt, 23 21 bytes
@_jX ªAøZ}f}gA=ì)Aè>U
Try it
@_jX ªAøZ}f}gA=ì)Aè>U
:Implicit input of integer U
A :10
ì :Digit array
= :Reassign to A
@ }g :While the length of A < U+1, take the last element as X,
:pass it through the following function & push the result to A
_ }f : Find the first integer Z >= 0 that returns falsey
jX : Is Z co-prime with X?
ª : OR
AøZ : Does A contain Z?
) :End loop
Aè>U :Count the elements in A that are greater than U
$endgroup$
add a comment |
$begingroup$
Japt, 23 21 bytes
@_jX ªAøZ}f}gA=ì)Aè>U
Try it
@_jX ªAøZ}f}gA=ì)Aè>U
:Implicit input of integer U
A :10
ì :Digit array
= :Reassign to A
@ }g :While the length of A < U+1, take the last element as X,
:pass it through the following function & push the result to A
_ }f : Find the first integer Z >= 0 that returns falsey
jX : Is Z co-prime with X?
ª : OR
AøZ : Does A contain Z?
) :End loop
Aè>U :Count the elements in A that are greater than U
$endgroup$
Japt, 23 21 bytes
@_jX ªAøZ}f}gA=ì)Aè>U
Try it
@_jX ªAøZ}f}gA=ì)Aè>U
:Implicit input of integer U
A :10
ì :Digit array
= :Reassign to A
@ }g :While the length of A < U+1, take the last element as X,
:pass it through the following function & push the result to A
_ }f : Find the first integer Z >= 0 that returns falsey
jX : Is Z co-prime with X?
ª : OR
AøZ : Does A contain Z?
) :End loop
Aè>U :Count the elements in A that are greater than U
edited Nov 13 '18 at 20:02
answered Nov 13 '18 at 16:55
ShaggyShaggy
19.2k21666
19.2k21666
add a comment |
add a comment |
$begingroup$
Python 3, 153 bytes
import math
def f(n):
l=[1];c=0
for _ in range(n-1):l+=[min(*range(2,n*4),key=lambda x:n*8if x in l or math.gcd(x,l[-1])<2else x)];c+=l[-1]>n
return c
Try it online! (Warning: Takes ~30 seconds to evaluate)
$endgroup$
add a comment |
$begingroup$
Python 3, 153 bytes
import math
def f(n):
l=[1];c=0
for _ in range(n-1):l+=[min(*range(2,n*4),key=lambda x:n*8if x in l or math.gcd(x,l[-1])<2else x)];c+=l[-1]>n
return c
Try it online! (Warning: Takes ~30 seconds to evaluate)
$endgroup$
add a comment |
$begingroup$
Python 3, 153 bytes
import math
def f(n):
l=[1];c=0
for _ in range(n-1):l+=[min(*range(2,n*4),key=lambda x:n*8if x in l or math.gcd(x,l[-1])<2else x)];c+=l[-1]>n
return c
Try it online! (Warning: Takes ~30 seconds to evaluate)
$endgroup$
Python 3, 153 bytes
import math
def f(n):
l=[1];c=0
for _ in range(n-1):l+=[min(*range(2,n*4),key=lambda x:n*8if x in l or math.gcd(x,l[-1])<2else x)];c+=l[-1]>n
return c
Try it online! (Warning: Takes ~30 seconds to evaluate)
answered Nov 19 '18 at 19:19
Black Owl KaiBlack Owl Kai
5807
5807
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).
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%2f175858%2fterms-of-the-ekg-sequence%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
$begingroup$
Can we use 0-indexing, with
1
being the 0th term of the sequence and therefor making, for example,15
the 10th term, rather than5
?$endgroup$
– Shaggy
Nov 13 '18 at 16:48
$begingroup$
@Shaggy I think it's fair to use this as a mathematical way, but actually it will change the result of test cases and indeed the asked function in itself. Thus I think you should'nt be allowed to do so. Sorry.
$endgroup$
– david
Nov 13 '18 at 16:59