Results 1 to 9 of 9

Thread: Listing Prime numbers within a group of numbers

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    27

    Unhappy Listing Prime numbers within a group of numbers

    Hello!

    How can I re-write this program with using arrays this time?
    Thank you


    Code:
     Dim lowerLimit, upperLimit, primeNumber As Integer
    
            Dim isPrime As Boolean = False
    
    
    
            lowerLimit = CType(TextBox1.Text, Integer)
    
            upperLimit = CType(TextBox2.Text, Integer)
    
    
    
            Do While lowerLimit < upperLimit
    
    
    
                primeNumber = lowerLimit
    
    
    
                If primeNumber = 2 Or primeNumber = 3 Then
    
                    isPrime = True
    
                ElseIf primeNumber = 1 Then
    
                    isPrime = False
    
                ElseIf primeNumber = upperLimit Then
    
                    Exit Do
    
                Else
    
                    For i As Integer = 2 To (primeNumber - 1)
    
                        If primeNumber Mod i = 0 Then
    
                            isPrime = False
    
                            Exit For
    
                        Else
    
                            isPrime = True
    
                        End If
    
                    Next
    
                End If
    
    
    
                If isPrime = True Then
    
                    TextBox3.Text += primeNumber & vbCrLf
    
                End If
    
    
    
                lowerLimit += 1
    
            Loop

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Listing Prime numbers within a group of numbers

    No idea. There does not appear to be any sane reason for using an array at any point in this process, not least because there is no fixed size collection anywhere to be recorded. If you were using a sieve method I suppose there might be an argument for using a 2D array but you're not, so there isn't.

    As a tip, bear in mind that no even numbers (after 2) can be prime so you might as well set your loop up to be from the first odd number in the range and then step through 2 at a time rather than the normal 1. It will speed things up more than a little.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3
    Fanatic Member
    Join Date
    Feb 2013
    Posts
    985

    Re: Listing Prime numbers within a group of numbers

    what you could do with an array is set up a table that lists all prime numbers up to a certain number, for example if number your going to check never going to be more than 100 then setup a table with all prime number between 0 and 100

    dim primearray(99) as boolean

    set all prime numbers to true using a text file for example when the program starts and then say you have number 50 to check

    if primearray(Yournumber - 1) = true then
    '''''''''its a prime number
    end if

    this method is much faster than a loop, andwith booleans you could have a massive table without a memory problem

    or

    have an array that simple lists all prime numbers

    dim primearray(99) as long ' put all prime number in here with text file or pre-calculate at startup

    then loop through the array instead of looping through and calculating each number in between
    Yes!!!
    Working from home is so much better than working in an office...
    Nothing can beat the combined stress of getting your work done on time whilst
    1. one toddler keeps pressing your AVR's power button
    2. one baby keeps crying for milk
    3. one child keeps running in and out of the house screaming and shouting
    4. one wife keeps nagging you to stop playing on the pc and do some real work.. house chores
    5. working at 1 O'clock in the morning because nobody is awake at that time
    6. being grossly underpaid for all your hard work


  4. #4
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Listing Prime numbers within a group of numbers

    I'm unsure what you mean by using arrays. Do you mean that you want to get an array of all prime numbers between a lower and upper bound?

    There is a better way to calculate if a number is a prime than what you're doing. A number is a prime if it can't be evenly divided by any number up to (including) the square root of that number. So a better algorithm to check if is a number is a prime is this:
    Code:
      Public Function IsPrime(number As Integer) As Boolean
        If number = 2 Then
          Return True
        End If
        If number Mod 2 = 0 OrElse number < 2 Then
          'no even numbers except 2 is a prime and 1 is by definition not a prime
          Return False
        End If
        Dim sqr As Integer = CInt(Math.Ceiling(Math.Sqrt(number)))
        'We can skip all even numbers in the loop since only even numbers are evenly divisible with
        'another even number and those aren't prime numbers
        For i = 3 To sqr Step 2
          If number Mod i = 0 Then
            'Have a divisor, not a prime
            Return False
          End If
        Next
        Return True
      End Function
    Now, if you want to write a function that returns an array of all primes between two given number then it could look something like this:
    Code:
      Public Function GetPrimeNumbers(lowerLimit As Integer, upperLimit As Integer) As Integer()
        If lowerLimit > upperLimit Then
          Throw New ArgumentException("lowerLimit cannot be higher than upperLimit")
        End If
        Dim list As New List(Of Integer)
        For i As Integer = lowerLimit To upperLimit
          If IsPrime(i) Then
            list.Add(i)
          End If
        Next
        Return list.ToArray()
      End Function
    Last edited by Joacim Andersson; May 23rd, 2013 at 05:44 AM.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    27

    Re: Listing Prime numbers within a group of numbers

    What I need to do is to write a program that gets the lower and upper bounds from the user and list the prime numbers between those bounds. I want get the prime values and load them into the arrays and then show those values in textbox

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Listing Prime numbers within a group of numbers

    That's exactly what the code I posted above does. Well, it get's the array of all prime numbers between a lower and upper bound. Now all you have to do is to list them the way you want.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    27

    Re: Listing Prime numbers within a group of numbers

    I can't understand how do I get the numbers out to textbox3 ?

    Public Function IsPrime(number As Integer) As Boolean
    If number = 2 Then
    Return True
    End If
    If number Mod 2 = 0 OrElse number < 2 Then
    'no even numbers except 2 is a prime and 1 is by definition not a prime
    Return False
    End If
    Dim sqr As Integer = CInt(Math.Ceiling(Math.Sqrt(number)))
    'We can skip all even numbers in the loop since only even numbers are evenly divisible with
    'another even number and those aren't prime numbers
    For i = 3 To sqr Step 2
    If number Mod i = 0 Then
    'Have a divisor, not a prime
    Return False
    End If
    Next
    Return True
    End Function
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim lowerLimit, upperLimit, primeNumber, i As Integer
    Dim isPrime As Boolean = False
    Integer.TryParse(TextBox1.Text, lowerLimit)
    Integer.TryParse(TextBox2.Text, upperLimit)
    Do While lowerLimit < upperLimit
    primeNumber = lowerLimit


    TextBox3.Text += ?????????????????????? & vbCrLf

    lowerLimit += 1
    Loop
    End Sub

  8. #8
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Listing Prime numbers within a group of numbers

    First of all you only pasted one of the two functions I posted, you need both of them. Secondly you actually also need to call on them.
    Is your TextBox a multiline TextBox? Wouldn't a ListBox be better to show the prime numbers?
    I will assume that TextBox3 is a multiline textbox and that you want to show one prime number on each line.
    Code:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      Dim lower, upper As Integer
      If Integer.TryParse(TextBox1.Text, lower) AndAlso Integer.TryParse(TextBox2.Text, upper) Then
        Dim primeNumbers() As Integer = GetPrimeNumbers(lower, upper)
        Dim text As String = ""
        For Each prime As Integer In primeNumbers
          text &= prime.ToString() & vbCrLf
        Next
        TextBox3.Text = text
      End If
    End Sub

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Mar 2013
    Posts
    27

    Re: Listing Prime numbers within a group of numbers

    Quote Originally Posted by Joacim Andersson View Post
    First of all you only pasted one of the two functions I posted, you need both of them. Secondly you actually also need to call on them.
    Is your TextBox a multiline TextBox? Wouldn't a ListBox be better to show the prime numbers?
    I will assume that TextBox3 is a multiline textbox and that you want to show one prime number on each line.
    Code:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
      Dim lower, upper As Integer
      If Integer.TryParse(TextBox1.Text, lower) AndAlso Integer.TryParse(TextBox2.Text, upper) Then
        Dim primeNumbers() As Integer = GetPrimeNumbers(lower, upper)
        Dim text As String = ""
        For Each prime As Integer In primeNumbers
          text &= prime.ToString() & vbCrLf
        Next
        TextBox3.Text = text
      End If
    End Sub


    Thank you very much

    This is exactly what I wanted

    Have a really good day !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width