Generate the nth Narayana-Zidek-Capell number given an input n. Fewest bytes win.
f(1)=1, f(n) is the sum of the previous floor(n/2) Narayana-Zidek-Capell terms.
Test Cases:
f(1)=1
f(9)=42
f(14)=1308
f(15)=2605
f(23)=664299
Generate the nth Narayana-Zidek-Capell number given an input n. Fewest bytes win.
f(1)=1, f(n) is the sum of the previous floor(n/2) Narayana-Zidek-Capell terms.
Test Cases:
f(1)=1
f(9)=42
f(14)=1308
f(15)=2605
f(23)=664299
HĊrµṖ߀Sȯ1
Takes n
as argument and prints the result.
H divide input by 2
Ċ round up to get first n to recurse
r inclusive range from that to n
µ (chain separator)
Ṗ remove n itself from the range
߀ call self recursively on each value in the range
S sum results
ȯ1 if sum was zero, return one
1
is redundant since the argument will be one in the case it is used. Secondly, we can take the second "half" of the range from zero to n
to get the same list of numbers as HĊrµṖ
gets us which is ḶŒHṪ
- TIO
\$\endgroup\$
Commented
Nov 29, 2023 at 22:19
This uses a formula from the OEIS page for the Narayana-Zidek-Cappell numbers.
Edit: Got rid of parentheses using operator precedence with thanks to feersum and Neil.
f=->x{x<4?1:2*f[x-1]-x%2*f[x/2]}
x%2
?
\$\endgroup\$
x%2*
at least.
\$\endgroup\$
x<2?
... this makes it much clearer thanks!
\$\endgroup\$
Algorithm taken from the OEIS page. n<3
may be changed to n<4
with no effect. Returns the n
th number, where n
is a positive integer.
a=lambda n:n<3or 2*a(n-1)-n%2*a(n/2)
An iterative solution as 05AB1E doesn't have functions.
X¸sGDN>;ï£Os‚˜}¬
X¸ # initialize a list with 1
sG } # input-1 number of times do
D # duplicate current list
N>;ï£ # take n/2 elements from the list
O # sum those elements
s‚˜ # add at the start of the list
¬ # get the first element and implicitly print
A translation of the OEIS algorithm. There's just not enough C code around here!
f(n){return n<3?:2*f(n-1)-n%2*f(n/2);}
n<3?:(...)
work?
\$\endgroup\$
Commented
Jul 11, 2016 at 22:16
def f(n):
x=1,
for i in range(n):x+=sum(x[-i//2:]),
print(x[-1])
A function that takes input via argument and prints to STDOUT. This is a direct implementation of the definition.
How it works
def f(n): Function with input target term index n
x=1, Initialise term list x as tuple (1)
for i in range(n):... For all term indices in [0,n-1]...
x[-i//2:] ..yield the previous floor(i/2) terms...
x+=sum(...) ...and append their sum to x
print(x[-1]) Print the last term in x, which is the nth term
L|syM>/b2Ub1
Defines a function y(n)
that returns the n
th Narayana-Zidek-Capell-number.
If[#<4,1,2#0[#-1]-#~Mod~2#0[(#-1)/2]]&
Anonymous function. Takes 𝑛 as input and returns 𝑓(𝑛) as output. Based off of the Ruby solution.
f 1=1
f n=sum$f<$>[n-div n 2..n-1]
Usage example: f 14
-> 1308
.
A direct implementation of the definition.
int z(int n){return n<3?1:n%2>0?(2*z(n-1)-z(n/2)):(2*z(n-1));}
func f(i int) int{if(i<4){return 1};return 2*f(i-1)-i%2*f(i/2)}
Pretty much a direct port from the C answer
This is a full program without recursion. A recursive function can be defined in 52 bytes (it might be possible to beat that) but that's just a pretty boring port of sherlock9's answer (and it errors if you ask for f(100) or more) so I'm putting up this longer and more interesting version
<?php for($i=$argv[1];$j=$i;$i--)for(;--$j*2>=$i;)$a[$j]+=$a[$i]?:1;echo$a[1]?:1;
Causes many (O[n]) notices but that's fine.
x[1]=1;for(i in 2:10){x[i]=sum(x[i-1:floor(i/2)])};x[9]
Change 10
in the for
loop and x[9]
to get whichever index the user wants.
f=function(n)ifelse(n<4,1,2*f(n-1)-n%%2*f(floor(n/2)))
\$\endgroup\$
f=n=>Math.round(n<3?1:2*f(n-1)-n%2*f(parseInt(n/2)))
Based on the C answer.
parseInt
instead of Math.floor
`if`(n<4,1,2*f(n-1)-(n mod 2)*f(floor(n/2)))
Usage:
> f:=n->`if`(n<4,1,2*f(n-1)-(n mod 2)*f(floor(n/2)));
> seq( f(i), i = 1..10 );
1, 1, 1, 2, 3, 6, 11, 22, 42, 84
f=function(n,a=0)if(n<2)1 else{for(i in n-1:(n%/%2))a=a+f(i);a}
a=0
is added as a default because it saves me two curly brackets. Function recursively calls itself as needed.