# My Humble "SIMPLE Sequence Solver"

June 16 2004 at 12:39 PM
Moderator

Response to Wrong

DEFINT A-Z
DECLARE FUNCTION Factorial (num)
DECLARE FUNCTION NextNum (op, n1, n2, n3, n4)
DECLARE FUNCTION Prime (num, ret)

OPTION BASE 1
DIM num(4) AS INTEGER

'20177
'20201
'20231
'20249
'20269
'20297

'This program requires correct input, it is NOT, AT ALL, idiot-proof.
' If you don't know about the INPUT statement, don't use this program.
CLS
PRINT "Input a number sequence 4 numbers long separated with commas."
INPUT "", num(1), num(2), num(3), num(4)
PRINT
PRINT

FOR i = 1 TO 5
ans = NextNum(i, num(1), num(2), num(3), num(4))
IF ans > -32767 THEN : EXIT FOR
NEXT i

PRINT "The next number is: "; ans

FUNCTION Factorial (num)
IF num > 0 THEN
ret = Factorial(num - 1) * num
ELSE
Factorial = 1
EXIT FUNCTION
END IF
Factorial = ret
END FUNCTION

FUNCTION NextNum (op, n1, n2, n3, n4)
'This returns a the correct answer or -.
'op is operation.
'1: + and -
'2: * and /
'3: !
'4: Primes
'5: Fibonacci(sp?) Series

DIM diff1, diff2, diff3 AS SINGLE

'Preset program knowledge.
'Test for + and -
IF op = 1 THEN
diff1! = (n2 - n1): diff2! = (n3 - n2)
'Test if the difference is + or -
diff3! = (diff2! + (diff2! - diff1!))
nnum! = n3 + diff3!

'If this = the fourth number then this pattern may be correct.
IF n4 = nnum! THEN
NextNum = n4 + (diff3! + (diff3! - diff2!))
EXIT FUNCTION
END IF

'Test if the difference is * or /
IF diff1! > 0 AND diff2! > 0 THEN
diff3! = (diff2! * (diff2! / diff1!))
nnum! = n3 + diff3!

'If this = the fourth number then this pattern may be correct.
IF n4 = nnum! THEN
NextNum = n4 + (diff3! * (diff3! / diff2!))
EXIT FUNCTION
END IF
END IF

'Test for * and /
ELSEIF op = 2 THEN
IF n2 = 0 AND n3 = 0 AND n4 = 0 THEN
NextNum = 0
EXIT FUNCTION
'It can't be * or / if any of the last 3 are 0 but they aren't all
' because the only way is */ 0 and that makes them all 0.
ELSEIF n1 <> 0 AND n2 <> 0 AND n3 <> 0 AND n4 <> 0 THEN
diff1! = (n2 / n1): diff2! = (n3 / n2)
'Test if the difference is + or -
diff3! = (diff2! + (diff2! - diff1!))
nnum! = n3 * diff3!

'If this = the fourth number then this pattern may be correct.
IF n4 = nnum! THEN
NextNum = n4 * (diff3! + (diff3! - diff2!))
EXIT FUNCTION
END IF

'Test if the difference is * or /
IF diff1! > 0 AND diff2! THEN
diff3! = (diff2! * (diff2! / diff1!))
nnum! = n3 * diff3!

'If this = the fourth number then this pattern may be correct.
IF n4 = nnum! THEN
NextNum = n4 * (diff3! * (diff3! / diff2!))
EXIT FUNCTION
END IF
END IF

END IF

ELSEIF op = 3 THEN
'1 2 3 4 5 6 7 ''''' 8 9 10
'1 2 6 24 120 720 5,040''''' 40,320 362,880 3,628,800
'(The only time you would recognize this series is if it was made
' up of the ones you know.)
FOR i = 1 TO 4
IF n1 = Factorial(i) AND n2 = Factorial(i + 1) THEN
IF n3 = Factorial(i + 2) AND n4 = Factorial(i + 3) THEN
NextNum = Factorial(i + 4)
EXIT FUNCTION
END IF
END IF
NEXT i
FOR i = 7 TO 4 STEP -1
IF n1 = Factorial(i) AND n2 = Factorial(i - 1) THEN
IF n3 = Factorial(i - 2) AND n4 = Factorial(i - 3) THEN
NextNum = Factorial(i - 4)
EXIT FUNCTION
END IF
END IF
NEXT i

ELSEIF op = 4 THEN
'Primes
IF Prime(n1, 0) = 0 AND Prime(n2, 0) = 0 AND Prime(n3, 0) = 0 AND Prime(n4, 0) = 0 THEN
diff = Prime(n1, n2)
count = diff + 1
i = n4
DO
i = Prime(i, -1)
count = count - 1
LOOP UNTIL count = 0
NextNum = i
EXIT FUNCTION
END IF

ELSEIF op = 5 THEN
'Fibonacci(sp?) Series
IF n1 + n2 = n3 AND n2 + n3 = n4 THEN
NextNum = n3 + n4
EXIT FUNCTION
ELSEIF n1 - n2 = n3 AND n2 - n3 = n4 THEN
NextNum = n3 - n4
EXIT FUNCTION
ELSEIF n1 * n2 = n3 AND n2 * n3 = n4 THEN
NextNum = n3 * n4
EXIT FUNCTION
ELSEIF n1 <> 0 AND n2 <> 0 AND n3 <> 0 AND n4 <> 0 THEN
IF n1 / n2 = n3 AND n2 / n3 = n4 THEN
NextNum = n3 / n4
EXIT FUNCTION
END IF
END IF

END IF

NextNum = -32767
END FUNCTION

FUNCTION Prime (num, ret)
'ret determines what is returned. -3 indicates an error
'0 - Whether it is prime or not.
'> 0 - How many prime numbers are between num and ret
'-1 - The next prime number above num.
'-2 - The next prime number below num.
x# = num
IF ret = 0 THEN
IF (x# / 2) = INT(x# / 2) AND NOT x# = 2 THEN
Prime = 2
EXIT FUNCTION
ELSE
FOR i# = 3 TO INT(SQR(num)) + 1 STEP 2
IF (x# / i#) = INT(x# / i#) THEN
Prime = i#
EXIT FUNCTION
END IF
NEXT i#
END IF
Prime = 0
EXIT FUNCTION
ELSEIF ret > 0 THEN
IF num < ret THEN
count = 0
FOR i = (num + 1) TO (ret - 1)
IF Prime(i, 0) = 0 THEN
count = count + 1
END IF
NEXT i
Prime = count
EXIT FUNCTION

ELSEIF num > ret THEN
count = 0
FOR i = (ret + 1) TO (num - 1)
IF Prime(i, 0) = 0 THEN
count = count + 1
END IF
NEXT i
Prime = count
EXIT FUNCTION

END IF

ELSEIF ret = -1 THEN
i = num + 1
DO UNTIL Prime(i, 0) = 0
i = i + 1
LOOP
Prime = i
EXIT FUNCTION

ELSEIF ret = -2 THEN
i = num - 1
DO UNTIL i = 0 OR Prime(i, 0) = 0
i = i - 1
LOOP
IF i = 0 THEN : Prime = -3: EXIT FUNCTION
Prime = i
EXIT FUNCTION

END IF
Prime = -3
END FUNCTION

 Respond to this message
Responses