Determining Prime Numbers Visual Basic
Prime numbers are numbers which only numbers divisible by 1 and itself. But there are exceptions that number 2 is prime. The dereten primes are:
2,3,5,7,11,13,17, ...
For example number 3 is prime because 3 is only divisible by 1 and the number itself.
Denominator: the numerator Result Time
3: 1 3 0
3: 3 1 0
Number 3 is only divisible by 1 and 3
Examples of non-prime number is 4
Denominator: the numerator Result Time
4: 1 4 0
4: 2 2 0
4: 4 1 0
4 Numbers divisible by 1,2 and 4.
Algorithm
0. Start
1. Specify a number, eg N
2. Determine Count Prima, eg CPrima = 0
3. I repeat from 1 s.d N
Test Is N Mod I = 0
If Yes then Add CPrima = CPrima + 1
Test Is CPrima> 2
If Yes Go to Step 4
4. Repeat to 3
5. Test If CPrima2 So Not Prima N
6. Completed
Private Sub Form_Activate ()
Dim N, I, CPrima As Byte
N = 7 'number to be tested
CPrima = 0 'Count Zero Distribution
For I = 1 To N Step 1
IF (N Mod I = 0) Then
CPrima = CPrima + 1
IF CPrima> 2 Then Exit For
End IF
Next I
IF CPrima <= 2 Then
Print N; "Prime Numbers"
Else
Print N; "Not Numbers Prima"
End Sub
Monday, June 6, 2011
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment