Kod Aturcara No. Perdana |
<< Back | Home | Next >> |
Nombor Perdana 1 (Up to 100 Nos Only)
_____________________________________________________________________
Private Sub Command1_Click()
Dim
a, b, c, x, y, nom As Integer
Dim
perdana(100) As Integer
nom
= Val(Text1.Text)
t
= Chr(9)
c
= 2
bil
= 0
List1.Clear
List1.AddItem ("Bil"
& t & "No Perdana")
For
b = 2 To nom Step 1
If
c = 2 Or c = 3 Then
perdana(b)
= c
bil
= bil + 1
x
= x + 1
List1.AddItem (bil & t & perdana(b))
Else
For
a = 3 To nom Step 2
If
(c Mod a = 0) Then
If
(c = a) Then
perdana(b)
= c
bil
= bil + 1
List1.AddItem (bil & t & perdana(b))
y
= y + 1
Exit
For
Else
Exit
For
End
If
End
If
Next
End
If
c
= c + 1
Next
Text2.Text
= x + y
End Sub
_____________________________________________________________________
(Thanks to Cik Salmah & Madinah)
Nombor Perdana 2 (Unlimited Nos)
_____________________________________________________________________
Private Sub Command1_Click()
num2 = Val(Text1)
Label2.Caption
= 1
List1.Clear
List1.AddItem
("No Perdana")
For
i = 1 To num2 - 1
Label2.Caption
= Label2.Caption + 1
Next
i
End Sub
Private Sub Label2_Change()
num
= Val(Label2.Caption)
If
num = 2 Then
List1.AddItem
(num)
End
If
For
i = 3 To Int(Sqr(num)) Step 2
If
num Mod i = 0 Then
Exit Sub
End If
Next
i
If
num Mod 2 = 0 Then
Exit
Sub
End
If
List1.AddItem
(num)
End Sub
_____________________________________________________________________
(Thanks to Cik Salmah)
TQ.
|