Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Need Help

Need Help

Scheduled Pinned Locked Moved Visual Basic
helpcsharpcssdata-structures
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jason Baggett
    wrote on last edited by
    #1

    I'm learning VB.Net and a problem I am trying to do asks the following: Write a procedure that will print all prime numbers from 1 to N, where N is passed to the procedure as a parameter. A prime number is one that can be evenly divided only by 1 or itself. The smallest prime numbers are 1,2,3 and 5. Write a program to test your function. Hint: Identify the primes from smallest to the largest and store all the identified prime numbers in an array. Determine whether a new number is a prime number by checking the remainder of this number divided by each of the currently identified prime numbers, except for 1. If a new number cannot be evenly divided (i.e. no remainder) by any prime numbers that are less than the square root of the number, the new number is a prime number. I'm lost!!! Can anyone help me? Thanks JB

    M 1 Reply Last reply
    0
    • J Jason Baggett

      I'm learning VB.Net and a problem I am trying to do asks the following: Write a procedure that will print all prime numbers from 1 to N, where N is passed to the procedure as a parameter. A prime number is one that can be evenly divided only by 1 or itself. The smallest prime numbers are 1,2,3 and 5. Write a program to test your function. Hint: Identify the primes from smallest to the largest and store all the identified prime numbers in an array. Determine whether a new number is a prime number by checking the remainder of this number divided by each of the currently identified prime numbers, except for 1. If a new number cannot be evenly divided (i.e. no remainder) by any prime numbers that are less than the square root of the number, the new number is a prime number. I'm lost!!! Can anyone help me? Thanks JB

      M Offline
      M Offline
      Marc 0
      wrote on last edited by
      #2

      :wtf: ! Nice hint is that, it only makes it harder to understand... X| Okay, maybe this will help you in the right direction: Make a loop that goes from 2 (not 1!) to N. Something like For i As Integer = 2 To N. If i is a prime, store it in an array. i is a prime if for each number stored in the array (lets call that p), i / p is not a round number. I have a code snippet if you want. :cool:

      M 1 Reply Last reply
      0
      • M Marc 0

        :wtf: ! Nice hint is that, it only makes it harder to understand... X| Okay, maybe this will help you in the right direction: Make a loop that goes from 2 (not 1!) to N. Something like For i As Integer = 2 To N. If i is a prime, store it in an array. i is a prime if for each number stored in the array (lets call that p), i / p is not a round number. I have a code snippet if you want. :cool:

        M Offline
        M Offline
        Marc 0
        wrote on last edited by
        #3

        Ok, here's the function:

        Public Shared Function GetPrimes(ByVal n As Integer) As Integer()
            Dim primes As New ArrayList
            'Print 1 at the start, don't add to primes!
            Debug.WriteLine(1)
        
            For i As Integer = 2 To n
                'Calculate the square root of i
                Dim root As Integer = CInt(Math.Sqrt(i))
                'Is this number a prime?
                Dim prime As Boolean = True
                'Go to each previously calculated prime
                For Each p As Integer In primes
                    'Check to see if the prime is smaller than the square root
                    If (p <= root) Then
                        'Check if rounded division and not-rounded division are the same
                        If (i / p) = (i \\ p) Then
                            'Not a prime
                            prime = False
                            'No need to check any further
                            Exit For
                        End If
                    End If
                Next p
        
                If prime Then
                    Debug.WriteLine(i)
                    'Add the prime to the arraylist
                    primes.Add(i)
                End If
            Next i
        
            'Insert 1 at the start of primes
            'Can't do that at the beginning, because 100 / 1 = 100 \\ 1
            primes.Insert(0, 1)
            'Return the primes
            Return CType(primes.ToArray(GetType(Integer)), Integer())
        End Function
        

        Hope this helped you out! :cool:

        J 1 Reply Last reply
        0
        • M Marc 0

          Ok, here's the function:

          Public Shared Function GetPrimes(ByVal n As Integer) As Integer()
              Dim primes As New ArrayList
              'Print 1 at the start, don't add to primes!
              Debug.WriteLine(1)
          
              For i As Integer = 2 To n
                  'Calculate the square root of i
                  Dim root As Integer = CInt(Math.Sqrt(i))
                  'Is this number a prime?
                  Dim prime As Boolean = True
                  'Go to each previously calculated prime
                  For Each p As Integer In primes
                      'Check to see if the prime is smaller than the square root
                      If (p <= root) Then
                          'Check if rounded division and not-rounded division are the same
                          If (i / p) = (i \\ p) Then
                              'Not a prime
                              prime = False
                              'No need to check any further
                              Exit For
                          End If
                      End If
                  Next p
          
                  If prime Then
                      Debug.WriteLine(i)
                      'Add the prime to the arraylist
                      primes.Add(i)
                  End If
              Next i
          
              'Insert 1 at the start of primes
              'Can't do that at the beginning, because 100 / 1 = 100 \\ 1
              primes.Insert(0, 1)
              'Return the primes
              Return CType(primes.ToArray(GetType(Integer)), Integer())
          End Function
          

          Hope this helped you out! :cool:

          J Offline
          J Offline
          Jason Baggett
          wrote on last edited by
          #4

          Thanks dude, I'll look at it later today. I might be hollering at you for further help if you don't mind....??? email me direct at jbaggett@hotfish.com for my Instant Message names

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups