Patience, young “Padovan”
$begingroup$
Everyone knows the Fibonacci sequence:
You take a square, attach an equal square to it, then repeatedly attach a square whose side length is equal to the largest side length of the resulting rectangle.
The result is a beautiful spiral of squares whose sequence of numbers is the Fibonacci sequence:
But, what if we didn't want to use squares?
If we use equilateral triangles—instead of squares—in a similar fashion, we get an equally beautiful spiral of triangles and a new sequence: the Padovan sequence, aka A000931:
Task:
Given a positive integer, $N$, output $a_N$, the $N$th term in the Padovan sequence OR the first $N$ terms.
Assume that the first three terms of the sequence are all $1$. Thus, the sequence will start as follows:
$$
1,1,1,2,2,3,...
$$
Input:
Any positive integer $Nge0$
Invalid input does not have to be taken into account
Output:
The $N$th term in the Padovan sequence OR the first $N$ terms of the Padovan sequence.
If the first $N$ terms are printed out, the output can be whatever is convenient (list/array, multi-line string, etc.)
Can be either $0$-indexed or $1$-indexed
Test Cases:
(0-indexed, $N$th term)
Input | Output
--------------
0 | 1
1 | 1
2 | 1
4 | 2
6 | 4
14 | 37
20 | 200
33 | 7739
(1-indexed, first $N$ terms)
Input | Output
--------------
1 | 1
3 | 1,1,1
4 | 1,1,1,2
7 | 1,1,1,2,2,3,4
10 | 1,1,1,2,2,3,4,5,7,9
12 | 1,1,1,2,2,3,4,5,7,9,12,16
Rules:
This is code-golf: the fewer bytes, the better!
Standard loopholes are forbidden.
code-golf number sequence
$endgroup$
|
show 3 more comments
$begingroup$
Everyone knows the Fibonacci sequence:
You take a square, attach an equal square to it, then repeatedly attach a square whose side length is equal to the largest side length of the resulting rectangle.
The result is a beautiful spiral of squares whose sequence of numbers is the Fibonacci sequence:
But, what if we didn't want to use squares?
If we use equilateral triangles—instead of squares—in a similar fashion, we get an equally beautiful spiral of triangles and a new sequence: the Padovan sequence, aka A000931:
Task:
Given a positive integer, $N$, output $a_N$, the $N$th term in the Padovan sequence OR the first $N$ terms.
Assume that the first three terms of the sequence are all $1$. Thus, the sequence will start as follows:
$$
1,1,1,2,2,3,...
$$
Input:
Any positive integer $Nge0$
Invalid input does not have to be taken into account
Output:
The $N$th term in the Padovan sequence OR the first $N$ terms of the Padovan sequence.
If the first $N$ terms are printed out, the output can be whatever is convenient (list/array, multi-line string, etc.)
Can be either $0$-indexed or $1$-indexed
Test Cases:
(0-indexed, $N$th term)
Input | Output
--------------
0 | 1
1 | 1
2 | 1
4 | 2
6 | 4
14 | 37
20 | 200
33 | 7739
(1-indexed, first $N$ terms)
Input | Output
--------------
1 | 1
3 | 1,1,1
4 | 1,1,1,2
7 | 1,1,1,2,2,3,4
10 | 1,1,1,2,2,3,4,5,7,9
12 | 1,1,1,2,2,3,4,5,7,9,12,16
Rules:
This is code-golf: the fewer bytes, the better!
Standard loopholes are forbidden.
code-golf number sequence
$endgroup$
2
$begingroup$
14
(0-indexed) is shown as outputting28
while I believe it should yield37
$endgroup$
– Jonathan Allan
2 days ago
$begingroup$
@JonathanAllan yes, you are correct. I fixed the last two test cases for $N$th term but not that one. The post has been edited.
$endgroup$
– Tau
2 days ago
$begingroup$
@LuisMendo I believe so. I'll edit the post.
$endgroup$
– Tau
yesterday
1
$begingroup$
@sharur this definition for the Fibonacci sequence is the visual definition. Each successive square added has a length of that term in the sequence. The sequence you describe is the numerical reasoning behind it. Both sequences work just as well as the other.
$endgroup$
– Tau
23 hours ago
1
$begingroup$
Note that the OEIS sequence you linked is slightly different, since it usesa_0=1, a_1=0, a_2=0
. It ends up being shifted by a bit because thena_5=a_6=a_7=1
$endgroup$
– Carmeister
23 hours ago
|
show 3 more comments
$begingroup$
Everyone knows the Fibonacci sequence:
You take a square, attach an equal square to it, then repeatedly attach a square whose side length is equal to the largest side length of the resulting rectangle.
The result is a beautiful spiral of squares whose sequence of numbers is the Fibonacci sequence:
But, what if we didn't want to use squares?
If we use equilateral triangles—instead of squares—in a similar fashion, we get an equally beautiful spiral of triangles and a new sequence: the Padovan sequence, aka A000931:
Task:
Given a positive integer, $N$, output $a_N$, the $N$th term in the Padovan sequence OR the first $N$ terms.
Assume that the first three terms of the sequence are all $1$. Thus, the sequence will start as follows:
$$
1,1,1,2,2,3,...
$$
Input:
Any positive integer $Nge0$
Invalid input does not have to be taken into account
Output:
The $N$th term in the Padovan sequence OR the first $N$ terms of the Padovan sequence.
If the first $N$ terms are printed out, the output can be whatever is convenient (list/array, multi-line string, etc.)
Can be either $0$-indexed or $1$-indexed
Test Cases:
(0-indexed, $N$th term)
Input | Output
--------------
0 | 1
1 | 1
2 | 1
4 | 2
6 | 4
14 | 37
20 | 200
33 | 7739
(1-indexed, first $N$ terms)
Input | Output
--------------
1 | 1
3 | 1,1,1
4 | 1,1,1,2
7 | 1,1,1,2,2,3,4
10 | 1,1,1,2,2,3,4,5,7,9
12 | 1,1,1,2,2,3,4,5,7,9,12,16
Rules:
This is code-golf: the fewer bytes, the better!
Standard loopholes are forbidden.
code-golf number sequence
$endgroup$
Everyone knows the Fibonacci sequence:
You take a square, attach an equal square to it, then repeatedly attach a square whose side length is equal to the largest side length of the resulting rectangle.
The result is a beautiful spiral of squares whose sequence of numbers is the Fibonacci sequence:
But, what if we didn't want to use squares?
If we use equilateral triangles—instead of squares—in a similar fashion, we get an equally beautiful spiral of triangles and a new sequence: the Padovan sequence, aka A000931:
Task:
Given a positive integer, $N$, output $a_N$, the $N$th term in the Padovan sequence OR the first $N$ terms.
Assume that the first three terms of the sequence are all $1$. Thus, the sequence will start as follows:
$$
1,1,1,2,2,3,...
$$
Input:
Any positive integer $Nge0$
Invalid input does not have to be taken into account
Output:
The $N$th term in the Padovan sequence OR the first $N$ terms of the Padovan sequence.
If the first $N$ terms are printed out, the output can be whatever is convenient (list/array, multi-line string, etc.)
Can be either $0$-indexed or $1$-indexed
Test Cases:
(0-indexed, $N$th term)
Input | Output
--------------
0 | 1
1 | 1
2 | 1
4 | 2
6 | 4
14 | 37
20 | 200
33 | 7739
(1-indexed, first $N$ terms)
Input | Output
--------------
1 | 1
3 | 1,1,1
4 | 1,1,1,2
7 | 1,1,1,2,2,3,4
10 | 1,1,1,2,2,3,4,5,7,9
12 | 1,1,1,2,2,3,4,5,7,9,12,16
Rules:
This is code-golf: the fewer bytes, the better!
Standard loopholes are forbidden.
code-golf number sequence
code-golf number sequence
edited yesterday
Tau
asked 2 days ago
TauTau
946515
946515
2
$begingroup$
14
(0-indexed) is shown as outputting28
while I believe it should yield37
$endgroup$
– Jonathan Allan
2 days ago
$begingroup$
@JonathanAllan yes, you are correct. I fixed the last two test cases for $N$th term but not that one. The post has been edited.
$endgroup$
– Tau
2 days ago
$begingroup$
@LuisMendo I believe so. I'll edit the post.
$endgroup$
– Tau
yesterday
1
$begingroup$
@sharur this definition for the Fibonacci sequence is the visual definition. Each successive square added has a length of that term in the sequence. The sequence you describe is the numerical reasoning behind it. Both sequences work just as well as the other.
$endgroup$
– Tau
23 hours ago
1
$begingroup$
Note that the OEIS sequence you linked is slightly different, since it usesa_0=1, a_1=0, a_2=0
. It ends up being shifted by a bit because thena_5=a_6=a_7=1
$endgroup$
– Carmeister
23 hours ago
|
show 3 more comments
2
$begingroup$
14
(0-indexed) is shown as outputting28
while I believe it should yield37
$endgroup$
– Jonathan Allan
2 days ago
$begingroup$
@JonathanAllan yes, you are correct. I fixed the last two test cases for $N$th term but not that one. The post has been edited.
$endgroup$
– Tau
2 days ago
$begingroup$
@LuisMendo I believe so. I'll edit the post.
$endgroup$
– Tau
yesterday
1
$begingroup$
@sharur this definition for the Fibonacci sequence is the visual definition. Each successive square added has a length of that term in the sequence. The sequence you describe is the numerical reasoning behind it. Both sequences work just as well as the other.
$endgroup$
– Tau
23 hours ago
1
$begingroup$
Note that the OEIS sequence you linked is slightly different, since it usesa_0=1, a_1=0, a_2=0
. It ends up being shifted by a bit because thena_5=a_6=a_7=1
$endgroup$
– Carmeister
23 hours ago
2
2
$begingroup$
14
(0-indexed) is shown as outputting 28
while I believe it should yield 37
$endgroup$
– Jonathan Allan
2 days ago
$begingroup$
14
(0-indexed) is shown as outputting 28
while I believe it should yield 37
$endgroup$
– Jonathan Allan
2 days ago
$begingroup$
@JonathanAllan yes, you are correct. I fixed the last two test cases for $N$th term but not that one. The post has been edited.
$endgroup$
– Tau
2 days ago
$begingroup$
@JonathanAllan yes, you are correct. I fixed the last two test cases for $N$th term but not that one. The post has been edited.
$endgroup$
– Tau
2 days ago
$begingroup$
@LuisMendo I believe so. I'll edit the post.
$endgroup$
– Tau
yesterday
$begingroup$
@LuisMendo I believe so. I'll edit the post.
$endgroup$
– Tau
yesterday
1
1
$begingroup$
@sharur this definition for the Fibonacci sequence is the visual definition. Each successive square added has a length of that term in the sequence. The sequence you describe is the numerical reasoning behind it. Both sequences work just as well as the other.
$endgroup$
– Tau
23 hours ago
$begingroup$
@sharur this definition for the Fibonacci sequence is the visual definition. Each successive square added has a length of that term in the sequence. The sequence you describe is the numerical reasoning behind it. Both sequences work just as well as the other.
$endgroup$
– Tau
23 hours ago
1
1
$begingroup$
Note that the OEIS sequence you linked is slightly different, since it uses
a_0=1, a_1=0, a_2=0
. It ends up being shifted by a bit because then a_5=a_6=a_7=1
$endgroup$
– Carmeister
23 hours ago
$begingroup$
Note that the OEIS sequence you linked is slightly different, since it uses
a_0=1, a_1=0, a_2=0
. It ends up being shifted by a bit because then a_5=a_6=a_7=1
$endgroup$
– Carmeister
23 hours ago
|
show 3 more comments
38 Answers
38
active
oldest
votes
1 2
next
$begingroup$
Jelly, 10 bytes
9s3’Ẓæ*³FṀ
Try it online!
1-indexed. Computes the largest element of: $$begin{bmatrix}0&0&1 \ 1&0&1 \ 0&1&0end{bmatrix}^n$$
where the binary matrix is conveniently computed as: $$begin{bmatrix}mathsf{isprime}(0)&mathsf{isprime}(1)&mathsf{isprime}(2) \ mathsf{isprime}(3)&mathsf{isprime}(4)&mathsf{isprime}(5) \ mathsf{isprime}(6)&mathsf{isprime}(7)&mathsf{isprime}(8)end{bmatrix}$$
(this is a total coincidence.)
9s3 [[1,2,3],[4,5,6],[7,8,9]] 9 split 3
’ [[0,1,2],[3,4,5],[6,7,8]] decrease
Ẓ [[0,0,1],[1,0,1],[0,1,0]] isprime
æ*³ [[0,0,1],[1,0,1],[0,1,0]]^n matrix power by input
FṀ flatten, maximum
$endgroup$
25
$begingroup$
this is clearly some kind of voodoo
$endgroup$
– Pureferret
yesterday
7
$begingroup$
This should be published.
$endgroup$
– YSC
yesterday
5
$begingroup$
@YSC It has already been published in A000931. I'd never have guess the primes trick:)
$endgroup$
– flawr
yesterday
$begingroup$
@flawr ho... I missed it
$endgroup$
– YSC
yesterday
1
$begingroup$
...make that "unless someone can golf two bytes off this one" :) (now that I have a 9 byter)
$endgroup$
– Jonathan Allan
yesterday
|
show 3 more comments
$begingroup$
Oasis, 5 bytes
nth term 0-indexed
cd+1V
Try it online!
Explanation
1V # a(0) = 1
# a(1) = 1
# a(2) = 1
# a(n) =
c # a(n-2)
+ # +
d # a(n-3)
$endgroup$
add a comment |
$begingroup$
Jelly, 10 9 8 bytes
ŻṚm2Jc$S
A monadic Link accepting n
(0-indexed) which yields P(n)
.
Try it online!
How?
Implements $P(n) = sum_{i=0}^{lfloorfrac{n}2rfloor}binom{i+1}{n-2i}$
ŻṚm2Jc$S - Link: integer, n e.g. 20
Ż - zero range [0, 1, 2, 3, 4, ..., 19, 20]
Ṛ - reverse [20, 19, ..., 4, 3, 2, 1, 0]
m2 - modulo-slice with 2 [20, 18, 16, 14, 12, 10, 8, 6, 4, 2, 0] <- n-2i
$ - last two links as a monad:
J - range of length [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] <- i+1
c - left-choose-right [ 0, 0, 0, 0, 0, 0, 0, 28,126, 45, 1]
S - sum 200
And here is a "twofer"
...a totally different method also for 8 bytes (this one is 1-indexed, but much slower):
3ḊṗRẎ§ċ‘ - Link: n
3Ḋ - 3 dequeued = [2,3]
R - range = [1,2,3,...,n]
ṗ - Cartesian power [[[2],[3]],[[2,2],[2,3],[3,2],[3,3]],[[2,2,2],...],...]
Ẏ - tighten [[2],[3],[2,2],[2,3],[3,2],[3,3],[2,2,2],...]
§ - sums [ 2, 3, 4, 5, 5, 6, 6,...]
‘ - increment n+1
ċ - count occurrences P(n)
$endgroup$
add a comment |
$begingroup$
Haskell, 26 bytes
(l!!)
l=1:1:1:2:scanl(+)2l
Try it online! Outputs the n'th term zero-indexed.
I thought that the "obvious" recursive solution below would be unbeatable, but then I found this. It's similar to the classic golfy expression l=1:scanl(+)1l
for the infinite Fibonacci list, but here the difference between adjacent elements is the term 4 positions back. We can more directly write l=1:1:zipWith(+)l(0:l)
, but that's longer.
If this challenge allowed infinite list output, we could cut the first line and have 20 bytes.
27 bytes
f n|n<3=1|1>0=f(n-2)+f(n-3)
Try it online!
$endgroup$
add a comment |
$begingroup$
Python 2, 30 bytes
f=lambda n:n<3or f(n-2)+f(n-3)
Try it online!
Returns the n'th term zero indexed. Outputs True
for 1.
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 33 bytes
a@0=a@1=a@2=1;a@n_:=a[n-2]+a[n-3]
1-indexed, returns the nth term
Try it online!
$endgroup$
add a comment |
$begingroup$
Octave / MATLAB, 35 33 bytes
@(n)[1 filter(1,'cbaa'-98,2:n<5)]
Outputs the first n terms.
Try it online!
How it works
Anonymous function that implements a recursive filter.
'cbaa'-98
is a shorter form to produce [1 0 -1 -1]
.
2:n<5
is a shorter form to produce [1 1 1 0 0 ··· 0]
(n−1 terms).
filter(1,[1 0 -1 -1],[1 1 1 0 0 ··· 0])
passes the input [1 1 1 0 0 ··· 0]
through a discrete-time filter defined by a transfer function with numerator coefficient 1
and denominator coefficients [1 0 -1 -1]
.
$endgroup$
add a comment |
$begingroup$
Retina, 47 42 bytes
K`0¶1¶0
"$+"+`.+¶(.+)¶.+$
$&¶$.(*_$1*
6,G`
Try it online! Outputs the first n
terms on separate lines. Explanation:
K`0¶1¶0
Replace the input with the terms for -2
, -1
and 0
.
"$+"+`.+¶(.+)¶.+$
$&¶$.(*_$1*
Generate the next n
terms using the recurrence relation. *_
here is short for $&*_
which converts the (first) number in the match to unary, while $1*
is short for $1*_
which converts the middle number to unary. The $.(
returns the decimal sum of its unary arguments, i.e. the sum of the first and middle numbers.
6,G`
Discard the first six characters, i.e. the first three lines.
$endgroup$
add a comment |
$begingroup$
Cubix, 20 bytes
This is 0 indexed and outputs the Nth term
;@UOI010+p?/sqq;W.(
Try it online!
Wraps onto a cube with side length 2
; @
U O
I 0 1 0 + p ? /
s q q ; W . (
. .
. .
Watch it run
I010
- Initiates the stack
+p?
- Adds the top of stack, pulls the counter from the bottom of stack and tests
/;UO@
- If counter is 0, reflect onto top face, remove TOS, u-turn, output and halt
(sqq;W
- If counter is positive, reflect, decrement counter, swap TOS, push top to bottom twice, remove TOS and shift lane back into the main loop.
$endgroup$
add a comment |
$begingroup$
J, 23 bytes
-1 byte thanks to ngn and Galen
closed form, 26 bytes
0.5<.@+1.04535%~1.32472^<:
Try it online!
iterative, 23 bytes
(],1#._2 _3{ ::1:])^:[#
Try it online!
$endgroup$
1
$begingroup$
Another 24-byte solution (boring) : (1#.2 3$:@-~])`1:@.(3&>) Try it online!
$endgroup$
– Galen Ivanov
yesterday
$begingroup$
23 bytes thanks to ngn1:
->#
: Try it online!
$endgroup$
– Galen Ivanov
yesterday
$begingroup$
@GalenIvanov tyvm, that's a great trick.
$endgroup$
– Jonah
yesterday
add a comment |
$begingroup$
Python 2, 56 48 bytes
f=lambda n,a=1,b=1,c=1:n>2and f(n-1,b,c,a+b)or c
Try it online!
Returns nth value, 0-indexed.
$endgroup$
add a comment |
$begingroup$
Jelly, 11 bytes
5B+Ɲ2ị;Ʋ⁸¡Ḣ
Try it online!
0-indexed.
$endgroup$
add a comment |
$begingroup$
Perl 6, 24 bytes
{(1,1,1,*+*+!*...*)[$_]}
Try it online!
A pretty standard generated sequence, with each new element generated by the expression * + * + !*
. That adds the third-previous element, the second-previous element, and the logical negation of the previous element, which is always False
, which is numerically zero.
$endgroup$
$begingroup$
Why is this community wiki?
$endgroup$
– Jo King
2 days ago
$begingroup$
@JoKing Beats me. If I did it somehow, it wasn't on purpose.
$endgroup$
– Sean
yesterday
add a comment |
$begingroup$
Lua 5.3, 49 48 bytes
function f(n)return n<4 and 1or f(n-2)+f(n-3)end
Try it online!
Vanilla Lua doesn't have coercion of booleans to strings (even tonumber(true)
returns nil
), so you have to use a pseudo-ternary operator. This version is 1-indexed, like all of Lua. The 1or
part has to be changed to 1 or
in Lua 5.1, which has a different way of lexing numbers.
$endgroup$
add a comment |
$begingroup$
Ruby, 26 bytes
f=->n{n<3?1:f[n-2]+f[n-3]}
Try it online!
$endgroup$
add a comment |
$begingroup$
APL (Dyalog Unicode), 20 18 17 bytesSBCS
This code is 1-indexed. It's the same number of bytes to get n
items of the Padovan sequence, as you have to drop the last few extra members. It's also the same number of bytes to get 0-indexing.
Edit: -2 bytes thanks to ngn. -1 byte thanks to ngn
4⌷2(⊢,⍨2⌷+/)⍣⎕×⍳3
Try it online!
Explanation
4⌷2(⊢,⍨2⌷+/)⍣⎕×⍳3
⍺(. . . .)⍣⎕⍵ This format simply takes the input ⎕ and applies the function
inside the brackets (...) to its operands (here marked ⍵ and ⍺).
2(. . .+/)⍣⎕×⍳3 In this case, our ⍵, the left argument, is the array 1 1 1,
where we save our results as the function is repeatedly applied
and our ⍺, 2, is our right argument and is immediately applied to +/,
so that we have 2+/ which will return the pairwise sums of our array.
2⌷ We take the second pairwise sum, f(n-2) + f(n-3)
⊢,⍨ And add it to the head of our array.
4⌷ When we've finished adding Padovan numbers to the end of our list,
the n-th Padovan number (1-indexed) is the 4th member of that list,
and so, we implicitly return that.
$endgroup$
add a comment |
$begingroup$
K (ngn/k), 24 20 bytes
-4 bytes thanks to ngn!
{$[x<3;1;+/o'x-2 3]}
Try it online!
0-indexed, first N terms
$endgroup$
1
$begingroup$
f[x-2]+f[x-3]
->+/o'x-2 3
(o
is "recur")
$endgroup$
– ngn
yesterday
$begingroup$
@ngn Thanks! I tried it (without success) in J; it's elegant here.
$endgroup$
– Galen Ivanov
yesterday
$begingroup$
@ngn In fact here's one possibillity how it looks in J: (1#.2 3$:@-~])`1:@.(3&>)
$endgroup$
– Galen Ivanov
yesterday
$begingroup$
ah, right, base-1 decode is a train-friendly way to sum :)
$endgroup$
– ngn
yesterday
2
$begingroup$
1:
->#
in the j solution
$endgroup$
– ngn
yesterday
|
show 2 more comments
$begingroup$
x86 32-bit machine code, 17 bytes
53 33 db f7 e3 43 83 c1 04 03 d8 93 92 e2 fa 5b c3
Disassembly:
00CE1250 53 push ebx
00CE1251 33 DB xor ebx,ebx
00CE1253 F7 E3 mul eax,ebx
00CE1255 43 inc ebx
00CE1256 83 C1 04 add ecx,4
00CE1259 03 D8 add ebx,eax
00CE125B 93 xchg eax,ebx
00CE125C 92 xchg eax,edx
00CE125D E2 FA loop myloop (0CE1259h)
00CE125F 5B pop ebx
00CE1260 C3 ret
It is 0-indexed. The initialization is conveniently achieved by calculating eax * 0. The 128-bit result is 0, and it goes in edx:eax.
At the beginning of each iteration, the order of the registers is ebx, eax, edx. I had to choose the right order to take advantage of the encoding for the xchg eax
instruction - 1 byte.
I had to add 4 to the loop counter in order to let the output reach eax
, which holds the function's return value in the fastcall
convention.
I could use some other calling convention, which doesn't require saving and restoring ebx
, but fastcall
is fun anyway :)
$endgroup$
$begingroup$
I love to see machine code answers on PP&CG! +1
$endgroup$
– Tau
23 hours ago
add a comment |
$begingroup$
Japt -N
, 12 bytes
<3ªßUµ2 +ß´U
Try it
$endgroup$
$begingroup$
Looks like 12 is the best we can do :
$endgroup$
– Shaggy
2 days ago
$begingroup$
I stand corrected!
$endgroup$
– Shaggy
14 hours ago
add a comment |
$begingroup$
Pyth, 16 bytes
L?<b3!b+y-b2y-b3
This defines the function y
. Try it here!
Here's a more fun solution, though it's 9 bytes longer; bytes could be shaved though.
+l{sa.pMf.Am&>d2%d2T./QY!
This uses the definition given by David Callan on the OEIS page: "a(n) = number of compositions of n into parts that are odd and >= 3." Try it here! It takes input directly instead of defining a function.
$endgroup$
$begingroup$
y-b2y-b3
could maybe be refactored with either bifurcate orL
? Though declaring an array of 2 elements is costly.yL-Lb2,3
is longer :(
$endgroup$
– Ven
yesterday
$begingroup$
@Ven I was able to replace+y-b2y-b3
withsmy-bdhB2
which is the same amount of bytes;hB2
results in the array[2, 3]
$endgroup$
– RK.
yesterday
$begingroup$
Well done onhB2
. Too bad it's the same byte count.
$endgroup$
– Ven
yesterday
$begingroup$
Yeah, though I wonder if there's some way to get rid of thed
in the map.
$endgroup$
– RK.
yesterday
add a comment |
$begingroup$
05AB1E, 8 bytes
1Ð)λ£₂₃+
Try it online!
Bear with me, I haven't golfed in a while. I wonder if there's a shorter substitute for 1Ð)
which works in this case (I've tried 1D)
, 3Å1
etc. but none of them save bytes). Outputs the first $n$ terms of the sequence. Or, without the £
, it would output an infinite stream of the terms of the sequence.
How?
1Ð)λ£₂₃+ | Full program.
1Ð) | Initialize the stack with [1, 1, 1].
λ | Begin the recursive generation of a list: Starting from some base case,
| this command generates an infinite list with the pattern function given.
£ | Flag for λ. Instead of outputting an infinite stream, only print the first n.
₂₃+ | Add a(n-2) and a(n-3).
$endgroup$
$begingroup$
I don't think1Ð)
can be 2 bytes tbh. I can think of six different 3-bytes alternatives, but no 2-byters.
$endgroup$
– Kevin Cruijssen
yesterday
add a comment |
$begingroup$
Java, 41 bytes
Can't use a lambda (runtime error). Port of this Javascript answer
int f(int n){return n<3?1:f(n-2)+f(n-3);}
TIO
$endgroup$
$begingroup$
I think you are missing some requirements: Have a look at my modification here.
$endgroup$
– Shaq
15 hours ago
$begingroup$
Please disregard Shaq's comment: your answer is correct and is the shortest Java answer possible (as of Java 12).
$endgroup$
– Olivier Grégoire
11 hours ago
$begingroup$
Ok then. I'm not sure what I "missed" but ok. Edit: nvm I read the JS answer.
$endgroup$
– Benjamin Urquhart
6 hours ago
add a comment |
$begingroup$
R + pryr, 38 36 bytes
Zero-indexed recursive function.
f=pryr::f(`if`(n<3,1,f(n-2)+f(n-3)))
Try it online!
Thanks to @Giuseppe for pointing out two obviously needless bytes.
$endgroup$
2
$begingroup$
If you're going to be usingpryr
, the language should beR + pryr
and this can be 36 bytes
$endgroup$
– Giuseppe
yesterday
$begingroup$
@Giuseppe thanks! Updated now.
$endgroup$
– rturnbull
yesterday
add a comment |
$begingroup$
JavaScript (ES6), 23 bytes
Implements the recursive definition of A000931, but with $a(0)=a(1)=a(2)=1$, as specified in the challenge.
Returns the $N$th term, 0-indexed.
f=n=>n<3||f(n-2)+f(n-3)
Try it online!
$endgroup$
$begingroup$
I don't think it's reasonable to say that returningtrue
is the same as returning1
if the rest of the output is numbers.
$endgroup$
– Nit
yesterday
1
$begingroup$
@Nit Relevant meta post.
$endgroup$
– Arnauld
yesterday
$begingroup$
I think you are missing some requirements: Have a look at my modification (version in Java) here.
$endgroup$
– Shaq
15 hours ago
$begingroup$
@Shaq The challenge clearly specifies that the first three terms of the sequence are all 1. So, it's not the sequence defined in A000931 (but the formula is the same).
$endgroup$
– Arnauld
15 hours ago
$begingroup$
@Arnauld yep I can see it now. Sorry!
$endgroup$
– Shaq
14 hours ago
add a comment |
$begingroup$
C (clang), 41 bytes
int a(int i){return i<3?1:a(i-2)+a(i-3);}
Try it online!
New contributor
$endgroup$
1
$begingroup$
Welcome to PPCG :)
$endgroup$
– Shaggy
10 hours ago
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 34 bytes
int f(int g)=>g<3?1:f(g-2)+f(g-3);
Try it online!
$endgroup$
add a comment |
$begingroup$
TI-BASIC (TI-84), 34 bytes
[[0,1,0][0,0,1][1,1,0]]^(Ans+5:Ans(1,1
0-indexed $N$th term of the sequence.
Input is in Ans
.
Output is in Ans
and is automatically printed out.
I figured that enough time had passed, plus multiple answers had been posted, of which there were many which out-golfed this answer.
Example:
0
0
prgmCDGFD
1
9
9
prgmCDGFD
9
16
16
prgmCDGFD
65
Explanation:
[[0,1,0][0,0,1][1,1,0]]^(Ans+5:Ans(1,1 ;full program (example input: 6)
[[0,1,0][0,0,1][1,1,0]] ;generate the following matrix:
; [0 1 0]
; [0 0 1]
; [1 1 0]
^(Ans+5 ;then raise it to the power of: input + 5
; [4 7 5]
; [5 9 7]
; [7 12 9]
Ans(1,1 ;get the top-left index and leave it in "Ans"
;implicitly print Ans
$endgroup$
add a comment |
$begingroup$
Perl 5, 34 bytes
sub f{"@_"<3||f("@_"-2)+f("@_"-3)}
Try it online!
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 26 bytes
If[#<3,1,#0[#-2]+#0[#-3]]&
Try it online!
$endgroup$
add a comment |
$begingroup$
Pari/GP, 28 bytes
0-indexed.
f(n)=if(n<3,1,f(n-2)+f(n-3))
Try it online!
Pari/GP, 35 bytes
1-indexed.
n->Vec((1+x+O(x^n))/(1-x^2-x^3))[n]
Try it online!
The generating function of the sequence is $frac{1+x}{1-x^2-x^3}$.
$endgroup$
add a comment |
1 2
next
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%2f182797%2fpatience-young-padovan%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
38 Answers
38
active
oldest
votes
38 Answers
38
active
oldest
votes
active
oldest
votes
active
oldest
votes
1 2
next
$begingroup$
Jelly, 10 bytes
9s3’Ẓæ*³FṀ
Try it online!
1-indexed. Computes the largest element of: $$begin{bmatrix}0&0&1 \ 1&0&1 \ 0&1&0end{bmatrix}^n$$
where the binary matrix is conveniently computed as: $$begin{bmatrix}mathsf{isprime}(0)&mathsf{isprime}(1)&mathsf{isprime}(2) \ mathsf{isprime}(3)&mathsf{isprime}(4)&mathsf{isprime}(5) \ mathsf{isprime}(6)&mathsf{isprime}(7)&mathsf{isprime}(8)end{bmatrix}$$
(this is a total coincidence.)
9s3 [[1,2,3],[4,5,6],[7,8,9]] 9 split 3
’ [[0,1,2],[3,4,5],[6,7,8]] decrease
Ẓ [[0,0,1],[1,0,1],[0,1,0]] isprime
æ*³ [[0,0,1],[1,0,1],[0,1,0]]^n matrix power by input
FṀ flatten, maximum
$endgroup$
25
$begingroup$
this is clearly some kind of voodoo
$endgroup$
– Pureferret
yesterday
7
$begingroup$
This should be published.
$endgroup$
– YSC
yesterday
5
$begingroup$
@YSC It has already been published in A000931. I'd never have guess the primes trick:)
$endgroup$
– flawr
yesterday
$begingroup$
@flawr ho... I missed it
$endgroup$
– YSC
yesterday
1
$begingroup$
...make that "unless someone can golf two bytes off this one" :) (now that I have a 9 byter)
$endgroup$
– Jonathan Allan
yesterday
|
show 3 more comments
$begingroup$
Jelly, 10 bytes
9s3’Ẓæ*³FṀ
Try it online!
1-indexed. Computes the largest element of: $$begin{bmatrix}0&0&1 \ 1&0&1 \ 0&1&0end{bmatrix}^n$$
where the binary matrix is conveniently computed as: $$begin{bmatrix}mathsf{isprime}(0)&mathsf{isprime}(1)&mathsf{isprime}(2) \ mathsf{isprime}(3)&mathsf{isprime}(4)&mathsf{isprime}(5) \ mathsf{isprime}(6)&mathsf{isprime}(7)&mathsf{isprime}(8)end{bmatrix}$$
(this is a total coincidence.)
9s3 [[1,2,3],[4,5,6],[7,8,9]] 9 split 3
’ [[0,1,2],[3,4,5],[6,7,8]] decrease
Ẓ [[0,0,1],[1,0,1],[0,1,0]] isprime
æ*³ [[0,0,1],[1,0,1],[0,1,0]]^n matrix power by input
FṀ flatten, maximum
$endgroup$
25
$begingroup$
this is clearly some kind of voodoo
$endgroup$
– Pureferret
yesterday
7
$begingroup$
This should be published.
$endgroup$
– YSC
yesterday
5
$begingroup$
@YSC It has already been published in A000931. I'd never have guess the primes trick:)
$endgroup$
– flawr
yesterday
$begingroup$
@flawr ho... I missed it
$endgroup$
– YSC
yesterday
1
$begingroup$
...make that "unless someone can golf two bytes off this one" :) (now that I have a 9 byter)
$endgroup$
– Jonathan Allan
yesterday
|
show 3 more comments
$begingroup$
Jelly, 10 bytes
9s3’Ẓæ*³FṀ
Try it online!
1-indexed. Computes the largest element of: $$begin{bmatrix}0&0&1 \ 1&0&1 \ 0&1&0end{bmatrix}^n$$
where the binary matrix is conveniently computed as: $$begin{bmatrix}mathsf{isprime}(0)&mathsf{isprime}(1)&mathsf{isprime}(2) \ mathsf{isprime}(3)&mathsf{isprime}(4)&mathsf{isprime}(5) \ mathsf{isprime}(6)&mathsf{isprime}(7)&mathsf{isprime}(8)end{bmatrix}$$
(this is a total coincidence.)
9s3 [[1,2,3],[4,5,6],[7,8,9]] 9 split 3
’ [[0,1,2],[3,4,5],[6,7,8]] decrease
Ẓ [[0,0,1],[1,0,1],[0,1,0]] isprime
æ*³ [[0,0,1],[1,0,1],[0,1,0]]^n matrix power by input
FṀ flatten, maximum
$endgroup$
Jelly, 10 bytes
9s3’Ẓæ*³FṀ
Try it online!
1-indexed. Computes the largest element of: $$begin{bmatrix}0&0&1 \ 1&0&1 \ 0&1&0end{bmatrix}^n$$
where the binary matrix is conveniently computed as: $$begin{bmatrix}mathsf{isprime}(0)&mathsf{isprime}(1)&mathsf{isprime}(2) \ mathsf{isprime}(3)&mathsf{isprime}(4)&mathsf{isprime}(5) \ mathsf{isprime}(6)&mathsf{isprime}(7)&mathsf{isprime}(8)end{bmatrix}$$
(this is a total coincidence.)
9s3 [[1,2,3],[4,5,6],[7,8,9]] 9 split 3
’ [[0,1,2],[3,4,5],[6,7,8]] decrease
Ẓ [[0,0,1],[1,0,1],[0,1,0]] isprime
æ*³ [[0,0,1],[1,0,1],[0,1,0]]^n matrix power by input
FṀ flatten, maximum
answered 2 days ago
LynnLynn
50.7k898233
50.7k898233
25
$begingroup$
this is clearly some kind of voodoo
$endgroup$
– Pureferret
yesterday
7
$begingroup$
This should be published.
$endgroup$
– YSC
yesterday
5
$begingroup$
@YSC It has already been published in A000931. I'd never have guess the primes trick:)
$endgroup$
– flawr
yesterday
$begingroup$
@flawr ho... I missed it
$endgroup$
– YSC
yesterday
1
$begingroup$
...make that "unless someone can golf two bytes off this one" :) (now that I have a 9 byter)
$endgroup$
– Jonathan Allan
yesterday
|
show 3 more comments
25
$begingroup$
this is clearly some kind of voodoo
$endgroup$
– Pureferret
yesterday
7
$begingroup$
This should be published.
$endgroup$
– YSC
yesterday
5
$begingroup$
@YSC It has already been published in A000931. I'd never have guess the primes trick:)
$endgroup$
– flawr
yesterday
$begingroup$
@flawr ho... I missed it
$endgroup$
– YSC
yesterday
1
$begingroup$
...make that "unless someone can golf two bytes off this one" :) (now that I have a 9 byter)
$endgroup$
– Jonathan Allan
yesterday
25
25
$begingroup$
this is clearly some kind of voodoo
$endgroup$
– Pureferret
yesterday
$begingroup$
this is clearly some kind of voodoo
$endgroup$
– Pureferret
yesterday
7
7
$begingroup$
This should be published.
$endgroup$
– YSC
yesterday
$begingroup$
This should be published.
$endgroup$
– YSC
yesterday
5
5
$begingroup$
@YSC It has already been published in A000931. I'd never have guess the primes trick:)
$endgroup$
– flawr
yesterday
$begingroup$
@YSC It has already been published in A000931. I'd never have guess the primes trick:)
$endgroup$
– flawr
yesterday
$begingroup$
@flawr ho... I missed it
$endgroup$
– YSC
yesterday
$begingroup$
@flawr ho... I missed it
$endgroup$
– YSC
yesterday
1
1
$begingroup$
...make that "unless someone can golf two bytes off this one" :) (now that I have a 9 byter)
$endgroup$
– Jonathan Allan
yesterday
$begingroup$
...make that "unless someone can golf two bytes off this one" :) (now that I have a 9 byter)
$endgroup$
– Jonathan Allan
yesterday
|
show 3 more comments
$begingroup$
Oasis, 5 bytes
nth term 0-indexed
cd+1V
Try it online!
Explanation
1V # a(0) = 1
# a(1) = 1
# a(2) = 1
# a(n) =
c # a(n-2)
+ # +
d # a(n-3)
$endgroup$
add a comment |
$begingroup$
Oasis, 5 bytes
nth term 0-indexed
cd+1V
Try it online!
Explanation
1V # a(0) = 1
# a(1) = 1
# a(2) = 1
# a(n) =
c # a(n-2)
+ # +
d # a(n-3)
$endgroup$
add a comment |
$begingroup$
Oasis, 5 bytes
nth term 0-indexed
cd+1V
Try it online!
Explanation
1V # a(0) = 1
# a(1) = 1
# a(2) = 1
# a(n) =
c # a(n-2)
+ # +
d # a(n-3)
$endgroup$
Oasis, 5 bytes
nth term 0-indexed
cd+1V
Try it online!
Explanation
1V # a(0) = 1
# a(1) = 1
# a(2) = 1
# a(n) =
c # a(n-2)
+ # +
d # a(n-3)
answered 2 days ago
EmignaEmigna
47.8k433145
47.8k433145
add a comment |
add a comment |
$begingroup$
Jelly, 10 9 8 bytes
ŻṚm2Jc$S
A monadic Link accepting n
(0-indexed) which yields P(n)
.
Try it online!
How?
Implements $P(n) = sum_{i=0}^{lfloorfrac{n}2rfloor}binom{i+1}{n-2i}$
ŻṚm2Jc$S - Link: integer, n e.g. 20
Ż - zero range [0, 1, 2, 3, 4, ..., 19, 20]
Ṛ - reverse [20, 19, ..., 4, 3, 2, 1, 0]
m2 - modulo-slice with 2 [20, 18, 16, 14, 12, 10, 8, 6, 4, 2, 0] <- n-2i
$ - last two links as a monad:
J - range of length [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] <- i+1
c - left-choose-right [ 0, 0, 0, 0, 0, 0, 0, 28,126, 45, 1]
S - sum 200
And here is a "twofer"
...a totally different method also for 8 bytes (this one is 1-indexed, but much slower):
3ḊṗRẎ§ċ‘ - Link: n
3Ḋ - 3 dequeued = [2,3]
R - range = [1,2,3,...,n]
ṗ - Cartesian power [[[2],[3]],[[2,2],[2,3],[3,2],[3,3]],[[2,2,2],...],...]
Ẏ - tighten [[2],[3],[2,2],[2,3],[3,2],[3,3],[2,2,2],...]
§ - sums [ 2, 3, 4, 5, 5, 6, 6,...]
‘ - increment n+1
ċ - count occurrences P(n)
$endgroup$
add a comment |
$begingroup$
Jelly, 10 9 8 bytes
ŻṚm2Jc$S
A monadic Link accepting n
(0-indexed) which yields P(n)
.
Try it online!
How?
Implements $P(n) = sum_{i=0}^{lfloorfrac{n}2rfloor}binom{i+1}{n-2i}$
ŻṚm2Jc$S - Link: integer, n e.g. 20
Ż - zero range [0, 1, 2, 3, 4, ..., 19, 20]
Ṛ - reverse [20, 19, ..., 4, 3, 2, 1, 0]
m2 - modulo-slice with 2 [20, 18, 16, 14, 12, 10, 8, 6, 4, 2, 0] <- n-2i
$ - last two links as a monad:
J - range of length [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] <- i+1
c - left-choose-right [ 0, 0, 0, 0, 0, 0, 0, 28,126, 45, 1]
S - sum 200
And here is a "twofer"
...a totally different method also for 8 bytes (this one is 1-indexed, but much slower):
3ḊṗRẎ§ċ‘ - Link: n
3Ḋ - 3 dequeued = [2,3]
R - range = [1,2,3,...,n]
ṗ - Cartesian power [[[2],[3]],[[2,2],[2,3],[3,2],[3,3]],[[2,2,2],...],...]
Ẏ - tighten [[2],[3],[2,2],[2,3],[3,2],[3,3],[2,2,2],...]
§ - sums [ 2, 3, 4, 5, 5, 6, 6,...]
‘ - increment n+1
ċ - count occurrences P(n)
$endgroup$
add a comment |
$begingroup$
Jelly, 10 9 8 bytes
ŻṚm2Jc$S
A monadic Link accepting n
(0-indexed) which yields P(n)
.
Try it online!
How?
Implements $P(n) = sum_{i=0}^{lfloorfrac{n}2rfloor}binom{i+1}{n-2i}$
ŻṚm2Jc$S - Link: integer, n e.g. 20
Ż - zero range [0, 1, 2, 3, 4, ..., 19, 20]
Ṛ - reverse [20, 19, ..., 4, 3, 2, 1, 0]
m2 - modulo-slice with 2 [20, 18, 16, 14, 12, 10, 8, 6, 4, 2, 0] <- n-2i
$ - last two links as a monad:
J - range of length [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] <- i+1
c - left-choose-right [ 0, 0, 0, 0, 0, 0, 0, 28,126, 45, 1]
S - sum 200
And here is a "twofer"
...a totally different method also for 8 bytes (this one is 1-indexed, but much slower):
3ḊṗRẎ§ċ‘ - Link: n
3Ḋ - 3 dequeued = [2,3]
R - range = [1,2,3,...,n]
ṗ - Cartesian power [[[2],[3]],[[2,2],[2,3],[3,2],[3,3]],[[2,2,2],...],...]
Ẏ - tighten [[2],[3],[2,2],[2,3],[3,2],[3,3],[2,2,2],...]
§ - sums [ 2, 3, 4, 5, 5, 6, 6,...]
‘ - increment n+1
ċ - count occurrences P(n)
$endgroup$
Jelly, 10 9 8 bytes
ŻṚm2Jc$S
A monadic Link accepting n
(0-indexed) which yields P(n)
.
Try it online!
How?
Implements $P(n) = sum_{i=0}^{lfloorfrac{n}2rfloor}binom{i+1}{n-2i}$
ŻṚm2Jc$S - Link: integer, n e.g. 20
Ż - zero range [0, 1, 2, 3, 4, ..., 19, 20]
Ṛ - reverse [20, 19, ..., 4, 3, 2, 1, 0]
m2 - modulo-slice with 2 [20, 18, 16, 14, 12, 10, 8, 6, 4, 2, 0] <- n-2i
$ - last two links as a monad:
J - range of length [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] <- i+1
c - left-choose-right [ 0, 0, 0, 0, 0, 0, 0, 28,126, 45, 1]
S - sum 200
And here is a "twofer"
...a totally different method also for 8 bytes (this one is 1-indexed, but much slower):
3ḊṗRẎ§ċ‘ - Link: n
3Ḋ - 3 dequeued = [2,3]
R - range = [1,2,3,...,n]
ṗ - Cartesian power [[[2],[3]],[[2,2],[2,3],[3,2],[3,3]],[[2,2,2],...],...]
Ẏ - tighten [[2],[3],[2,2],[2,3],[3,2],[3,3],[2,2,2],...]
§ - sums [ 2, 3, 4, 5, 5, 6, 6,...]
‘ - increment n+1
ċ - count occurrences P(n)
edited yesterday
answered 2 days ago
Jonathan AllanJonathan Allan
54k536174
54k536174
add a comment |
add a comment |
$begingroup$
Haskell, 26 bytes
(l!!)
l=1:1:1:2:scanl(+)2l
Try it online! Outputs the n'th term zero-indexed.
I thought that the "obvious" recursive solution below would be unbeatable, but then I found this. It's similar to the classic golfy expression l=1:scanl(+)1l
for the infinite Fibonacci list, but here the difference between adjacent elements is the term 4 positions back. We can more directly write l=1:1:zipWith(+)l(0:l)
, but that's longer.
If this challenge allowed infinite list output, we could cut the first line and have 20 bytes.
27 bytes
f n|n<3=1|1>0=f(n-2)+f(n-3)
Try it online!
$endgroup$
add a comment |
$begingroup$
Haskell, 26 bytes
(l!!)
l=1:1:1:2:scanl(+)2l
Try it online! Outputs the n'th term zero-indexed.
I thought that the "obvious" recursive solution below would be unbeatable, but then I found this. It's similar to the classic golfy expression l=1:scanl(+)1l
for the infinite Fibonacci list, but here the difference between adjacent elements is the term 4 positions back. We can more directly write l=1:1:zipWith(+)l(0:l)
, but that's longer.
If this challenge allowed infinite list output, we could cut the first line and have 20 bytes.
27 bytes
f n|n<3=1|1>0=f(n-2)+f(n-3)
Try it online!
$endgroup$
add a comment |
$begingroup$
Haskell, 26 bytes
(l!!)
l=1:1:1:2:scanl(+)2l
Try it online! Outputs the n'th term zero-indexed.
I thought that the "obvious" recursive solution below would be unbeatable, but then I found this. It's similar to the classic golfy expression l=1:scanl(+)1l
for the infinite Fibonacci list, but here the difference between adjacent elements is the term 4 positions back. We can more directly write l=1:1:zipWith(+)l(0:l)
, but that's longer.
If this challenge allowed infinite list output, we could cut the first line and have 20 bytes.
27 bytes
f n|n<3=1|1>0=f(n-2)+f(n-3)
Try it online!
$endgroup$
Haskell, 26 bytes
(l!!)
l=1:1:1:2:scanl(+)2l
Try it online! Outputs the n'th term zero-indexed.
I thought that the "obvious" recursive solution below would be unbeatable, but then I found this. It's similar to the classic golfy expression l=1:scanl(+)1l
for the infinite Fibonacci list, but here the difference between adjacent elements is the term 4 positions back. We can more directly write l=1:1:zipWith(+)l(0:l)
, but that's longer.
If this challenge allowed infinite list output, we could cut the first line and have 20 bytes.
27 bytes
f n|n<3=1|1>0=f(n-2)+f(n-3)
Try it online!
answered 2 days ago
xnorxnor
93.9k18192450
93.9k18192450
add a comment |
add a comment |
$begingroup$
Python 2, 30 bytes
f=lambda n:n<3or f(n-2)+f(n-3)
Try it online!
Returns the n'th term zero indexed. Outputs True
for 1.
$endgroup$
add a comment |
$begingroup$
Python 2, 30 bytes
f=lambda n:n<3or f(n-2)+f(n-3)
Try it online!
Returns the n'th term zero indexed. Outputs True
for 1.
$endgroup$
add a comment |
$begingroup$
Python 2, 30 bytes
f=lambda n:n<3or f(n-2)+f(n-3)
Try it online!
Returns the n'th term zero indexed. Outputs True
for 1.
$endgroup$
Python 2, 30 bytes
f=lambda n:n<3or f(n-2)+f(n-3)
Try it online!
Returns the n'th term zero indexed. Outputs True
for 1.
edited 2 days ago
answered 2 days ago
xnorxnor
93.9k18192450
93.9k18192450
add a comment |
add a comment |
$begingroup$
Wolfram Language (Mathematica), 33 bytes
a@0=a@1=a@2=1;a@n_:=a[n-2]+a[n-3]
1-indexed, returns the nth term
Try it online!
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 33 bytes
a@0=a@1=a@2=1;a@n_:=a[n-2]+a[n-3]
1-indexed, returns the nth term
Try it online!
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 33 bytes
a@0=a@1=a@2=1;a@n_:=a[n-2]+a[n-3]
1-indexed, returns the nth term
Try it online!
$endgroup$
Wolfram Language (Mathematica), 33 bytes
a@0=a@1=a@2=1;a@n_:=a[n-2]+a[n-3]
1-indexed, returns the nth term
Try it online!
answered 2 days ago
J42161217J42161217
13.9k21353
13.9k21353
add a comment |
add a comment |
$begingroup$
Octave / MATLAB, 35 33 bytes
@(n)[1 filter(1,'cbaa'-98,2:n<5)]
Outputs the first n terms.
Try it online!
How it works
Anonymous function that implements a recursive filter.
'cbaa'-98
is a shorter form to produce [1 0 -1 -1]
.
2:n<5
is a shorter form to produce [1 1 1 0 0 ··· 0]
(n−1 terms).
filter(1,[1 0 -1 -1],[1 1 1 0 0 ··· 0])
passes the input [1 1 1 0 0 ··· 0]
through a discrete-time filter defined by a transfer function with numerator coefficient 1
and denominator coefficients [1 0 -1 -1]
.
$endgroup$
add a comment |
$begingroup$
Octave / MATLAB, 35 33 bytes
@(n)[1 filter(1,'cbaa'-98,2:n<5)]
Outputs the first n terms.
Try it online!
How it works
Anonymous function that implements a recursive filter.
'cbaa'-98
is a shorter form to produce [1 0 -1 -1]
.
2:n<5
is a shorter form to produce [1 1 1 0 0 ··· 0]
(n−1 terms).
filter(1,[1 0 -1 -1],[1 1 1 0 0 ··· 0])
passes the input [1 1 1 0 0 ··· 0]
through a discrete-time filter defined by a transfer function with numerator coefficient 1
and denominator coefficients [1 0 -1 -1]
.
$endgroup$
add a comment |
$begingroup$
Octave / MATLAB, 35 33 bytes
@(n)[1 filter(1,'cbaa'-98,2:n<5)]
Outputs the first n terms.
Try it online!
How it works
Anonymous function that implements a recursive filter.
'cbaa'-98
is a shorter form to produce [1 0 -1 -1]
.
2:n<5
is a shorter form to produce [1 1 1 0 0 ··· 0]
(n−1 terms).
filter(1,[1 0 -1 -1],[1 1 1 0 0 ··· 0])
passes the input [1 1 1 0 0 ··· 0]
through a discrete-time filter defined by a transfer function with numerator coefficient 1
and denominator coefficients [1 0 -1 -1]
.
$endgroup$
Octave / MATLAB, 35 33 bytes
@(n)[1 filter(1,'cbaa'-98,2:n<5)]
Outputs the first n terms.
Try it online!
How it works
Anonymous function that implements a recursive filter.
'cbaa'-98
is a shorter form to produce [1 0 -1 -1]
.
2:n<5
is a shorter form to produce [1 1 1 0 0 ··· 0]
(n−1 terms).
filter(1,[1 0 -1 -1],[1 1 1 0 0 ··· 0])
passes the input [1 1 1 0 0 ··· 0]
through a discrete-time filter defined by a transfer function with numerator coefficient 1
and denominator coefficients [1 0 -1 -1]
.
edited yesterday
answered yesterday
Luis MendoLuis Mendo
75.3k889292
75.3k889292
add a comment |
add a comment |
$begingroup$
Retina, 47 42 bytes
K`0¶1¶0
"$+"+`.+¶(.+)¶.+$
$&¶$.(*_$1*
6,G`
Try it online! Outputs the first n
terms on separate lines. Explanation:
K`0¶1¶0
Replace the input with the terms for -2
, -1
and 0
.
"$+"+`.+¶(.+)¶.+$
$&¶$.(*_$1*
Generate the next n
terms using the recurrence relation. *_
here is short for $&*_
which converts the (first) number in the match to unary, while $1*
is short for $1*_
which converts the middle number to unary. The $.(
returns the decimal sum of its unary arguments, i.e. the sum of the first and middle numbers.
6,G`
Discard the first six characters, i.e. the first three lines.
$endgroup$
add a comment |
$begingroup$
Retina, 47 42 bytes
K`0¶1¶0
"$+"+`.+¶(.+)¶.+$
$&¶$.(*_$1*
6,G`
Try it online! Outputs the first n
terms on separate lines. Explanation:
K`0¶1¶0
Replace the input with the terms for -2
, -1
and 0
.
"$+"+`.+¶(.+)¶.+$
$&¶$.(*_$1*
Generate the next n
terms using the recurrence relation. *_
here is short for $&*_
which converts the (first) number in the match to unary, while $1*
is short for $1*_
which converts the middle number to unary. The $.(
returns the decimal sum of its unary arguments, i.e. the sum of the first and middle numbers.
6,G`
Discard the first six characters, i.e. the first three lines.
$endgroup$
add a comment |
$begingroup$
Retina, 47 42 bytes
K`0¶1¶0
"$+"+`.+¶(.+)¶.+$
$&¶$.(*_$1*
6,G`
Try it online! Outputs the first n
terms on separate lines. Explanation:
K`0¶1¶0
Replace the input with the terms for -2
, -1
and 0
.
"$+"+`.+¶(.+)¶.+$
$&¶$.(*_$1*
Generate the next n
terms using the recurrence relation. *_
here is short for $&*_
which converts the (first) number in the match to unary, while $1*
is short for $1*_
which converts the middle number to unary. The $.(
returns the decimal sum of its unary arguments, i.e. the sum of the first and middle numbers.
6,G`
Discard the first six characters, i.e. the first three lines.
$endgroup$
Retina, 47 42 bytes
K`0¶1¶0
"$+"+`.+¶(.+)¶.+$
$&¶$.(*_$1*
6,G`
Try it online! Outputs the first n
terms on separate lines. Explanation:
K`0¶1¶0
Replace the input with the terms for -2
, -1
and 0
.
"$+"+`.+¶(.+)¶.+$
$&¶$.(*_$1*
Generate the next n
terms using the recurrence relation. *_
here is short for $&*_
which converts the (first) number in the match to unary, while $1*
is short for $1*_
which converts the middle number to unary. The $.(
returns the decimal sum of its unary arguments, i.e. the sum of the first and middle numbers.
6,G`
Discard the first six characters, i.e. the first three lines.
edited 2 days ago
answered 2 days ago
NeilNeil
82.6k745179
82.6k745179
add a comment |
add a comment |
$begingroup$
Cubix, 20 bytes
This is 0 indexed and outputs the Nth term
;@UOI010+p?/sqq;W.(
Try it online!
Wraps onto a cube with side length 2
; @
U O
I 0 1 0 + p ? /
s q q ; W . (
. .
. .
Watch it run
I010
- Initiates the stack
+p?
- Adds the top of stack, pulls the counter from the bottom of stack and tests
/;UO@
- If counter is 0, reflect onto top face, remove TOS, u-turn, output and halt
(sqq;W
- If counter is positive, reflect, decrement counter, swap TOS, push top to bottom twice, remove TOS and shift lane back into the main loop.
$endgroup$
add a comment |
$begingroup$
Cubix, 20 bytes
This is 0 indexed and outputs the Nth term
;@UOI010+p?/sqq;W.(
Try it online!
Wraps onto a cube with side length 2
; @
U O
I 0 1 0 + p ? /
s q q ; W . (
. .
. .
Watch it run
I010
- Initiates the stack
+p?
- Adds the top of stack, pulls the counter from the bottom of stack and tests
/;UO@
- If counter is 0, reflect onto top face, remove TOS, u-turn, output and halt
(sqq;W
- If counter is positive, reflect, decrement counter, swap TOS, push top to bottom twice, remove TOS and shift lane back into the main loop.
$endgroup$
add a comment |
$begingroup$
Cubix, 20 bytes
This is 0 indexed and outputs the Nth term
;@UOI010+p?/sqq;W.(
Try it online!
Wraps onto a cube with side length 2
; @
U O
I 0 1 0 + p ? /
s q q ; W . (
. .
. .
Watch it run
I010
- Initiates the stack
+p?
- Adds the top of stack, pulls the counter from the bottom of stack and tests
/;UO@
- If counter is 0, reflect onto top face, remove TOS, u-turn, output and halt
(sqq;W
- If counter is positive, reflect, decrement counter, swap TOS, push top to bottom twice, remove TOS and shift lane back into the main loop.
$endgroup$
Cubix, 20 bytes
This is 0 indexed and outputs the Nth term
;@UOI010+p?/sqq;W.(
Try it online!
Wraps onto a cube with side length 2
; @
U O
I 0 1 0 + p ? /
s q q ; W . (
. .
. .
Watch it run
I010
- Initiates the stack
+p?
- Adds the top of stack, pulls the counter from the bottom of stack and tests
/;UO@
- If counter is 0, reflect onto top face, remove TOS, u-turn, output and halt
(sqq;W
- If counter is positive, reflect, decrement counter, swap TOS, push top to bottom twice, remove TOS and shift lane back into the main loop.
answered 2 days ago
MickyTMickyT
10.3k21637
10.3k21637
add a comment |
add a comment |
$begingroup$
J, 23 bytes
-1 byte thanks to ngn and Galen
closed form, 26 bytes
0.5<.@+1.04535%~1.32472^<:
Try it online!
iterative, 23 bytes
(],1#._2 _3{ ::1:])^:[#
Try it online!
$endgroup$
1
$begingroup$
Another 24-byte solution (boring) : (1#.2 3$:@-~])`1:@.(3&>) Try it online!
$endgroup$
– Galen Ivanov
yesterday
$begingroup$
23 bytes thanks to ngn1:
->#
: Try it online!
$endgroup$
– Galen Ivanov
yesterday
$begingroup$
@GalenIvanov tyvm, that's a great trick.
$endgroup$
– Jonah
yesterday
add a comment |
$begingroup$
J, 23 bytes
-1 byte thanks to ngn and Galen
closed form, 26 bytes
0.5<.@+1.04535%~1.32472^<:
Try it online!
iterative, 23 bytes
(],1#._2 _3{ ::1:])^:[#
Try it online!
$endgroup$
1
$begingroup$
Another 24-byte solution (boring) : (1#.2 3$:@-~])`1:@.(3&>) Try it online!
$endgroup$
– Galen Ivanov
yesterday
$begingroup$
23 bytes thanks to ngn1:
->#
: Try it online!
$endgroup$
– Galen Ivanov
yesterday
$begingroup$
@GalenIvanov tyvm, that's a great trick.
$endgroup$
– Jonah
yesterday
add a comment |
$begingroup$
J, 23 bytes
-1 byte thanks to ngn and Galen
closed form, 26 bytes
0.5<.@+1.04535%~1.32472^<:
Try it online!
iterative, 23 bytes
(],1#._2 _3{ ::1:])^:[#
Try it online!
$endgroup$
J, 23 bytes
-1 byte thanks to ngn and Galen
closed form, 26 bytes
0.5<.@+1.04535%~1.32472^<:
Try it online!
iterative, 23 bytes
(],1#._2 _3{ ::1:])^:[#
Try it online!
edited yesterday
answered 2 days ago
JonahJonah
2,6611017
2,6611017
1
$begingroup$
Another 24-byte solution (boring) : (1#.2 3$:@-~])`1:@.(3&>) Try it online!
$endgroup$
– Galen Ivanov
yesterday
$begingroup$
23 bytes thanks to ngn1:
->#
: Try it online!
$endgroup$
– Galen Ivanov
yesterday
$begingroup$
@GalenIvanov tyvm, that's a great trick.
$endgroup$
– Jonah
yesterday
add a comment |
1
$begingroup$
Another 24-byte solution (boring) : (1#.2 3$:@-~])`1:@.(3&>) Try it online!
$endgroup$
– Galen Ivanov
yesterday
$begingroup$
23 bytes thanks to ngn1:
->#
: Try it online!
$endgroup$
– Galen Ivanov
yesterday
$begingroup$
@GalenIvanov tyvm, that's a great trick.
$endgroup$
– Jonah
yesterday
1
1
$begingroup$
Another 24-byte solution (boring) : (1#.2 3$:@-~])`1:@.(3&>) Try it online!
$endgroup$
– Galen Ivanov
yesterday
$begingroup$
Another 24-byte solution (boring) : (1#.2 3$:@-~])`1:@.(3&>) Try it online!
$endgroup$
– Galen Ivanov
yesterday
$begingroup$
23 bytes thanks to ngn
1:
-> #
: Try it online!$endgroup$
– Galen Ivanov
yesterday
$begingroup$
23 bytes thanks to ngn
1:
-> #
: Try it online!$endgroup$
– Galen Ivanov
yesterday
$begingroup$
@GalenIvanov tyvm, that's a great trick.
$endgroup$
– Jonah
yesterday
$begingroup$
@GalenIvanov tyvm, that's a great trick.
$endgroup$
– Jonah
yesterday
add a comment |
$begingroup$
Python 2, 56 48 bytes
f=lambda n,a=1,b=1,c=1:n>2and f(n-1,b,c,a+b)or c
Try it online!
Returns nth value, 0-indexed.
$endgroup$
add a comment |
$begingroup$
Python 2, 56 48 bytes
f=lambda n,a=1,b=1,c=1:n>2and f(n-1,b,c,a+b)or c
Try it online!
Returns nth value, 0-indexed.
$endgroup$
add a comment |
$begingroup$
Python 2, 56 48 bytes
f=lambda n,a=1,b=1,c=1:n>2and f(n-1,b,c,a+b)or c
Try it online!
Returns nth value, 0-indexed.
$endgroup$
Python 2, 56 48 bytes
f=lambda n,a=1,b=1,c=1:n>2and f(n-1,b,c,a+b)or c
Try it online!
Returns nth value, 0-indexed.
answered 2 days ago
Chas BrownChas Brown
5,2191523
5,2191523
add a comment |
add a comment |
$begingroup$
Jelly, 11 bytes
5B+Ɲ2ị;Ʋ⁸¡Ḣ
Try it online!
0-indexed.
$endgroup$
add a comment |
$begingroup$
Jelly, 11 bytes
5B+Ɲ2ị;Ʋ⁸¡Ḣ
Try it online!
0-indexed.
$endgroup$
add a comment |
$begingroup$
Jelly, 11 bytes
5B+Ɲ2ị;Ʋ⁸¡Ḣ
Try it online!
0-indexed.
$endgroup$
Jelly, 11 bytes
5B+Ɲ2ị;Ʋ⁸¡Ḣ
Try it online!
0-indexed.
edited 2 days ago
answered 2 days ago
Erik the OutgolferErik the Outgolfer
33k429106
33k429106
add a comment |
add a comment |
$begingroup$
Perl 6, 24 bytes
{(1,1,1,*+*+!*...*)[$_]}
Try it online!
A pretty standard generated sequence, with each new element generated by the expression * + * + !*
. That adds the third-previous element, the second-previous element, and the logical negation of the previous element, which is always False
, which is numerically zero.
$endgroup$
$begingroup$
Why is this community wiki?
$endgroup$
– Jo King
2 days ago
$begingroup$
@JoKing Beats me. If I did it somehow, it wasn't on purpose.
$endgroup$
– Sean
yesterday
add a comment |
$begingroup$
Perl 6, 24 bytes
{(1,1,1,*+*+!*...*)[$_]}
Try it online!
A pretty standard generated sequence, with each new element generated by the expression * + * + !*
. That adds the third-previous element, the second-previous element, and the logical negation of the previous element, which is always False
, which is numerically zero.
$endgroup$
$begingroup$
Why is this community wiki?
$endgroup$
– Jo King
2 days ago
$begingroup$
@JoKing Beats me. If I did it somehow, it wasn't on purpose.
$endgroup$
– Sean
yesterday
add a comment |
$begingroup$
Perl 6, 24 bytes
{(1,1,1,*+*+!*...*)[$_]}
Try it online!
A pretty standard generated sequence, with each new element generated by the expression * + * + !*
. That adds the third-previous element, the second-previous element, and the logical negation of the previous element, which is always False
, which is numerically zero.
$endgroup$
Perl 6, 24 bytes
{(1,1,1,*+*+!*...*)[$_]}
Try it online!
A pretty standard generated sequence, with each new element generated by the expression * + * + !*
. That adds the third-previous element, the second-previous element, and the logical negation of the previous element, which is always False
, which is numerically zero.
answered 2 days ago
community wiki
Sean
$begingroup$
Why is this community wiki?
$endgroup$
– Jo King
2 days ago
$begingroup$
@JoKing Beats me. If I did it somehow, it wasn't on purpose.
$endgroup$
– Sean
yesterday
add a comment |
$begingroup$
Why is this community wiki?
$endgroup$
– Jo King
2 days ago
$begingroup$
@JoKing Beats me. If I did it somehow, it wasn't on purpose.
$endgroup$
– Sean
yesterday
$begingroup$
Why is this community wiki?
$endgroup$
– Jo King
2 days ago
$begingroup$
Why is this community wiki?
$endgroup$
– Jo King
2 days ago
$begingroup$
@JoKing Beats me. If I did it somehow, it wasn't on purpose.
$endgroup$
– Sean
yesterday
$begingroup$
@JoKing Beats me. If I did it somehow, it wasn't on purpose.
$endgroup$
– Sean
yesterday
add a comment |
$begingroup$
Lua 5.3, 49 48 bytes
function f(n)return n<4 and 1or f(n-2)+f(n-3)end
Try it online!
Vanilla Lua doesn't have coercion of booleans to strings (even tonumber(true)
returns nil
), so you have to use a pseudo-ternary operator. This version is 1-indexed, like all of Lua. The 1or
part has to be changed to 1 or
in Lua 5.1, which has a different way of lexing numbers.
$endgroup$
add a comment |
$begingroup$
Lua 5.3, 49 48 bytes
function f(n)return n<4 and 1or f(n-2)+f(n-3)end
Try it online!
Vanilla Lua doesn't have coercion of booleans to strings (even tonumber(true)
returns nil
), so you have to use a pseudo-ternary operator. This version is 1-indexed, like all of Lua. The 1or
part has to be changed to 1 or
in Lua 5.1, which has a different way of lexing numbers.
$endgroup$
add a comment |
$begingroup$
Lua 5.3, 49 48 bytes
function f(n)return n<4 and 1or f(n-2)+f(n-3)end
Try it online!
Vanilla Lua doesn't have coercion of booleans to strings (even tonumber(true)
returns nil
), so you have to use a pseudo-ternary operator. This version is 1-indexed, like all of Lua. The 1or
part has to be changed to 1 or
in Lua 5.1, which has a different way of lexing numbers.
$endgroup$
Lua 5.3, 49 48 bytes
function f(n)return n<4 and 1or f(n-2)+f(n-3)end
Try it online!
Vanilla Lua doesn't have coercion of booleans to strings (even tonumber(true)
returns nil
), so you have to use a pseudo-ternary operator. This version is 1-indexed, like all of Lua. The 1or
part has to be changed to 1 or
in Lua 5.1, which has a different way of lexing numbers.
edited yesterday
answered 2 days ago
cyclaministcyclaminist
1813
1813
add a comment |
add a comment |
$begingroup$
Ruby, 26 bytes
f=->n{n<3?1:f[n-2]+f[n-3]}
Try it online!
$endgroup$
add a comment |
$begingroup$
Ruby, 26 bytes
f=->n{n<3?1:f[n-2]+f[n-3]}
Try it online!
$endgroup$
add a comment |
$begingroup$
Ruby, 26 bytes
f=->n{n<3?1:f[n-2]+f[n-3]}
Try it online!
$endgroup$
Ruby, 26 bytes
f=->n{n<3?1:f[n-2]+f[n-3]}
Try it online!
answered yesterday
G BG B
8,2561429
8,2561429
add a comment |
add a comment |
$begingroup$
APL (Dyalog Unicode), 20 18 17 bytesSBCS
This code is 1-indexed. It's the same number of bytes to get n
items of the Padovan sequence, as you have to drop the last few extra members. It's also the same number of bytes to get 0-indexing.
Edit: -2 bytes thanks to ngn. -1 byte thanks to ngn
4⌷2(⊢,⍨2⌷+/)⍣⎕×⍳3
Try it online!
Explanation
4⌷2(⊢,⍨2⌷+/)⍣⎕×⍳3
⍺(. . . .)⍣⎕⍵ This format simply takes the input ⎕ and applies the function
inside the brackets (...) to its operands (here marked ⍵ and ⍺).
2(. . .+/)⍣⎕×⍳3 In this case, our ⍵, the left argument, is the array 1 1 1,
where we save our results as the function is repeatedly applied
and our ⍺, 2, is our right argument and is immediately applied to +/,
so that we have 2+/ which will return the pairwise sums of our array.
2⌷ We take the second pairwise sum, f(n-2) + f(n-3)
⊢,⍨ And add it to the head of our array.
4⌷ When we've finished adding Padovan numbers to the end of our list,
the n-th Padovan number (1-indexed) is the 4th member of that list,
and so, we implicitly return that.
$endgroup$
add a comment |
$begingroup$
APL (Dyalog Unicode), 20 18 17 bytesSBCS
This code is 1-indexed. It's the same number of bytes to get n
items of the Padovan sequence, as you have to drop the last few extra members. It's also the same number of bytes to get 0-indexing.
Edit: -2 bytes thanks to ngn. -1 byte thanks to ngn
4⌷2(⊢,⍨2⌷+/)⍣⎕×⍳3
Try it online!
Explanation
4⌷2(⊢,⍨2⌷+/)⍣⎕×⍳3
⍺(. . . .)⍣⎕⍵ This format simply takes the input ⎕ and applies the function
inside the brackets (...) to its operands (here marked ⍵ and ⍺).
2(. . .+/)⍣⎕×⍳3 In this case, our ⍵, the left argument, is the array 1 1 1,
where we save our results as the function is repeatedly applied
and our ⍺, 2, is our right argument and is immediately applied to +/,
so that we have 2+/ which will return the pairwise sums of our array.
2⌷ We take the second pairwise sum, f(n-2) + f(n-3)
⊢,⍨ And add it to the head of our array.
4⌷ When we've finished adding Padovan numbers to the end of our list,
the n-th Padovan number (1-indexed) is the 4th member of that list,
and so, we implicitly return that.
$endgroup$
add a comment |
$begingroup$
APL (Dyalog Unicode), 20 18 17 bytesSBCS
This code is 1-indexed. It's the same number of bytes to get n
items of the Padovan sequence, as you have to drop the last few extra members. It's also the same number of bytes to get 0-indexing.
Edit: -2 bytes thanks to ngn. -1 byte thanks to ngn
4⌷2(⊢,⍨2⌷+/)⍣⎕×⍳3
Try it online!
Explanation
4⌷2(⊢,⍨2⌷+/)⍣⎕×⍳3
⍺(. . . .)⍣⎕⍵ This format simply takes the input ⎕ and applies the function
inside the brackets (...) to its operands (here marked ⍵ and ⍺).
2(. . .+/)⍣⎕×⍳3 In this case, our ⍵, the left argument, is the array 1 1 1,
where we save our results as the function is repeatedly applied
and our ⍺, 2, is our right argument and is immediately applied to +/,
so that we have 2+/ which will return the pairwise sums of our array.
2⌷ We take the second pairwise sum, f(n-2) + f(n-3)
⊢,⍨ And add it to the head of our array.
4⌷ When we've finished adding Padovan numbers to the end of our list,
the n-th Padovan number (1-indexed) is the 4th member of that list,
and so, we implicitly return that.
$endgroup$
APL (Dyalog Unicode), 20 18 17 bytesSBCS
This code is 1-indexed. It's the same number of bytes to get n
items of the Padovan sequence, as you have to drop the last few extra members. It's also the same number of bytes to get 0-indexing.
Edit: -2 bytes thanks to ngn. -1 byte thanks to ngn
4⌷2(⊢,⍨2⌷+/)⍣⎕×⍳3
Try it online!
Explanation
4⌷2(⊢,⍨2⌷+/)⍣⎕×⍳3
⍺(. . . .)⍣⎕⍵ This format simply takes the input ⎕ and applies the function
inside the brackets (...) to its operands (here marked ⍵ and ⍺).
2(. . .+/)⍣⎕×⍳3 In this case, our ⍵, the left argument, is the array 1 1 1,
where we save our results as the function is repeatedly applied
and our ⍺, 2, is our right argument and is immediately applied to +/,
so that we have 2+/ which will return the pairwise sums of our array.
2⌷ We take the second pairwise sum, f(n-2) + f(n-3)
⊢,⍨ And add it to the head of our array.
4⌷ When we've finished adding Padovan numbers to the end of our list,
the n-th Padovan number (1-indexed) is the 4th member of that list,
and so, we implicitly return that.
edited yesterday
answered yesterday
Sherlock9Sherlock9
8,16411860
8,16411860
add a comment |
add a comment |
$begingroup$
K (ngn/k), 24 20 bytes
-4 bytes thanks to ngn!
{$[x<3;1;+/o'x-2 3]}
Try it online!
0-indexed, first N terms
$endgroup$
1
$begingroup$
f[x-2]+f[x-3]
->+/o'x-2 3
(o
is "recur")
$endgroup$
– ngn
yesterday
$begingroup$
@ngn Thanks! I tried it (without success) in J; it's elegant here.
$endgroup$
– Galen Ivanov
yesterday
$begingroup$
@ngn In fact here's one possibillity how it looks in J: (1#.2 3$:@-~])`1:@.(3&>)
$endgroup$
– Galen Ivanov
yesterday
$begingroup$
ah, right, base-1 decode is a train-friendly way to sum :)
$endgroup$
– ngn
yesterday
2
$begingroup$
1:
->#
in the j solution
$endgroup$
– ngn
yesterday
|
show 2 more comments
$begingroup$
K (ngn/k), 24 20 bytes
-4 bytes thanks to ngn!
{$[x<3;1;+/o'x-2 3]}
Try it online!
0-indexed, first N terms
$endgroup$
1
$begingroup$
f[x-2]+f[x-3]
->+/o'x-2 3
(o
is "recur")
$endgroup$
– ngn
yesterday
$begingroup$
@ngn Thanks! I tried it (without success) in J; it's elegant here.
$endgroup$
– Galen Ivanov
yesterday
$begingroup$
@ngn In fact here's one possibillity how it looks in J: (1#.2 3$:@-~])`1:@.(3&>)
$endgroup$
– Galen Ivanov
yesterday
$begingroup$
ah, right, base-1 decode is a train-friendly way to sum :)
$endgroup$
– ngn
yesterday
2
$begingroup$
1:
->#
in the j solution
$endgroup$
– ngn
yesterday
|
show 2 more comments
$begingroup$
K (ngn/k), 24 20 bytes
-4 bytes thanks to ngn!
{$[x<3;1;+/o'x-2 3]}
Try it online!
0-indexed, first N terms
$endgroup$
K (ngn/k), 24 20 bytes
-4 bytes thanks to ngn!
{$[x<3;1;+/o'x-2 3]}
Try it online!
0-indexed, first N terms
edited yesterday
answered yesterday
Galen IvanovGalen Ivanov
7,41211034
7,41211034
1
$begingroup$
f[x-2]+f[x-3]
->+/o'x-2 3
(o
is "recur")
$endgroup$
– ngn
yesterday
$begingroup$
@ngn Thanks! I tried it (without success) in J; it's elegant here.
$endgroup$
– Galen Ivanov
yesterday
$begingroup$
@ngn In fact here's one possibillity how it looks in J: (1#.2 3$:@-~])`1:@.(3&>)
$endgroup$
– Galen Ivanov
yesterday
$begingroup$
ah, right, base-1 decode is a train-friendly way to sum :)
$endgroup$
– ngn
yesterday
2
$begingroup$
1:
->#
in the j solution
$endgroup$
– ngn
yesterday
|
show 2 more comments
1
$begingroup$
f[x-2]+f[x-3]
->+/o'x-2 3
(o
is "recur")
$endgroup$
– ngn
yesterday
$begingroup$
@ngn Thanks! I tried it (without success) in J; it's elegant here.
$endgroup$
– Galen Ivanov
yesterday
$begingroup$
@ngn In fact here's one possibillity how it looks in J: (1#.2 3$:@-~])`1:@.(3&>)
$endgroup$
– Galen Ivanov
yesterday
$begingroup$
ah, right, base-1 decode is a train-friendly way to sum :)
$endgroup$
– ngn
yesterday
2
$begingroup$
1:
->#
in the j solution
$endgroup$
– ngn
yesterday
1
1
$begingroup$
f[x-2]+f[x-3]
-> +/o'x-2 3
(o
is "recur")$endgroup$
– ngn
yesterday
$begingroup$
f[x-2]+f[x-3]
-> +/o'x-2 3
(o
is "recur")$endgroup$
– ngn
yesterday
$begingroup$
@ngn Thanks! I tried it (without success) in J; it's elegant here.
$endgroup$
– Galen Ivanov
yesterday
$begingroup$
@ngn Thanks! I tried it (without success) in J; it's elegant here.
$endgroup$
– Galen Ivanov
yesterday
$begingroup$
@ngn In fact here's one possibillity how it looks in J: (1#.2 3$:@-~])`1:@.(3&>)
$endgroup$
– Galen Ivanov
yesterday
$begingroup$
@ngn In fact here's one possibillity how it looks in J: (1#.2 3$:@-~])`1:@.(3&>)
$endgroup$
– Galen Ivanov
yesterday
$begingroup$
ah, right, base-1 decode is a train-friendly way to sum :)
$endgroup$
– ngn
yesterday
$begingroup$
ah, right, base-1 decode is a train-friendly way to sum :)
$endgroup$
– ngn
yesterday
2
2
$begingroup$
1:
-> #
in the j solution$endgroup$
– ngn
yesterday
$begingroup$
1:
-> #
in the j solution$endgroup$
– ngn
yesterday
|
show 2 more comments
$begingroup$
x86 32-bit machine code, 17 bytes
53 33 db f7 e3 43 83 c1 04 03 d8 93 92 e2 fa 5b c3
Disassembly:
00CE1250 53 push ebx
00CE1251 33 DB xor ebx,ebx
00CE1253 F7 E3 mul eax,ebx
00CE1255 43 inc ebx
00CE1256 83 C1 04 add ecx,4
00CE1259 03 D8 add ebx,eax
00CE125B 93 xchg eax,ebx
00CE125C 92 xchg eax,edx
00CE125D E2 FA loop myloop (0CE1259h)
00CE125F 5B pop ebx
00CE1260 C3 ret
It is 0-indexed. The initialization is conveniently achieved by calculating eax * 0. The 128-bit result is 0, and it goes in edx:eax.
At the beginning of each iteration, the order of the registers is ebx, eax, edx. I had to choose the right order to take advantage of the encoding for the xchg eax
instruction - 1 byte.
I had to add 4 to the loop counter in order to let the output reach eax
, which holds the function's return value in the fastcall
convention.
I could use some other calling convention, which doesn't require saving and restoring ebx
, but fastcall
is fun anyway :)
$endgroup$
$begingroup$
I love to see machine code answers on PP&CG! +1
$endgroup$
– Tau
23 hours ago
add a comment |
$begingroup$
x86 32-bit machine code, 17 bytes
53 33 db f7 e3 43 83 c1 04 03 d8 93 92 e2 fa 5b c3
Disassembly:
00CE1250 53 push ebx
00CE1251 33 DB xor ebx,ebx
00CE1253 F7 E3 mul eax,ebx
00CE1255 43 inc ebx
00CE1256 83 C1 04 add ecx,4
00CE1259 03 D8 add ebx,eax
00CE125B 93 xchg eax,ebx
00CE125C 92 xchg eax,edx
00CE125D E2 FA loop myloop (0CE1259h)
00CE125F 5B pop ebx
00CE1260 C3 ret
It is 0-indexed. The initialization is conveniently achieved by calculating eax * 0. The 128-bit result is 0, and it goes in edx:eax.
At the beginning of each iteration, the order of the registers is ebx, eax, edx. I had to choose the right order to take advantage of the encoding for the xchg eax
instruction - 1 byte.
I had to add 4 to the loop counter in order to let the output reach eax
, which holds the function's return value in the fastcall
convention.
I could use some other calling convention, which doesn't require saving and restoring ebx
, but fastcall
is fun anyway :)
$endgroup$
$begingroup$
I love to see machine code answers on PP&CG! +1
$endgroup$
– Tau
23 hours ago
add a comment |
$begingroup$
x86 32-bit machine code, 17 bytes
53 33 db f7 e3 43 83 c1 04 03 d8 93 92 e2 fa 5b c3
Disassembly:
00CE1250 53 push ebx
00CE1251 33 DB xor ebx,ebx
00CE1253 F7 E3 mul eax,ebx
00CE1255 43 inc ebx
00CE1256 83 C1 04 add ecx,4
00CE1259 03 D8 add ebx,eax
00CE125B 93 xchg eax,ebx
00CE125C 92 xchg eax,edx
00CE125D E2 FA loop myloop (0CE1259h)
00CE125F 5B pop ebx
00CE1260 C3 ret
It is 0-indexed. The initialization is conveniently achieved by calculating eax * 0. The 128-bit result is 0, and it goes in edx:eax.
At the beginning of each iteration, the order of the registers is ebx, eax, edx. I had to choose the right order to take advantage of the encoding for the xchg eax
instruction - 1 byte.
I had to add 4 to the loop counter in order to let the output reach eax
, which holds the function's return value in the fastcall
convention.
I could use some other calling convention, which doesn't require saving and restoring ebx
, but fastcall
is fun anyway :)
$endgroup$
x86 32-bit machine code, 17 bytes
53 33 db f7 e3 43 83 c1 04 03 d8 93 92 e2 fa 5b c3
Disassembly:
00CE1250 53 push ebx
00CE1251 33 DB xor ebx,ebx
00CE1253 F7 E3 mul eax,ebx
00CE1255 43 inc ebx
00CE1256 83 C1 04 add ecx,4
00CE1259 03 D8 add ebx,eax
00CE125B 93 xchg eax,ebx
00CE125C 92 xchg eax,edx
00CE125D E2 FA loop myloop (0CE1259h)
00CE125F 5B pop ebx
00CE1260 C3 ret
It is 0-indexed. The initialization is conveniently achieved by calculating eax * 0. The 128-bit result is 0, and it goes in edx:eax.
At the beginning of each iteration, the order of the registers is ebx, eax, edx. I had to choose the right order to take advantage of the encoding for the xchg eax
instruction - 1 byte.
I had to add 4 to the loop counter in order to let the output reach eax
, which holds the function's return value in the fastcall
convention.
I could use some other calling convention, which doesn't require saving and restoring ebx
, but fastcall
is fun anyway :)
answered yesterday
anatolyganatolyg
7,2592166
7,2592166
$begingroup$
I love to see machine code answers on PP&CG! +1
$endgroup$
– Tau
23 hours ago
add a comment |
$begingroup$
I love to see machine code answers on PP&CG! +1
$endgroup$
– Tau
23 hours ago
$begingroup$
I love to see machine code answers on PP&CG! +1
$endgroup$
– Tau
23 hours ago
$begingroup$
I love to see machine code answers on PP&CG! +1
$endgroup$
– Tau
23 hours ago
add a comment |
$begingroup$
Japt -N
, 12 bytes
<3ªßUµ2 +ß´U
Try it
$endgroup$
$begingroup$
Looks like 12 is the best we can do :
$endgroup$
– Shaggy
2 days ago
$begingroup$
I stand corrected!
$endgroup$
– Shaggy
14 hours ago
add a comment |
$begingroup$
Japt -N
, 12 bytes
<3ªßUµ2 +ß´U
Try it
$endgroup$
$begingroup$
Looks like 12 is the best we can do :
$endgroup$
– Shaggy
2 days ago
$begingroup$
I stand corrected!
$endgroup$
– Shaggy
14 hours ago
add a comment |
$begingroup$
Japt -N
, 12 bytes
<3ªßUµ2 +ß´U
Try it
$endgroup$
Japt -N
, 12 bytes
<3ªßUµ2 +ß´U
Try it
answered 2 days ago
Embodiment of IgnoranceEmbodiment of Ignorance
2,886127
2,886127
$begingroup$
Looks like 12 is the best we can do :
$endgroup$
– Shaggy
2 days ago
$begingroup$
I stand corrected!
$endgroup$
– Shaggy
14 hours ago
add a comment |
$begingroup$
Looks like 12 is the best we can do :
$endgroup$
– Shaggy
2 days ago
$begingroup$
I stand corrected!
$endgroup$
– Shaggy
14 hours ago
$begingroup$
Looks like 12 is the best we can do :
$endgroup$
– Shaggy
2 days ago
$begingroup$
Looks like 12 is the best we can do :
$endgroup$
– Shaggy
2 days ago
$begingroup$
I stand corrected!
$endgroup$
– Shaggy
14 hours ago
$begingroup$
I stand corrected!
$endgroup$
– Shaggy
14 hours ago
add a comment |
$begingroup$
Pyth, 16 bytes
L?<b3!b+y-b2y-b3
This defines the function y
. Try it here!
Here's a more fun solution, though it's 9 bytes longer; bytes could be shaved though.
+l{sa.pMf.Am&>d2%d2T./QY!
This uses the definition given by David Callan on the OEIS page: "a(n) = number of compositions of n into parts that are odd and >= 3." Try it here! It takes input directly instead of defining a function.
$endgroup$
$begingroup$
y-b2y-b3
could maybe be refactored with either bifurcate orL
? Though declaring an array of 2 elements is costly.yL-Lb2,3
is longer :(
$endgroup$
– Ven
yesterday
$begingroup$
@Ven I was able to replace+y-b2y-b3
withsmy-bdhB2
which is the same amount of bytes;hB2
results in the array[2, 3]
$endgroup$
– RK.
yesterday
$begingroup$
Well done onhB2
. Too bad it's the same byte count.
$endgroup$
– Ven
yesterday
$begingroup$
Yeah, though I wonder if there's some way to get rid of thed
in the map.
$endgroup$
– RK.
yesterday
add a comment |
$begingroup$
Pyth, 16 bytes
L?<b3!b+y-b2y-b3
This defines the function y
. Try it here!
Here's a more fun solution, though it's 9 bytes longer; bytes could be shaved though.
+l{sa.pMf.Am&>d2%d2T./QY!
This uses the definition given by David Callan on the OEIS page: "a(n) = number of compositions of n into parts that are odd and >= 3." Try it here! It takes input directly instead of defining a function.
$endgroup$
$begingroup$
y-b2y-b3
could maybe be refactored with either bifurcate orL
? Though declaring an array of 2 elements is costly.yL-Lb2,3
is longer :(
$endgroup$
– Ven
yesterday
$begingroup$
@Ven I was able to replace+y-b2y-b3
withsmy-bdhB2
which is the same amount of bytes;hB2
results in the array[2, 3]
$endgroup$
– RK.
yesterday
$begingroup$
Well done onhB2
. Too bad it's the same byte count.
$endgroup$
– Ven
yesterday
$begingroup$
Yeah, though I wonder if there's some way to get rid of thed
in the map.
$endgroup$
– RK.
yesterday
add a comment |
$begingroup$
Pyth, 16 bytes
L?<b3!b+y-b2y-b3
This defines the function y
. Try it here!
Here's a more fun solution, though it's 9 bytes longer; bytes could be shaved though.
+l{sa.pMf.Am&>d2%d2T./QY!
This uses the definition given by David Callan on the OEIS page: "a(n) = number of compositions of n into parts that are odd and >= 3." Try it here! It takes input directly instead of defining a function.
$endgroup$
Pyth, 16 bytes
L?<b3!b+y-b2y-b3
This defines the function y
. Try it here!
Here's a more fun solution, though it's 9 bytes longer; bytes could be shaved though.
+l{sa.pMf.Am&>d2%d2T./QY!
This uses the definition given by David Callan on the OEIS page: "a(n) = number of compositions of n into parts that are odd and >= 3." Try it here! It takes input directly instead of defining a function.
answered 2 days ago
RK.RK.
407211
407211
$begingroup$
y-b2y-b3
could maybe be refactored with either bifurcate orL
? Though declaring an array of 2 elements is costly.yL-Lb2,3
is longer :(
$endgroup$
– Ven
yesterday
$begingroup$
@Ven I was able to replace+y-b2y-b3
withsmy-bdhB2
which is the same amount of bytes;hB2
results in the array[2, 3]
$endgroup$
– RK.
yesterday
$begingroup$
Well done onhB2
. Too bad it's the same byte count.
$endgroup$
– Ven
yesterday
$begingroup$
Yeah, though I wonder if there's some way to get rid of thed
in the map.
$endgroup$
– RK.
yesterday
add a comment |
$begingroup$
y-b2y-b3
could maybe be refactored with either bifurcate orL
? Though declaring an array of 2 elements is costly.yL-Lb2,3
is longer :(
$endgroup$
– Ven
yesterday
$begingroup$
@Ven I was able to replace+y-b2y-b3
withsmy-bdhB2
which is the same amount of bytes;hB2
results in the array[2, 3]
$endgroup$
– RK.
yesterday
$begingroup$
Well done onhB2
. Too bad it's the same byte count.
$endgroup$
– Ven
yesterday
$begingroup$
Yeah, though I wonder if there's some way to get rid of thed
in the map.
$endgroup$
– RK.
yesterday
$begingroup$
y-b2y-b3
could maybe be refactored with either bifurcate or L
? Though declaring an array of 2 elements is costly. yL-Lb2,3
is longer :($endgroup$
– Ven
yesterday
$begingroup$
y-b2y-b3
could maybe be refactored with either bifurcate or L
? Though declaring an array of 2 elements is costly. yL-Lb2,3
is longer :($endgroup$
– Ven
yesterday
$begingroup$
@Ven I was able to replace
+y-b2y-b3
with smy-bdhB2
which is the same amount of bytes; hB2
results in the array [2, 3]
$endgroup$
– RK.
yesterday
$begingroup$
@Ven I was able to replace
+y-b2y-b3
with smy-bdhB2
which is the same amount of bytes; hB2
results in the array [2, 3]
$endgroup$
– RK.
yesterday
$begingroup$
Well done on
hB2
. Too bad it's the same byte count.$endgroup$
– Ven
yesterday
$begingroup$
Well done on
hB2
. Too bad it's the same byte count.$endgroup$
– Ven
yesterday
$begingroup$
Yeah, though I wonder if there's some way to get rid of the
d
in the map.$endgroup$
– RK.
yesterday
$begingroup$
Yeah, though I wonder if there's some way to get rid of the
d
in the map.$endgroup$
– RK.
yesterday
add a comment |
$begingroup$
05AB1E, 8 bytes
1Ð)λ£₂₃+
Try it online!
Bear with me, I haven't golfed in a while. I wonder if there's a shorter substitute for 1Ð)
which works in this case (I've tried 1D)
, 3Å1
etc. but none of them save bytes). Outputs the first $n$ terms of the sequence. Or, without the £
, it would output an infinite stream of the terms of the sequence.
How?
1Ð)λ£₂₃+ | Full program.
1Ð) | Initialize the stack with [1, 1, 1].
λ | Begin the recursive generation of a list: Starting from some base case,
| this command generates an infinite list with the pattern function given.
£ | Flag for λ. Instead of outputting an infinite stream, only print the first n.
₂₃+ | Add a(n-2) and a(n-3).
$endgroup$
$begingroup$
I don't think1Ð)
can be 2 bytes tbh. I can think of six different 3-bytes alternatives, but no 2-byters.
$endgroup$
– Kevin Cruijssen
yesterday
add a comment |
$begingroup$
05AB1E, 8 bytes
1Ð)λ£₂₃+
Try it online!
Bear with me, I haven't golfed in a while. I wonder if there's a shorter substitute for 1Ð)
which works in this case (I've tried 1D)
, 3Å1
etc. but none of them save bytes). Outputs the first $n$ terms of the sequence. Or, without the £
, it would output an infinite stream of the terms of the sequence.
How?
1Ð)λ£₂₃+ | Full program.
1Ð) | Initialize the stack with [1, 1, 1].
λ | Begin the recursive generation of a list: Starting from some base case,
| this command generates an infinite list with the pattern function given.
£ | Flag for λ. Instead of outputting an infinite stream, only print the first n.
₂₃+ | Add a(n-2) and a(n-3).
$endgroup$
$begingroup$
I don't think1Ð)
can be 2 bytes tbh. I can think of six different 3-bytes alternatives, but no 2-byters.
$endgroup$
– Kevin Cruijssen
yesterday
add a comment |
$begingroup$
05AB1E, 8 bytes
1Ð)λ£₂₃+
Try it online!
Bear with me, I haven't golfed in a while. I wonder if there's a shorter substitute for 1Ð)
which works in this case (I've tried 1D)
, 3Å1
etc. but none of them save bytes). Outputs the first $n$ terms of the sequence. Or, without the £
, it would output an infinite stream of the terms of the sequence.
How?
1Ð)λ£₂₃+ | Full program.
1Ð) | Initialize the stack with [1, 1, 1].
λ | Begin the recursive generation of a list: Starting from some base case,
| this command generates an infinite list with the pattern function given.
£ | Flag for λ. Instead of outputting an infinite stream, only print the first n.
₂₃+ | Add a(n-2) and a(n-3).
$endgroup$
05AB1E, 8 bytes
1Ð)λ£₂₃+
Try it online!
Bear with me, I haven't golfed in a while. I wonder if there's a shorter substitute for 1Ð)
which works in this case (I've tried 1D)
, 3Å1
etc. but none of them save bytes). Outputs the first $n$ terms of the sequence. Or, without the £
, it would output an infinite stream of the terms of the sequence.
How?
1Ð)λ£₂₃+ | Full program.
1Ð) | Initialize the stack with [1, 1, 1].
λ | Begin the recursive generation of a list: Starting from some base case,
| this command generates an infinite list with the pattern function given.
£ | Flag for λ. Instead of outputting an infinite stream, only print the first n.
₂₃+ | Add a(n-2) and a(n-3).
edited yesterday
answered yesterday
Mr. XcoderMr. Xcoder
32.2k759200
32.2k759200
$begingroup$
I don't think1Ð)
can be 2 bytes tbh. I can think of six different 3-bytes alternatives, but no 2-byters.
$endgroup$
– Kevin Cruijssen
yesterday
add a comment |
$begingroup$
I don't think1Ð)
can be 2 bytes tbh. I can think of six different 3-bytes alternatives, but no 2-byters.
$endgroup$
– Kevin Cruijssen
yesterday
$begingroup$
I don't think
1Ð)
can be 2 bytes tbh. I can think of six different 3-bytes alternatives, but no 2-byters.$endgroup$
– Kevin Cruijssen
yesterday
$begingroup$
I don't think
1Ð)
can be 2 bytes tbh. I can think of six different 3-bytes alternatives, but no 2-byters.$endgroup$
– Kevin Cruijssen
yesterday
add a comment |
$begingroup$
Java, 41 bytes
Can't use a lambda (runtime error). Port of this Javascript answer
int f(int n){return n<3?1:f(n-2)+f(n-3);}
TIO
$endgroup$
$begingroup$
I think you are missing some requirements: Have a look at my modification here.
$endgroup$
– Shaq
15 hours ago
$begingroup$
Please disregard Shaq's comment: your answer is correct and is the shortest Java answer possible (as of Java 12).
$endgroup$
– Olivier Grégoire
11 hours ago
$begingroup$
Ok then. I'm not sure what I "missed" but ok. Edit: nvm I read the JS answer.
$endgroup$
– Benjamin Urquhart
6 hours ago
add a comment |
$begingroup$
Java, 41 bytes
Can't use a lambda (runtime error). Port of this Javascript answer
int f(int n){return n<3?1:f(n-2)+f(n-3);}
TIO
$endgroup$
$begingroup$
I think you are missing some requirements: Have a look at my modification here.
$endgroup$
– Shaq
15 hours ago
$begingroup$
Please disregard Shaq's comment: your answer is correct and is the shortest Java answer possible (as of Java 12).
$endgroup$
– Olivier Grégoire
11 hours ago
$begingroup$
Ok then. I'm not sure what I "missed" but ok. Edit: nvm I read the JS answer.
$endgroup$
– Benjamin Urquhart
6 hours ago
add a comment |
$begingroup$
Java, 41 bytes
Can't use a lambda (runtime error). Port of this Javascript answer
int f(int n){return n<3?1:f(n-2)+f(n-3);}
TIO
$endgroup$
Java, 41 bytes
Can't use a lambda (runtime error). Port of this Javascript answer
int f(int n){return n<3?1:f(n-2)+f(n-3);}
TIO
answered yesterday
Benjamin UrquhartBenjamin Urquhart
40017
40017
$begingroup$
I think you are missing some requirements: Have a look at my modification here.
$endgroup$
– Shaq
15 hours ago
$begingroup$
Please disregard Shaq's comment: your answer is correct and is the shortest Java answer possible (as of Java 12).
$endgroup$
– Olivier Grégoire
11 hours ago
$begingroup$
Ok then. I'm not sure what I "missed" but ok. Edit: nvm I read the JS answer.
$endgroup$
– Benjamin Urquhart
6 hours ago
add a comment |
$begingroup$
I think you are missing some requirements: Have a look at my modification here.
$endgroup$
– Shaq
15 hours ago
$begingroup$
Please disregard Shaq's comment: your answer is correct and is the shortest Java answer possible (as of Java 12).
$endgroup$
– Olivier Grégoire
11 hours ago
$begingroup$
Ok then. I'm not sure what I "missed" but ok. Edit: nvm I read the JS answer.
$endgroup$
– Benjamin Urquhart
6 hours ago
$begingroup$
I think you are missing some requirements: Have a look at my modification here.
$endgroup$
– Shaq
15 hours ago
$begingroup$
I think you are missing some requirements: Have a look at my modification here.
$endgroup$
– Shaq
15 hours ago
$begingroup$
Please disregard Shaq's comment: your answer is correct and is the shortest Java answer possible (as of Java 12).
$endgroup$
– Olivier Grégoire
11 hours ago
$begingroup$
Please disregard Shaq's comment: your answer is correct and is the shortest Java answer possible (as of Java 12).
$endgroup$
– Olivier Grégoire
11 hours ago
$begingroup$
Ok then. I'm not sure what I "missed" but ok. Edit: nvm I read the JS answer.
$endgroup$
– Benjamin Urquhart
6 hours ago
$begingroup$
Ok then. I'm not sure what I "missed" but ok. Edit: nvm I read the JS answer.
$endgroup$
– Benjamin Urquhart
6 hours ago
add a comment |
$begingroup$
R + pryr, 38 36 bytes
Zero-indexed recursive function.
f=pryr::f(`if`(n<3,1,f(n-2)+f(n-3)))
Try it online!
Thanks to @Giuseppe for pointing out two obviously needless bytes.
$endgroup$
2
$begingroup$
If you're going to be usingpryr
, the language should beR + pryr
and this can be 36 bytes
$endgroup$
– Giuseppe
yesterday
$begingroup$
@Giuseppe thanks! Updated now.
$endgroup$
– rturnbull
yesterday
add a comment |
$begingroup$
R + pryr, 38 36 bytes
Zero-indexed recursive function.
f=pryr::f(`if`(n<3,1,f(n-2)+f(n-3)))
Try it online!
Thanks to @Giuseppe for pointing out two obviously needless bytes.
$endgroup$
2
$begingroup$
If you're going to be usingpryr
, the language should beR + pryr
and this can be 36 bytes
$endgroup$
– Giuseppe
yesterday
$begingroup$
@Giuseppe thanks! Updated now.
$endgroup$
– rturnbull
yesterday
add a comment |
$begingroup$
R + pryr, 38 36 bytes
Zero-indexed recursive function.
f=pryr::f(`if`(n<3,1,f(n-2)+f(n-3)))
Try it online!
Thanks to @Giuseppe for pointing out two obviously needless bytes.
$endgroup$
R + pryr, 38 36 bytes
Zero-indexed recursive function.
f=pryr::f(`if`(n<3,1,f(n-2)+f(n-3)))
Try it online!
Thanks to @Giuseppe for pointing out two obviously needless bytes.
edited yesterday
answered yesterday
rturnbullrturnbull
3,519925
3,519925
2
$begingroup$
If you're going to be usingpryr
, the language should beR + pryr
and this can be 36 bytes
$endgroup$
– Giuseppe
yesterday
$begingroup$
@Giuseppe thanks! Updated now.
$endgroup$
– rturnbull
yesterday
add a comment |
2
$begingroup$
If you're going to be usingpryr
, the language should beR + pryr
and this can be 36 bytes
$endgroup$
– Giuseppe
yesterday
$begingroup$
@Giuseppe thanks! Updated now.
$endgroup$
– rturnbull
yesterday
2
2
$begingroup$
If you're going to be using
pryr
, the language should be R + pryr
and this can be 36 bytes$endgroup$
– Giuseppe
yesterday
$begingroup$
If you're going to be using
pryr
, the language should be R + pryr
and this can be 36 bytes$endgroup$
– Giuseppe
yesterday
$begingroup$
@Giuseppe thanks! Updated now.
$endgroup$
– rturnbull
yesterday
$begingroup$
@Giuseppe thanks! Updated now.
$endgroup$
– rturnbull
yesterday
add a comment |
$begingroup$
JavaScript (ES6), 23 bytes
Implements the recursive definition of A000931, but with $a(0)=a(1)=a(2)=1$, as specified in the challenge.
Returns the $N$th term, 0-indexed.
f=n=>n<3||f(n-2)+f(n-3)
Try it online!
$endgroup$
$begingroup$
I don't think it's reasonable to say that returningtrue
is the same as returning1
if the rest of the output is numbers.
$endgroup$
– Nit
yesterday
1
$begingroup$
@Nit Relevant meta post.
$endgroup$
– Arnauld
yesterday
$begingroup$
I think you are missing some requirements: Have a look at my modification (version in Java) here.
$endgroup$
– Shaq
15 hours ago
$begingroup$
@Shaq The challenge clearly specifies that the first three terms of the sequence are all 1. So, it's not the sequence defined in A000931 (but the formula is the same).
$endgroup$
– Arnauld
15 hours ago
$begingroup$
@Arnauld yep I can see it now. Sorry!
$endgroup$
– Shaq
14 hours ago
add a comment |
$begingroup$
JavaScript (ES6), 23 bytes
Implements the recursive definition of A000931, but with $a(0)=a(1)=a(2)=1$, as specified in the challenge.
Returns the $N$th term, 0-indexed.
f=n=>n<3||f(n-2)+f(n-3)
Try it online!
$endgroup$
$begingroup$
I don't think it's reasonable to say that returningtrue
is the same as returning1
if the rest of the output is numbers.
$endgroup$
– Nit
yesterday
1
$begingroup$
@Nit Relevant meta post.
$endgroup$
– Arnauld
yesterday
$begingroup$
I think you are missing some requirements: Have a look at my modification (version in Java) here.
$endgroup$
– Shaq
15 hours ago
$begingroup$
@Shaq The challenge clearly specifies that the first three terms of the sequence are all 1. So, it's not the sequence defined in A000931 (but the formula is the same).
$endgroup$
– Arnauld
15 hours ago
$begingroup$
@Arnauld yep I can see it now. Sorry!
$endgroup$
– Shaq
14 hours ago
add a comment |
$begingroup$
JavaScript (ES6), 23 bytes
Implements the recursive definition of A000931, but with $a(0)=a(1)=a(2)=1$, as specified in the challenge.
Returns the $N$th term, 0-indexed.
f=n=>n<3||f(n-2)+f(n-3)
Try it online!
$endgroup$
JavaScript (ES6), 23 bytes
Implements the recursive definition of A000931, but with $a(0)=a(1)=a(2)=1$, as specified in the challenge.
Returns the $N$th term, 0-indexed.
f=n=>n<3||f(n-2)+f(n-3)
Try it online!
edited 15 hours ago
answered 2 days ago
ArnauldArnauld
80.7k797334
80.7k797334
$begingroup$
I don't think it's reasonable to say that returningtrue
is the same as returning1
if the rest of the output is numbers.
$endgroup$
– Nit
yesterday
1
$begingroup$
@Nit Relevant meta post.
$endgroup$
– Arnauld
yesterday
$begingroup$
I think you are missing some requirements: Have a look at my modification (version in Java) here.
$endgroup$
– Shaq
15 hours ago
$begingroup$
@Shaq The challenge clearly specifies that the first three terms of the sequence are all 1. So, it's not the sequence defined in A000931 (but the formula is the same).
$endgroup$
– Arnauld
15 hours ago
$begingroup$
@Arnauld yep I can see it now. Sorry!
$endgroup$
– Shaq
14 hours ago
add a comment |
$begingroup$
I don't think it's reasonable to say that returningtrue
is the same as returning1
if the rest of the output is numbers.
$endgroup$
– Nit
yesterday
1
$begingroup$
@Nit Relevant meta post.
$endgroup$
– Arnauld
yesterday
$begingroup$
I think you are missing some requirements: Have a look at my modification (version in Java) here.
$endgroup$
– Shaq
15 hours ago
$begingroup$
@Shaq The challenge clearly specifies that the first three terms of the sequence are all 1. So, it's not the sequence defined in A000931 (but the formula is the same).
$endgroup$
– Arnauld
15 hours ago
$begingroup$
@Arnauld yep I can see it now. Sorry!
$endgroup$
– Shaq
14 hours ago
$begingroup$
I don't think it's reasonable to say that returning
true
is the same as returning 1
if the rest of the output is numbers.$endgroup$
– Nit
yesterday
$begingroup$
I don't think it's reasonable to say that returning
true
is the same as returning 1
if the rest of the output is numbers.$endgroup$
– Nit
yesterday
1
1
$begingroup$
@Nit Relevant meta post.
$endgroup$
– Arnauld
yesterday
$begingroup$
@Nit Relevant meta post.
$endgroup$
– Arnauld
yesterday
$begingroup$
I think you are missing some requirements: Have a look at my modification (version in Java) here.
$endgroup$
– Shaq
15 hours ago
$begingroup$
I think you are missing some requirements: Have a look at my modification (version in Java) here.
$endgroup$
– Shaq
15 hours ago
$begingroup$
@Shaq The challenge clearly specifies that the first three terms of the sequence are all 1. So, it's not the sequence defined in A000931 (but the formula is the same).
$endgroup$
– Arnauld
15 hours ago
$begingroup$
@Shaq The challenge clearly specifies that the first three terms of the sequence are all 1. So, it's not the sequence defined in A000931 (but the formula is the same).
$endgroup$
– Arnauld
15 hours ago
$begingroup$
@Arnauld yep I can see it now. Sorry!
$endgroup$
– Shaq
14 hours ago
$begingroup$
@Arnauld yep I can see it now. Sorry!
$endgroup$
– Shaq
14 hours ago
add a comment |
$begingroup$
C (clang), 41 bytes
int a(int i){return i<3?1:a(i-2)+a(i-3);}
Try it online!
New contributor
$endgroup$
1
$begingroup$
Welcome to PPCG :)
$endgroup$
– Shaggy
10 hours ago
add a comment |
$begingroup$
C (clang), 41 bytes
int a(int i){return i<3?1:a(i-2)+a(i-3);}
Try it online!
New contributor
$endgroup$
1
$begingroup$
Welcome to PPCG :)
$endgroup$
– Shaggy
10 hours ago
add a comment |
$begingroup$
C (clang), 41 bytes
int a(int i){return i<3?1:a(i-2)+a(i-3);}
Try it online!
New contributor
$endgroup$
C (clang), 41 bytes
int a(int i){return i<3?1:a(i-2)+a(i-3);}
Try it online!
New contributor
New contributor
answered 10 hours ago
peterzugerpeterzuger
213
213
New contributor
New contributor
1
$begingroup$
Welcome to PPCG :)
$endgroup$
– Shaggy
10 hours ago
add a comment |
1
$begingroup$
Welcome to PPCG :)
$endgroup$
– Shaggy
10 hours ago
1
1
$begingroup$
Welcome to PPCG :)
$endgroup$
– Shaggy
10 hours ago
$begingroup$
Welcome to PPCG :)
$endgroup$
– Shaggy
10 hours ago
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 34 bytes
int f(int g)=>g<3?1:f(g-2)+f(g-3);
Try it online!
$endgroup$
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 34 bytes
int f(int g)=>g<3?1:f(g-2)+f(g-3);
Try it online!
$endgroup$
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 34 bytes
int f(int g)=>g<3?1:f(g-2)+f(g-3);
Try it online!
$endgroup$
C# (Visual C# Interactive Compiler), 34 bytes
int f(int g)=>g<3?1:f(g-2)+f(g-3);
Try it online!
answered 2 days ago
Embodiment of IgnoranceEmbodiment of Ignorance
2,886127
2,886127
add a comment |
add a comment |
$begingroup$
TI-BASIC (TI-84), 34 bytes
[[0,1,0][0,0,1][1,1,0]]^(Ans+5:Ans(1,1
0-indexed $N$th term of the sequence.
Input is in Ans
.
Output is in Ans
and is automatically printed out.
I figured that enough time had passed, plus multiple answers had been posted, of which there were many which out-golfed this answer.
Example:
0
0
prgmCDGFD
1
9
9
prgmCDGFD
9
16
16
prgmCDGFD
65
Explanation:
[[0,1,0][0,0,1][1,1,0]]^(Ans+5:Ans(1,1 ;full program (example input: 6)
[[0,1,0][0,0,1][1,1,0]] ;generate the following matrix:
; [0 1 0]
; [0 0 1]
; [1 1 0]
^(Ans+5 ;then raise it to the power of: input + 5
; [4 7 5]
; [5 9 7]
; [7 12 9]
Ans(1,1 ;get the top-left index and leave it in "Ans"
;implicitly print Ans
$endgroup$
add a comment |
$begingroup$
TI-BASIC (TI-84), 34 bytes
[[0,1,0][0,0,1][1,1,0]]^(Ans+5:Ans(1,1
0-indexed $N$th term of the sequence.
Input is in Ans
.
Output is in Ans
and is automatically printed out.
I figured that enough time had passed, plus multiple answers had been posted, of which there were many which out-golfed this answer.
Example:
0
0
prgmCDGFD
1
9
9
prgmCDGFD
9
16
16
prgmCDGFD
65
Explanation:
[[0,1,0][0,0,1][1,1,0]]^(Ans+5:Ans(1,1 ;full program (example input: 6)
[[0,1,0][0,0,1][1,1,0]] ;generate the following matrix:
; [0 1 0]
; [0 0 1]
; [1 1 0]
^(Ans+5 ;then raise it to the power of: input + 5
; [4 7 5]
; [5 9 7]
; [7 12 9]
Ans(1,1 ;get the top-left index and leave it in "Ans"
;implicitly print Ans
$endgroup$
add a comment |
$begingroup$
TI-BASIC (TI-84), 34 bytes
[[0,1,0][0,0,1][1,1,0]]^(Ans+5:Ans(1,1
0-indexed $N$th term of the sequence.
Input is in Ans
.
Output is in Ans
and is automatically printed out.
I figured that enough time had passed, plus multiple answers had been posted, of which there were many which out-golfed this answer.
Example:
0
0
prgmCDGFD
1
9
9
prgmCDGFD
9
16
16
prgmCDGFD
65
Explanation:
[[0,1,0][0,0,1][1,1,0]]^(Ans+5:Ans(1,1 ;full program (example input: 6)
[[0,1,0][0,0,1][1,1,0]] ;generate the following matrix:
; [0 1 0]
; [0 0 1]
; [1 1 0]
^(Ans+5 ;then raise it to the power of: input + 5
; [4 7 5]
; [5 9 7]
; [7 12 9]
Ans(1,1 ;get the top-left index and leave it in "Ans"
;implicitly print Ans
$endgroup$
TI-BASIC (TI-84), 34 bytes
[[0,1,0][0,0,1][1,1,0]]^(Ans+5:Ans(1,1
0-indexed $N$th term of the sequence.
Input is in Ans
.
Output is in Ans
and is automatically printed out.
I figured that enough time had passed, plus multiple answers had been posted, of which there were many which out-golfed this answer.
Example:
0
0
prgmCDGFD
1
9
9
prgmCDGFD
9
16
16
prgmCDGFD
65
Explanation:
[[0,1,0][0,0,1][1,1,0]]^(Ans+5:Ans(1,1 ;full program (example input: 6)
[[0,1,0][0,0,1][1,1,0]] ;generate the following matrix:
; [0 1 0]
; [0 0 1]
; [1 1 0]
^(Ans+5 ;then raise it to the power of: input + 5
; [4 7 5]
; [5 9 7]
; [7 12 9]
Ans(1,1 ;get the top-left index and leave it in "Ans"
;implicitly print Ans
answered 2 days ago
TauTau
946515
946515
add a comment |
add a comment |
$begingroup$
Perl 5, 34 bytes
sub f{"@_"<3||f("@_"-2)+f("@_"-3)}
Try it online!
$endgroup$
add a comment |
$begingroup$
Perl 5, 34 bytes
sub f{"@_"<3||f("@_"-2)+f("@_"-3)}
Try it online!
$endgroup$
add a comment |
$begingroup$
Perl 5, 34 bytes
sub f{"@_"<3||f("@_"-2)+f("@_"-3)}
Try it online!
$endgroup$
Perl 5, 34 bytes
sub f{"@_"<3||f("@_"-2)+f("@_"-3)}
Try it online!
answered yesterday
XcaliXcali
5,475520
5,475520
add a comment |
add a comment |
$begingroup$
Wolfram Language (Mathematica), 26 bytes
If[#<3,1,#0[#-2]+#0[#-3]]&
Try it online!
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 26 bytes
If[#<3,1,#0[#-2]+#0[#-3]]&
Try it online!
$endgroup$
add a comment |
$begingroup$
Wolfram Language (Mathematica), 26 bytes
If[#<3,1,#0[#-2]+#0[#-3]]&
Try it online!
$endgroup$
Wolfram Language (Mathematica), 26 bytes
If[#<3,1,#0[#-2]+#0[#-3]]&
Try it online!
answered 20 hours ago
attinatattinat
4897
4897
add a comment |
add a comment |
$begingroup$
Pari/GP, 28 bytes
0-indexed.
f(n)=if(n<3,1,f(n-2)+f(n-3))
Try it online!
Pari/GP, 35 bytes
1-indexed.
n->Vec((1+x+O(x^n))/(1-x^2-x^3))[n]
Try it online!
The generating function of the sequence is $frac{1+x}{1-x^2-x^3}$.
$endgroup$
add a comment |
$begingroup$
Pari/GP, 28 bytes
0-indexed.
f(n)=if(n<3,1,f(n-2)+f(n-3))
Try it online!
Pari/GP, 35 bytes
1-indexed.
n->Vec((1+x+O(x^n))/(1-x^2-x^3))[n]
Try it online!
The generating function of the sequence is $frac{1+x}{1-x^2-x^3}$.
$endgroup$
add a comment |
$begingroup$
Pari/GP, 28 bytes
0-indexed.
f(n)=if(n<3,1,f(n-2)+f(n-3))
Try it online!
Pari/GP, 35 bytes
1-indexed.
n->Vec((1+x+O(x^n))/(1-x^2-x^3))[n]
Try it online!
The generating function of the sequence is $frac{1+x}{1-x^2-x^3}$.
$endgroup$
Pari/GP, 28 bytes
0-indexed.
f(n)=if(n<3,1,f(n-2)+f(n-3))
Try it online!
Pari/GP, 35 bytes
1-indexed.
n->Vec((1+x+O(x^n))/(1-x^2-x^3))[n]
Try it online!
The generating function of the sequence is $frac{1+x}{1-x^2-x^3}$.
edited 19 hours ago
answered 20 hours ago
alephalphaalephalpha
21.8k33094
21.8k33094
add a comment |
add a comment |
1 2
next
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%2f182797%2fpatience-young-padovan%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
2
$begingroup$
14
(0-indexed) is shown as outputting28
while I believe it should yield37
$endgroup$
– Jonathan Allan
2 days ago
$begingroup$
@JonathanAllan yes, you are correct. I fixed the last two test cases for $N$th term but not that one. The post has been edited.
$endgroup$
– Tau
2 days ago
$begingroup$
@LuisMendo I believe so. I'll edit the post.
$endgroup$
– Tau
yesterday
1
$begingroup$
@sharur this definition for the Fibonacci sequence is the visual definition. Each successive square added has a length of that term in the sequence. The sequence you describe is the numerical reasoning behind it. Both sequences work just as well as the other.
$endgroup$
– Tau
23 hours ago
1
$begingroup$
Note that the OEIS sequence you linked is slightly different, since it uses
a_0=1, a_1=0, a_2=0
. It ends up being shifted by a bit because thena_5=a_6=a_7=1
$endgroup$
– Carmeister
23 hours ago