counterplus
桁ごとに異なる(かもしれない)進数に対応づけて、数値を1ずつ増やす
counterplus()では、a1xa2...xan個の異なる数値が作られる。
これは通常の「進数」
counterplus2()では、a1+a1xa2+a1xa2xa3+...+a1xa2x...xan個の数値が作られる。これは、分岐木のノードの個数
counterplus(counter,t)
counterplus2(counter,t)
counterplus<-function(x,t){
x[1]<-x[1]+1
for(i in 1:(length(x))){
if(x[i]==t[i]){
x[i]<-0
if(i<length(x)){
x[i+1]<-x[i+1]+1
}
}else{
return(x)
}
}
return(x)
}
counterplus2<-function(x,t){
x[1]<-x[1]+1
for(i in 1:(length(x))){
if(x[i]==(t[i]+1)){
x[i]<-1
if(i<length(x)){
x[i+1]<-x[i+1]+1
}
}else{
return(x)
}
}
return(x)
}
nchild<-c(2,3,2,4)
nstep<-length(nchild)
counter<-rep(0,nstep)
loop<-TRUE
cnt<-1
ns<-NULL
ns[[cnt]]<-counter
while(loop){
counter<-counterplus(counter,nchild)
cnt<-cnt+1
if(sum(counter)==0){
loop<-FALSE
}else{
ns[[cnt]]<-c(counter)
}
}
cnt
length(ns)
counter<-rep(0,nstep)
loop<-TRUE
cnt<-1
ns<-NULL
ns[[cnt]]<-counter
while(loop){
counter<-counterplus2(counter,nchild)
cnt<-cnt+1
ns[[cnt]]<-c(counter)
if(sum(counter)==sum(nchild)){
loop<-FALSE
}
}
cnt
ns