Need Help
-
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
-
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
: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 thatp
), i / p is not a round number. I have a code snippet if you want. :cool: -
: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 thatp
), i / p is not a round number. I have a code snippet if you want. :cool: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:
-
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:
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