| Bytes | Lang | Time | Link |
|---|---|---|---|
| 015 | Pip | 210725T041924Z | DLosc |
| 042 | bash | 171113T162901Z | Nahuel F |
| 035 | Perl 5 | 171114T103638Z | Nahuel F |
| 056 | APL Dyalog Unicode | 171110T160217Z | J. Sall& |
| 046 | Ruby | 171113T071749Z | G B |
| 074 | Common Lisp | 171113T091123Z | Renzo |
| 040 | J | 171110T201607Z | Galen Iv |
| 045 | JavaScript | 171113T064116Z | tsh |
| 025 | APL | 171111T161045Z | Graham |
| 046 | Haskell | 171110T135625Z | Laikoni |
| 044 | Perl 5 | 171110T212404Z | Xcali |
| 061 | Wolfram Language Mathematica | 171110T203634Z | Martin E |
| 068 | Mathematica | 171110T201758Z | ZaMoC |
| 085 | Batch | 171110T170455Z | Neil |
| 096 | Java 8 | 171110T164705Z | Kevin Cr |
| 051 | Python 2 | 171110T163513Z | Dennis |
| 084 | C gcc | 171110T162126Z | pizzapan |
| 107 | Java OpenJDK 8 | 171110T153613Z | Roberto |
| 052 | JavaScript ES6 | 171110T142152Z | Arnauld |
| 052 | Python 3 | 171110T145442Z | Dennis |
| 010 | Husk | 171110T135608Z | Martin E |
| 010 | Jelly | 171110T140013Z | Dennis |
| 055 | Proton | 171110T140701Z | hyperneu |
| 017 | Jelly | 171110T135649Z | hyperneu |
Pip, 15 bytes
W P1L UiP2*Uv%t
Prints indefinitely. Attempt This Online!
Explanation
W While this expression is truthy--
P1 print 1 (evaluates to 1, which is truthy)
--loop:
Ui Increment i (initially 0)
L and loop that many times:
Uv Increment v (initially -1)
2* Multiply by 2
%t Mod 10
P Print
bash, 42 bytes
for((;;)){ echo $[i--?r++%5*2:(i=++j)>0];}
or 34 if valid
echo $[i--?r++%5*2:(i=++j)>0];. $0
APL (Dyalog Unicode), 52 59 56 bytes
r←c k
r←~n←0
:For j :In ⍳k
n+←j-1
r,←1,⍨j⍴n⌽0,2×⍳4
:End
r←k⍴r
This is a tradfn (traditional function) taking one argument k and returning the first k items of the sequence.
Thanks to @GalenIvanov for pointing out an error in the function. Thanks to @Adám for 3 bytes.
How it works:
r←c k ⍝ The function c takes the argument k and results in r
r n←1 0 ⍝ Initializing the variables r and n, and setting them to 1 and 0, respectively.
:For j :In ⍳k ⍝ For loop. ⍳k yields [1, 2, 3, ..., k], and j is the control variable.
n+←j-1 ⍝ Accumulates j-1 into n, so it follows the progression (0, 1, 3, 6, 10, 15...)
r,←1,⍨j⍴n⌽0,2×⍳4 ⍝ This line is explained below.
:End ⍝ Ends the loop
r←k⍴r ⍝ return the first k items of r.
⍝ ⍴ actually reshapes the vector r to the shape of k;
⍝ since k is a scalar, ⍴ reshapes r to a vector with k items.
2×⍳4 ⍝ APL is 1-indexed by default, so this yields the vector 2 4 6 8
0, ⍝ Prepend a 0 to it. We now have 0 2 4 6 8
n⌽ ⍝ Rotate the vector n times to the left.
j⍴ ⍝ Reshape it to have j items, which cycles the vector.
1,⍨ ⍝ Append a 1, then
r,← ⍝ Append everything to r.
Below are a Dfn(direct function) and a tacit function that also solve the challenge, both kindly provided by @Adám.
- Dfn:
{⍵⍴1,∊1,⍨¨j⍴¨(+\¯1+j←⍳⍵)⌽¨⊂0,2×⍳4}Try it online! - Tacit:
⊢⍴1,∘∊1,⍨¨⍳⍴¨(⊂0,2×⍳4)⌽⍨¨(+\¯1+⍳)Try it online!
Common Lisp, 74 bytes
(do((b 1(1+ b))(k -2))(())(print 1)(dotimes(m b)(print(mod(incf k 2)10))))
Prints the sequence indefinitely.
J, 46 42 40 bytes
-6 bytes thanks to cole
{.({.[:;[:#:&.>2^i.);@(1,&.><;.1)10|2*i.
Outputs the first N terms of this sequence.
How it works:
10|[:+:i. - generates a list of length N of 0 2 4 6 8 0 2 4... It simply takes mod 10 of the doubled items of a list of integers starting at 0.
[:;[:#:&.>2^i. - generates a bit mask for cutting the above list.
(1 means start): 1 1 0 1 0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 0 ... It finds 2 to the power of the consecutive non-negative integers, converts them into binary, flattens the list and takes only the first N items, so that the length of the both lists is the same.
;@(1,&.><;.1) - splits (cuts) the list of the even digits into sublistsaccording to the map of ones and zeroes, appends the sublist to 1 and finally flattens the resulting list
]{. - takes only the first N items, getting rid of the additional numbers in the list due to the added 1s.
JavaScript, 45 bytes
1 indexed:
f=(x,t=0,p=0)=>p<x?f(x-1,t+1,p+t):x-p?x%5*2:1
0 indexed:
g=(x,p=0)=>p<x?g(x-1,p?++t+p:t=1):x-p?x%5*2:1
f=(x,t=0,p=0)=>p<x?f(x-1,t+1,p+t):x-p?x%5*2:1
g=(x,p=0)=>p<x?g(x-1,p?++t+p:t=1):x-p?x%5*2:1
a.value=[...Array(100)].map((_,i)=>f(i+1))
b.value=[...Array(100)].map((_,i)=>g(i))
<output id=a></output>
<output id=b></output>
APL, 25 bytes
Returns nth term.
1,(⌽n↑⌽(+/⍳n←⎕)⍴0,2×⍳4),1
Explanation
n←⎕ Prompts for screen input of integer
+/⍳ Creates a vector of integers of 1 to n and sums
⍴0,2×⍳4 Creates a vector by replicating 0 2 4 6 8 to the length of sum
⌽n↑⌽ Rotates the vector, selects first n elements and rotates result
(selects last n elements}
1,...,1 Concatenates 1s in front and behind result
Haskell, 50 46 bytes
1#cycle[0,2..8]
n#r=1:take n r++(n+1)#drop n r
1#cycle[0,2..8] returns the sequence as infinite list.
-4 bytes thanks to Ørjan Johansen!
Wolfram Language (Mathematica), 61 bytes
Flatten[TakeList[2Mod[r=0~Range~#,5],UpTo/@r]~Riffle~1][[#]]&
Mathematica, 68 bytes
Returns the Nth term
Insert[Join@@Table[{0,2,4,6,8},#^2],1,Array[{(2-#+#^2)/2}&,#]][[#]]&
Batch, 85 bytes
@set/an=%2+1,t=n*-~n/2
@if %t% lss %1 %0 %1 %n%
@cmd/cset/a(%1-n)%%5*2*!!(t-=%1)+!t
Outputs the Nth term of the sequence. Works by calculating the next triangular number.
Java 8, 96 bytes
v->{for(int i=0,j=-2,k;;i++,System.out.println(1))for(k=0;k++<i;System.out.println(j%=10))j+=2;}
Prints indefinitely, each number on a new line.
Explanation:
v->{ // Method with empty unused parameter and no return-type
for(int i=0, // Index integer, starting at 1
j=-2, // Even index integer, starting at -2
k; // Inner index integer
; // Loop (1) indefinitely
// After every iteration:
i++, // Increase index `i` by 1
System.out.println(1)) // And print a 1
for(k=0; // Reset index `k` to 0
k++<i; // Inner loop (2) from 0 to `i`
// After every iteration:
System.out.println(j%=10)) // Set `j` to `j` modulo-10, and print it
j+=2; // Increase `j` by 2
// End of inner loop (2) (implicit / single-line body)
// End of loop (1) (implicit / single-line body)
} // End of method
C (gcc), 84 bytes
i;j;f(){for(i=1;;i++){printf("%d ",1);for(j=0;j<i;)printf("%d ",(2*j+++i*i-i)%10);}}
A function (f()) that prints the sequence infinitely, separated by spaces.
i is the length of the current even-run.
j is the index in the current even-run
(2*j+++i*i-i)%10 gives the correct even number, given i and j (and increments j), equivalent to ((j+Tr(i))%5)*2, where Tr(x) is the xth triangular number (which is the number of even numbers that have been printed before the current even run;
Java (OpenJDK 8), 107 bytes
i->{String b="";for(int l=i,r=0,z=0;i>0;b+=l-i--==r?(l=i)<1+r++*0?"1":"1":"02468".charAt(z++%5));return b;}
JavaScript (ES6), 62 54 52 bytes
Returns the Nth term of the sequence, 0-indexed.
n=>(g=e=>n--?g(e+=k++<l?2:k=!++l):k<l?e%10:1)(k=l=0)
Demo
let f =
n=>(g=e=>n--?g(e+=k++<l?2:k=!++l):k<l?e%10:1)(k=l=0)
for(n = 0; n < 30; n++) {
console.log('a(' + n + ') = ' + f(n))
}
Python 3, 52 bytes
def f(n):t=(8*n+1)**.5+1;return 0==t%1or(n-t//2)%5*2
Takes a 1-based index and returns True or an integral float.
Husk, 12 11 10 bytes
ṁ:1CN¢mDŀ5
Prints the sequence indefinitely.
Alternatively:
J1CΘN¢mDŀ5
Explanation
ŀ5 Start from [0, 1, 2, 3, 4]
mD Double each value to get [0, 2, 4, 6, 8]
¢ Repeat this list indefinitely, [0, 2, 4, 6, 8, 0, 2, ...]
CN Cut it into chunks of increasing lengths,
[[0], [2, 4], [6, 8, 0], ...]
ṁ:1 Prepend 1 to each sublist and concate the resulting lists.
For the alternative solution:
¢mDŀ5 Again, get [0, 2, 4, 6, 8, 0, 2, ...].
CΘN This time we prepend a zero to the natural numbers, which
prepends an empty list to the resulting chunks.
J1 Join all the sublists with 1.
We could also do ...ΘCN..., because Θ does "prepend default element", which prepends a zero for lists of integers and an empty list for lists of lists.
Jelly, 10 bytes
5ḶḤṁR€1pFḣ
Returns the first n items of the sequence.
How it works
5ḶḤṁR€1pFḣ Main libk. Argument: n
5 Set the return value to 5.
Ḷ Unlength; yield [0, 1, 2, 3, 4].
Ḥ Unhalve; yield [0, 2, 4, 6, 8].
R€ Range each; yield [[1], [1, 2], [1, 2, 3], ..., [1, ..., n]].
ṁ Mold; in the result to the left, replace [1] with [0], [1, 2] with
[2, 4], [1, 2, 3] with [6, 8, 0], and so forth.
1p Take the Cartesian product of [1] and the result.
F Flatten the result.
ḣ Head; take the first n items of the result.
Proton, 55 bytes
i=0 j=-2while1{for k:0..i print(j=(j+2)%10)print(1)i++}
Prints the sequence indefinitely