This file contains two separate programs
(recaman1.f and recaman2.f) for Recaman's sequences A005132 (a(*)), A057167 (h(*)),
and many associated sequences.

c recaman1.f
c Fortran program for  Recaman's sequences A005132 (a(*)), A057167 (h(*)),
c    A064227, A064228, A064288, A064289, etc
c
c a(*) = the sequence
c h(n) = 1 if we have hit i already, else c
c tim(n) = no. of times n appears
c rec(n) = record time to hit a tough n
c ntough(n) = list of tough n's
c ht = height of current term = no. of addition steps - no. of substraction steps
c height(n) = height of n when it appears
c htime(n) = height of number reached at n-th step = ht of n-th term
c recht(m) = first number of height m
c rechtn(m) = time when get first number of height m
c lasth(m) = last number of height m
c lasthn(m) = time when get last number of height m
c
	implicit integer(a-z)
	integer rec(10000),ntough(1000),tim(100)
	integer height(100),htime(100),recht(100),lasthn(100)
	integer rechtn(100),lasth(100)
	real xjjj
	integer*4 h,a
	common /fred/	h(1300000000),a(100000000)
c set limits
	maxn=1300000000
c	maxi=926169
        maxi=1000
	maxrec=1000
	maxt=100
	maxht=100
c initiate arrays etc
	c=-1
c h(i) = 1 if we have hit i already, else c
	do 1 i=1,maxn
1	h(i)=c
c tim(n) = no. of times n appears
	do 16 i=1,maxt
16	tim(i)=0
	a(1)=1
	h(1)=1
	tim(1)=1
	record=1
	rec(1)=1
	ntough(1)=1
	norec=1
	ht=1
	height(1)=1
	htime(1)=1
	recht(1)=1
	rechtn(1)=1
	lasth(1)=1
	lasthn(1)=1
	bight=1
c the main loop
	do 10 nx=2,maxi
c 	write(*,*)"nx = ",nx,a(nx-1)
c can we subtract?
	t1=a(nx-1)-nx
	if(t1.le.0)goto 11
	if(t1.gt.maxn)goto 3
	if(h(t1).eq.c)goto 12
c no must add
11	t1=a(nx-1)+nx
	ht=ht+1
	if(ht.le.bight)goto 30
c new record height
	bight=ht
	if(bight.le.maxht) recht(ht)=t1
	if(bight.le.maxht) rechtn(ht)=nx
30	continue
	if(t1.gt.maxn)goto 2
	a(nx)=t1
	if(h(t1).eq.c) h(t1)=nx
	if(t1.le.maxt)tim(t1)=tim(t1)+1
	if(h(t1).eq.nx.and.t1.le.maxht)height(t1)=ht
	if(nx.le.maxht)htime(nx)=ht
	if(ht.le.maxht)lasth(ht)=t1
	if(ht.le.maxht)lasthn(ht)=nx
	goto 10
c yes can subtract
12	a(nx)=t1
	h(t1)=nx
	ht=ht-1
	if(t1.le.maxt)tim(t1)=tim(t1)+1
	if(t1.le.maxht)height(t1)=ht
	if(nx.le.maxht)htime(nx)=ht
	if(ht.le.maxht)lasth(ht)=t1
	if(ht.le.maxht)lasthn(ht)=nx
10	continue
c done
	xjjj=-float(a(maxi))/float(maxi)+float(ht)
	jjj=xjjj
c get records
	nlast=maxn
	do 4 n=1,maxn
	if(h(n).eq.c)goto 5
	if(h(n).le.record)goto 4
	norec=norec+1
	record=h(n)
	if(norec.le.maxrec)goto 6
	write(*,*)"Too many records"
	stop
6	rec(norec)=h(n)
 	ntough(norec)=n
4	continue
5	nlast=n
	write(*,*)"the sequence"
	write(*,101)(a(i),i=1,500)
	write(*,*)"when it hits"
	write(*,101)(h(i),i=1,3000)
101	format(10i8)
	write(*,*)"records:"
	write(*,101)(rec(i),i=1,norec)
	write(*,*)"tough n's:"
	write(*,101)(ntough(i),i=1,norec)
	write(*,*)"First missing n = ",nlast
	write(*,*)"frequencies:"
	write(*,101)(tim(i),i=1,maxt)
	write(*,*)"height of n:"
	write(*,101)(height(i),i=1,maxht)
	write(*,*)"height of a(n):"
	write(*,101)(htime(i),i=1,maxht)
	write(*,*)"records for height (values)"
	write(*,101)(recht(i),i=1,maxht)
	write(*,*)"records for height (times)"
	write(*,101)(rechtn(i),i=1,maxht)
	write(*,*)"jjj = ",jjj
	write(*,*)" value at last time had height h:"
	write(*,101)(lasth(i),i=1,jjj)
	write(*,*)"last time had height h:"
	write(*,101)(lasthn(i),i=1,jjj)
	stop
c term got too big
2	continue
	write(*,*)"Huge2: ",nx,t1
	stop
3	continue
	write(*,*)"Huge3: ",nx,t1
	stop
	end





c recaman2.f
c This version computes the first billion terms

c Fortran program for  Recaman's sequences A005132 (a(*)), A057167 (h(*)),
c    A064227, A064228, A064288, A064289, etc
c
c a(*) = the sequence
c h(n) = 1 if we have hit i already, else c
c tim(n) = no. of times n appears
c rec(n) = record time to hit a tough n
c ntough(n) = list of tough n's
c ht = height of current term = no. of addition steps - no. of substraction steps
c height(n) = height of n when it appears
c htime(n) = height of number reached at n-th step = ht of n-th term
c recht(m) = first number of height m
c rechtn(m) = time when get first number of height m
c lasth(m) = last number of height m
c lasthn(m) = time when get last number of height m
c
	implicit integer(a-z)
	integer rec(10000),ntough(1000),tim(100)
	integer height(100),htime(100),recht(100),lasthn(100)
	integer rechtn(100),lasth(100)
	real xjjj
	integer*1 h
	common /fred/	h(16000000000)
c set limits
	maxn=16000000000
        maxi=1000000000
	maxrec=1000
	maxt=100
	maxht=100
c initiate arrays etc
	c=0
c h(i) = 1 if we have hit i already, else c
c	do 1 i=1,maxn
c1	h(i)=c
c tim(n) = no. of times n appears
	do 16 i=1,maxt
16	tim(i)=0
	anow=1
	h(1)=1
	tim(1)=1
	record=1
	rec(1)=1
	ntough(1)=1
	norec=1
	ht=1
	height(1)=1
	htime(1)=1
	recht(1)=1
	rechtn(1)=1
	lasth(1)=1
	lasthn(1)=1
	bight=1
c the main loop
	do 10 nx=2,maxi
c 	write(*,*)"nx = ",nx,a(nx-1)
c can we subtract?
c	t1=a(nx-1)-nx
	t1=anow-nx
	if(t1.le.0)goto 11
	if(t1.gt.maxn)goto 3
	if(h(t1).eq.c)goto 12
c no must add
11	t1=anow+nx
	ht=ht+1
	if(ht.le.bight)goto 30
c new record height
	bight=ht
	if(bight.le.maxht) recht(ht)=t1
	if(bight.le.maxht) rechtn(ht)=nx
30	continue
	if(t1.gt.maxn)goto 2
c	a(nx)=t1
	anow=t1
	if(h(t1).eq.c) h(t1)=1
	if(t1.le.maxt)tim(t1)=tim(t1)+1
	if(h(t1).eq.1.and.t1.le.maxht)height(t1)=ht
	if(nx.le.maxht)htime(nx)=ht
	if(ht.le.maxht)lasth(ht)=t1
	if(ht.le.maxht)lasthn(ht)=nx
	goto 10
c yes can subtract
12	continue
c	a(nx)=t1
	anow=t1
	h(t1)=1
	ht=ht-1
	if(t1.le.maxt)tim(t1)=tim(t1)+1
	if(t1.le.maxht)height(t1)=ht
	if(nx.le.maxht)htime(nx)=ht
	if(ht.le.maxht)lasth(ht)=t1
	if(ht.le.maxht)lasthn(ht)=nx
10	continue
c done
	xjjj=-float(anow)/float(maxi)+float(ht)
	jjj=xjjj
c get records
c	nlast=maxn
c	do 4 n=1,maxn
c	if(h(n).eq.c)goto 5
c	if(h(n).le.record)goto 4
c	norec=norec+1
c	record=h(n)
c	if(norec.le.maxrec)goto 6
c	write(*,*)"Too many records"
c	stop
c6	rec(norec)=h(n)
c 	ntough(norec)=n
c4	continue
c5	nlast=n
c	write(*,*)"the sequence"
c	write(*,101)(a(i),i=1,300)
c	write(*,*)"when it hits"
c	write(*,101)(h(i),i=1,3000)
101	format(6i12)
c	write(*,*)"records:"
c	write(*,101)(rec(i),i=1,norec)
c	write(*,*)"tough n's:"
c	write(*,101)(ntough(i),i=1,norec)
c	write(*,*)"First missing n = ",nlast
	write(*,*)"frequencies:"
	write(*,101)(tim(i),i=1,maxt)
	write(*,*)"height of n:"
	write(*,101)(height(i),i=1,maxht)
	write(*,*)"height of a(n):"
	write(*,101)(htime(i),i=1,maxht)
	write(*,*)"records for height (values)"
	write(*,101)(recht(i),i=1,maxht)
	write(*,*)"records for height (times)"
	write(*,101)(rechtn(i),i=1,maxht)
	write(*,*)"jjj = ",jjj
	write(*,*)" value at last time had height h:"
	write(*,101)(lasth(i),i=1,jjj)
	write(*,*)"last time had height h:"
	write(*,101)(lasthn(i),i=1,jjj)
	stop
c term got too big
2	continue
	write(*,*)"Huge2: ",nx,t1
	stop
3	continue
	write(*,*)"Huge3: ",nx,t1
	stop
	end