C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






To Generate Prime No In VB.net


Posted Date: 03 Jul 2008      Total Responses: 2

Posted By: Afsar       Member Level: Bronze     Points: 1



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim X As Integer
Dim Y As Integer
Dim flag As Boolean
ListBox1.Items.Clear()
Y = 1
Do While Not ListBox1.Items.Count = Val(TextBox1.Text)
Y = Y + 1
flag = True
For X = 2 To Y
If X = Y Then
If flag = True Then
ListBox1.Items.Add(Y)
Exit For
End If
Else
If (Y Mod X = 0) Then
flag = False
End If
End If
Next
Loop

Label1.Text = "Total No. of Prime No. - " & ListBox1.Items.Count
End Sub





Responses

Author: Dharmaraj    03 Jul 2008Member Level: DiamondRating:     Points: 6

Hi,
Code for Prime numbers

Option Explicit
Private m_lngPrimeArray() As Long
Public Sub GenPrimes(ByVal lngCount As Long)

Dim lngCounter As Long
Dim lngNumber As Long
Dim lngDevCounter As Long
Dim blnPrime As Boolean
Dim lngDevUBound As Long
Dim sngStart As Single

sngStart = Timer

ReDim m_lngPrimeArray(lngCount + 2)

'The first prime is 2 (only devidable by 1 and itself)!
m_lngPrimeArray(1) = 2

'Initialize some counters.
lngNumber = 3
lngCounter = 2
lngDevUBound = 1
Do While lngCounter <= (lngCount + 2)

'Find the array index that contains the known prime
'smaller than the root of the number.

Do While m_lngPrimeArray(lngDevUBound + 1) _
< Sqr(lngNumber) And Not _
m_lngPrimeArray(lngDevUBound + 1) = 0
lngDevUBound = lngDevUBound + 1
Loop

'Assume this number will be a prime.
blnPrime = True

'Try dividing this number by any allready found prime
'which is smaller
'then the root of this number.
For lngDevCounter = 1 To lngDevUBound
If lngNumber Mod m_lngPrimeArray(lngDevCounter) = 0 Then
'Sorry number is dividable so no prime.
blnPrime = False
Exit For
End If
Next lngDevCounter

If blnPrime Then
'Gues we found a new prime.
m_lngPrimeArray(lngCounter) = lngNumber
'Increase prime found count.
lngCounter = lngCounter + 1
End If
'Increase number.
lngNumber = lngNumber + 2
Loop

Debug.Print Timer - sngStart

End Sub



Author: sudha    04 Jul 2008Member Level: GoldRating:     Points: 6

protected void Button1_Click(object sender, EventArgs e)
{
//SPECIFYING NUMBER RANGE
for (int i = 1; i <= 40; i++)
{
bool a = CheckNo(Convert.ToInt32(i));
if (a == true)
{
Response.Write(i + " " + "Number is Prime" + " ");
}
else
{
Response.Write(i + " " + "Number is Not Prime" + " );
}
}
}

//FUNCTION FOR DETECTING PRIME NUMBERS
protected bool CheckNo(int number)
{
bool Flag = true;
if (number == 1 && number == 2)
{
Flag = false;
}
for (int i = number - 1; i >= 2; i--)
{
if (number % i == 0)
{
Flag = false; break;
}
}
return Flag;
}




Post Reply
You must Sign In to post a response.
Next : Can any one help me out , Factory classes
Previous : select query
Return to Discussion Forum
Post New Message
Category: .NET

Related Messages



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use