4 / 0= 1.#INF Int(n/a)= 1.#INF sp= 0

4 / 1= 4 Int(n/a)= 4 sp= 0

4 / 2= 2 Int(n/a)= 2 sp= 0

At this point the for next has reached it's limit of half of the number ie. 2 is half of 4. so the program exits the for next and proceeds to the next statement which is to print the number is prime.

Where as:

8 / 0= 1.#INF Int(n/a)= 1.#INF sp= 0

8 / 1= 8 Int(n/a)= 8 sp= 0

8 / 2= 4 Int(n/a)= 4 sp= 0

8 / 3= 2.666666666666667 Int(n/a)= 2 sp= 1

Here on the 4th test (testing the number 3)

8/3 = 2.6667 is not equal to 2 (the integerof 2.6), so it sets sp to 1 and exits to for next, jumping to the noprime subroutine.

Code: Select all

`Input "Number: ",n`

Cls

Print "Processing... This may take a while..."

For a = 0 To Int(n/2)

If n/a = Int(n/a) Then sp=0 Else sp = 1

Print n;" /";a;"=";n/a;" Int(n/a)=";Int(n/a);" sp=";sp

If sp = 1 Then Goto noprime

Next a

Print "exiting For Next"

Print "The number";n;" is prime. Press any key to quit."

Do:Loop While Inkey$ = ""

End

noprime:

Print "The number";n;" is not prime. Press any key to quit."

Do:Loop While Inkey$ = ""

End

A simple example to find primes is:

The for next uses a step of 2 starting at 3 to the square root of the number to test.

For example if you were to test 121 for primality the For Next would test from 3 to 11 in steps of 2 ie. (3,5,7,9,11)

There is no need to test any higher then the square root of the number.

Code: Select all

`Dim As Integer n,q`

Print "Prime number test"

Input "Enter an odd number (3 or higher) ";n

For q = 3 To Int(sqr(n)) Step 2

If n Mod q = 0 Then

Print n; " is not prime, it is divisible by";q

Print "press any key to end"

Sleep

end

EndIf

Next

Print n; " is prime"

Print "press any key to end"

Sleep

end