' FACTORS.BAS ----01/94---- QBasic --- hh
' First version written in 1979 for the Apple II computer
' Time Difference function added 1/94
DEFDBL A-E, G-Z
DECLARE FUNCTION TimeDiff& (FirstTime$, SecondTime$)
REM
GOSUB 440
220 FOR F = 1 TO R
E = N / F: IF INT(E) <> E THEN 310
IF E < F THEN 310
B = F: D = E
F1 = F1 * -1
IF F1 = 1 THEN PRINT B, D, : ELSE PRINT B, D, "Pair No. "; FP - 1; "&"; FP
T = T + B + D
290 IF T > 2000000000000000# THEN F2 = 1: T = 0
FP = FP + 1
310 NEXT F
IF B = D THEN T = T - B
EndTime$ = TIME$
PRINT "Elapsed time in seconds ="; TimeDiff&(StartTime$, EndTime$)
PRINT "Sum of Aliquot divisers is: "; T
IF B = D THEN PRINT N; " is a Square Number"
IF D = N THEN PRINT N; " is a Prime Number"
IF T = N THEN PRINT N; " is a Perfect Number"
IF T < N THEN PRINT N; " is a Deficient Number"
IF T > N THEN PRINT N; " is an Abundant Number"
IF BELL = 1 THEN PRINT CHR$(7)
420 PRINT : GOSUB 550: GOTO 220
430 PRINT TAB(25); "***************": RETURN
440 KEY OFF: CLS : LOCATE 3, 25: GOSUB 430: PRINT TAB(25); "FP FACTOR PAIRS": GOSUB 430
GOSUB 430
PRINT : PRINT "Some Definitions----a number is...."
PRINT : PRINT "PRIME..... if the only factors are itself & one."
PRINT "PERFECT... if equal to the sum of its factors."
PRINT "SQUARE.... if equal to a factor squared."
PRINT "DEFICIENT. if greater then the sum of its factors."
PRINT "ABUNDANT.. if smaller then the sum of its factors."
PRINT : INPUT "Do you want a signal on completion ? (Y/'Return') "; B$
IF B$ = "Y" OR B$ = "y" THEN BELL = 1
550 PRINT : INPUT "What number do you wish factored ? (enter '0' to end) "; N
IF N = 0 THEN GOTO 640 ELSE IF N = 1 GOTO 550
F1 = -1: FP = 1
T = -N
R = INT(SQR(N)) + 1: PRINT
IF R > 9999999! GOTO 650
PRINT : PRINT "The Factor Pairs of "; N; " are:"
StartTime$ = TIME$
RETURN
640 CLS : KEY ON: END
650 PRINT N; " is too large for this program to factor."
GOTO 420
REM The size of the numbers capable of being factored by this program
REM is limited by the variable F which must be single precision.
REM 9,999,998 squared is 99,999,960,000,004. (9,999,999^ is too big!)
REM largest possible number (with ths program) is 99,999,971,016,703.
REM try 32749,32760,32761,12960000,466560000,59999999999 (that's ten 9's)
REM or 21,767,823,360,000 (4665600^, 558 factor pairs)
REM but allow about 11 hours !!! (on a 8080 based machine)
REM (about 1 hr on my 16 Mhz 386 -- 116 sec on 66 Mhz 486)
REM Written by H.Heinz in Aug./79 for Apples B.C. users group.
REM Adapted for Victor (with double precision) in June /83
REM Phone # (I'm interested in Rec. Math. Programs) 604-584-5501
FUNCTION TimeDiff& (FirstTime$, SecondTime$)
HrDiff% = VAL(LEFT$(SecondTime$, 2)) - VAL(LEFT$(FirstTime$, 2))
MinDiff% = VAL(MID$(SecondTime$, 4, 2)) - VAL(MID$(FirstTime$, 4, 2))
SecDiff% = VAL(MID$(SecondTime$, 7, 2)) - VAL(MID$(FirstTime$, 7, 2))
TimeDiff& = 3600& * HrDiff% + 60 * MinDiff% + SecDiff%
END FUNCTION