; this program searches for the first power of 7 
; that contains six consecutive "7"

100 CALL CLEAR
$MLC F 110 10 3000
300 CALL LINK("SEVEN",N,A$)
310 PRINT "7 TO THE POWER OF ";N; " IS"
320 PRINT A$
330 END
$SEVEN
	DIMTABLE F 256			; F points to the special 256 bytes buffer
	DIMTABLE G 256			; G points to 256 bytes (for returning string)
	STARTDATA
	   BYTES 0,7,4,1,8,5,2,9,6,3
           BYTES 0,0,1,2,2,3,4,4,5,6
        ENDDATA	E			; E points to this multiplication table
	LET P 1				; current power = 1
	PUTTABLE F 0 7			; current value is 7 at first byte

	; R0 = pointer on F table
	; R1 = pointer to E table
	; R2 = 7
	; R3 = one digit and pointer into E
	; R4 = one digit after multiplication
	; R5 = carry
	; R6 = number of digits-1 into F table
	; R7 = number of consecutive "7" currently found
	; R8 = flag for FOUND
	; R9 = loop counter on digits while mul*7
	; R10 = 10	

	$[
	LI R6,1				; current LEN in digits in F table
	CLR R8				; flag for "found"
	LI R10,>A00			; R10 = byte 10
	LI R2,>700			; R2 = byte 7
	MOV @E,R1			; mutliplication table E
	MAIN_LOOP:
		CLR R7
		CLR R5
		MOV R6,R9
		MOV @F,R0
		MULTIPLY_LOOP:
			CLR R3			; for byte operation
			MOVB *R0,R3		; new digit
			SWPB R3			; to word
			A R1,R3			; E+digit -> points to the units of table*7
			MOVB *R3,R4		; get units
			AB R5,R4		; plus carry
			MOVB @10(R3),R5		; E+digit+10 -> points to tenths of table*7, new carry
			CB R4,R10		; more than a digit?
			JLT AA:
				SB R10,R4	; if so, reports ten...
				AI R5,>100	; ...on the carry (byte)
		AA:
			MOVB R4,*R0+		; store new digit
			CB R2,R4		; is it a "7" ?
			JNE BB:
				INC R7		; if so, one more found
				CI R7,6		; six "7" ?
				JNE CC:
					INC R8	; yes!!! end of search !
				JMP CC:
		BB:
				CLR R7		; not a "7"... reset counter
		CC:
		DEC R9				; loop counter
		JNE MULTIPLY_LOoP:	; no, return to DEBUT
		CI R5,0				; a carry remains?
		JEQ DD:
			INC R6			; yes! One more digit
			MOVB R5,*R0		; and store it
		DD:
		INC @P				; power+1
	CI R8,0					; flag for found
	JEQ MAIN_LOOP:			; still zero... back to MAIN
	DEC R6					; to return LEN-1
	MOV R6,@Z				; else, update number of digits
	$]	
		
			
			
	LET J Z
	FOR I 0 Z
		GETTABLE F I X			; one digit from F table
		ADD X 48			; in ASCII
		PUTTABLE G J X			; one character in G string
		DEC J
	NEXT
	INC Z					; correct len
	LET U 2
	PUTTABLE U 0 G				; set A$ to G string with Z characters
        PUTPARAM 1 P				; set N to the current power
$$
$END
