/* gnubc program: 3x+3k */

"
			THE 3x+3^k CONJECTURE:







If collatzk(y,k) is typed in, (y non-zero), the iterates

y, t(y), t(t(y)),... of the 3x+3^k function t(x) are printed

and the number of steps taken to reach one of 3^k(1, -1, -5, -17)

is recorded.  (It is conjectured that every trajectory starting

from a non-zero integer will end in one of these numbers.)
"

/*  the least non-negative remainder when an integer a is divided by a positive
integer b */

/* a%b=m(a,b) if a>=0 or a<0 and b divides a */
/* a%b=m(a,b)-b if a<0, b>0, a not divisible by b */

define mod(a,b){
	auto c
	c=a%b
	if(a>=0) return(c)
	if(c==0) return(0)
        return(c+b)	
}

/* the 3x+3^k function */
define collatztk(x,k){
auto t
        t=3^k
	if(mod(x,2)==0) return(x/2)
	return((3*x+t)/2)
}
 
define p(n){
auto s
   s=0
   while(y%3==0){
        s=s+1
        y=y/3
   }
}
define collatzk(y,k){
	auto i,x,t,s
        t=3^k
        s=p(y)
	x=y
		for(i=0;i>=0;i++){
                        if(x%3==0 && x%2 && x%t!=0 && s<k){
                          s=s+1
                          print "x=",x," is odd and x is divisible by 3^",s,"\n"
                        }
                        if(x%t==0){
                          print "x=",x," is divisible by 3^",k,", x/3^k=",x/t,"\n"
                        }
			if(x==0 || x==t || x == -t || x == -5*t || x == -17*t){
				print "starting number = ",y,"\n"
				print "the number of iterations taken to reach ", x, " is "
				return(i)
			}
			(x=collatztk(x,k)) /* this prints t(x) */
		}
}